Valhalla Legends Forums Archive | General Programming | Porting C++ to VB

AuthorMessageTime
ChR0NiC
Hey I was just having some fun seeing what I knew since I started learning C++, and I tried porting some stuff from C++ to VB. Can I have some opinions on what I might have done wrong and why it's wrong. Just so I can improve, and I know what areas to study more.

Original C++ Code
[code]
__declspec(dllexport)
BOOL _stdcall CreateAccount(char *OutBuf, char *password) {
   DWORD dwHashBuffer[5];
   calchashbuf(dwHashBuffer, password, strlen(password));
   memcpy(OutBuf, dwHashBuffer, 5*4);
   return TRUE;
}
[/code]

Ported to VB
[code]
Public Function CreateAccount(Outbuf As String, password As String) As Boolean
Dim dwHashBuffer(5) As Double
Call Calchashbuf(Join(dwHashBuffer, vbNullString), password, Len(password))
Call CopyMemory(Outbuf, Join(dwHashBuffer, vbNullString), 5 * 4)
CreateAccount = True
End Function
[/code]

Original C++ Code
[code]
void _stdcall calchashbuf(unsigned long *hash, void *inbuf, int len) {
   char *buf = (char*)inbuf;
   int pos;
   int sublen;
   unsigned long hashbuf[0x10 + 5];
   hashbuf[0] = 0x67452301;
   hashbuf[1] = 0xEFCDAB89;
   hashbuf[2] = 0x98BADCFE;
   hashbuf[3] = 0x10325476;
   hashbuf[4] = 0xC3D2E1F0;
   for(pos=0; pos<len; pos+=0x40) {
      sublen = len - pos;
      if(sublen > 0x40)
         sublen = 0x40;
      memcpy(hashbuf+5, buf+pos, sublen);
      if(sublen<0x40)
         ZeroMemory((char*)(hashbuf+5)+sublen, 0x40-sublen);
      datahash(hashbuf);
   }
   memcpy(hash, hashbuf, 5*4);
}
[/code]

Ported to VB code
[code]
Public Function Calchashbuf(Hash As Long, inbuf As Variant, Length As Integer)
buf = CStr(inbuf)
Dim pos As Integer
Dim sublen As Integer
Dim hashbuf(&H10 + 5)
hashbuf(0) = &H67452301
hashbuf(1) = &HEFCDAB89
hashbuf(2) = &H98BADCFE
hashbuf(3) = &H10325476
hashbuf(4) = &HC3D2E1F0

For pos = 0 To Length - 1
pos = pos + &H40
sublen = Length - pos
If sublen > &H40 Then sublen = &H40
Call CopyMemory(Join(hashbuf, vbNullString) + 5, buf + pos, sublen)
If sublen < &H40 Then
Call ZeroMemory(Join(hashbuf, vbNullString) + 5 + sublen, &H40 - sublen)
Datahash (Join(hashbuf, vbNullString))
End If
Next pos
Call CopyMemory(Hash, hashbuf, 5 * 4)
End Function
[/code]

I am also doing DataHash, but it's not done yet, and if I'm totally screwing up and not doing anything correctly I wanna fix it before moving on.

Edit: I'm sure many of you recognize this C++ code as the BNETAuth C++ source code created by YobGuls. And yes, if you guessed that, you are obviously right.

I also used instead of memcpy and zeromemory used.
[code]
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
(ByRef Destination As Any, ByRef Source As Any, ByVal numbytes As Long)

