• Welcome to Valhalla Legends Archive.
 

Porting C++ to VB

Started by ChR0NiC, July 11, 2004, 11:20 PM

Previous topic - Next topic

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

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


Ported to VB

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


Original C++ 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);
}


Ported to VB 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


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.

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)

UserLoser.

#1
Are you allocating enough space for Outbuf in your AccountCreate function?

ChR0NiC

Quote from: UserLoser. on July 12, 2004, 01:58 AM
Are you allocating enough space for Outbuf in your AccountCreate function?

Hmmmm, do you mean the function I am using to call the CreateAccount function shown here?

Lenny

#3
Yes, you need to pre allocate space for strings before using CopyMemory.

Outbuf = Space(20)
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

hismajesty

#4
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*

Clokr_

Isnt a C++ DWORD a long in VB? (4 bytes)

Banana fanna fo fanna

instead of dword[5], try doing a string of length 20.

Clokr_

#7
    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

MyndFyre

Quote from: Clokr_ on August 10, 2004, 09:25 AM
Isnt a C++ DWORD a long in VB? (4 bytes)

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:


For i = 0 To 10
 MsgBox( i )
Next


That ensures proper fixed-width formatting and preserves whitespace.

Enjoy your stay :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Grok

Quote from: MyndFyre on August 10, 2004, 07:45 PM
Quote from: Clokr_ on August 10, 2004, 09:25 AM
Isnt a C++ DWORD a long in VB? (4 bytes)

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.  ;)

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.

Clokr_

Quote from: MyndFyre on August 10, 2004, 07:45 PM
Quote from: Clokr_ on August 10, 2004, 09:25 AM
Isnt a C++ DWORD a long in VB? (4 bytes)

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:


For i = 0 To 10
 MsgBox( i )
Next


That ensures proper fixed-width formatting and preserves whitespace.

Enjoy your stay :)

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.

Adron

Quote from: Clokr_ on August 11, 2004, 02:30 PM
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.

So... You're a newbie and a llama eh?

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
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.