Public Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" _
(ByRef Destination As Any, ByVal numbytes As Long)
[/code]
July 12, 2004, 4:20 AM
UserLoser.
Are you allocating enough space for Outbuf in your AccountCreate function?
July 12, 2004, 6:58 AM
ChR0NiC
[quote author=UserLoser. link=board=5;threadid=7670;start=0#msg69928 date=1089615483]
Are you allocating enough space for Outbuf in your AccountCreate function?
[/quote]

Hmmmm, do you mean the function I am using to call the CreateAccount function shown here?
July 13, 2004, 1:16 AM
Lenny
Yes, you need to pre allocate space for strings before using CopyMemory.

[code]Outbuf = Space(20)
[/code]
July 13, 2004, 5:46 AM
hismajesty
Does your CalcHashBuf work? I don't think the DataHash call should be included in the if statement.

Edit: Nevermind, read something wrong. *removed comment*
July 13, 2004, 5:09 PM
Clokr_
Isnt a C++ DWORD a long in VB? (4 bytes)
August 10, 2004, 2:25 PM
St0rm.iD
instead of dword[5], try doing a string of length 20.
August 10, 2004, 4:26 PM
Clokr_
For pos = 0 To Length - 1
pos = pos + &H40

That's wrong, in the C++ code it pluses H40 each round, instead in the VB code it pluses 1 (the for) and then H40, so it pluses H41. Instead you should use a for with step:

For pos = 0 to Length - 1 Step &H40
...
Next pos

or use a do-loop:

Do while pos < Length
pos = pos + &H40
...
Loop
August 10, 2004, 5:08 PM
Myndfyr
[quote author=Clokr_ link=board=5;threadid=7670;start=0#msg74888 date=1092147938]
Isnt a C++ DWORD a long in VB? (4 bytes)
[/quote]

Hello! Welcome to the Valhalla Legends forums!

I've noticed that on a couple threads, you've resurrected them back from the dead (just short of a month since the last post). I haven't seen if anyone else has censored you for it yet, but this is just a heads-up -- people tend to like to leave topics that have remained untouched for a while that way -- untouched. ;)

Also, when adding code, use [ code ] [ /code ] tags (no spaces:

[code]
For i = 0 To 10
MsgBox( i )
Next
[/code]

That ensures proper fixed-width formatting and preserves whitespace.

Enjoy your stay :)
August 11, 2004, 12:45 AM
Grok
[quote author=MyndFyre link=board=5;threadid=7670;start=0#msg74972 date=1092185107]
[quote author=Clokr_ link=board=5;threadid=7670;start=0#msg74888 date=1092147938]
Isnt a C++ DWORD a long in VB? (4 bytes)
[/quote]

Hello! Welcome to the Valhalla Legends forums!

I've noticed that on a couple threads, you've resurrected them back from the dead (just short of a month since the last post). I haven't seen if anyone else has censored you for it yet, but this is just a heads-up -- people tend to like to leave topics that have remained untouched for a while that way -- untouched. ;)
[/quote]

Less than a month is not long, especially considering he added content to the topic. If he brought it back just to say "oh", that would annoy people.
August 11, 2004, 5:48 PM
Clokr_
[quote author=MyndFyre link=board=5;threadid=7670;start=0#msg74972 date=1092185107]
[quote author=Clokr_ link=board=5;threadid=7670;start=0#msg74888 date=1092147938]
Isnt a C++ DWORD a long in VB? (4 bytes)
[/quote]

Hello! Welcome to the Valhalla Legends forums!

I've noticed that on a couple threads, you've resurrected them back from the dead (just short of a month since the last post). I haven't seen if anyone else has censored you for it yet, but this is just a heads-up -- people tend to like to leave topics that have remained untouched for a while that way -- untouched. ;)

Also, when adding code, use [ code ] [ /code ] tags (no spaces:

[code]
For i = 0 To 10
MsgBox( i )
Next
[/code]

That ensures proper fixed-width formatting and preserves whitespace.

Enjoy your stay :)
[/quote]

1- I just joined the forum and it showed NEW button. The topics were on the first page and usually in 99% of forums that mean that they are not that old. Also, I didnt spam.
2- There is no Code button, I didnt know if the tag was CODE or not.
August 11, 2004, 7:30 PM
Adron
[quote author=Clokr_ link=board=5;threadid=7670;start=0#msg75067 date=1092252612]
1- I just joined the forum and it showed NEW button. The topics were on the first page and usually in 99% of forums that mean that they are not that old. Also, I didnt spam.
2- There is no Code button, I didnt know if the tag was CODE or not.
[/quote]

So... You're a newbie and a llama eh?
August 11, 2004, 8:44 PM
Lenny
It's safe to say this topic should not have been revived.

I really don't think it was necessary to correct an example that had nothing to do with the original thread.  Not only is the topic 9 months old, but you corrected him for having an extra pair of '()'s
April 15, 2005, 10:30 PM

Search