Valhalla Legends Forums Archive | Battle.net Bot Development | [C++] DebugOutputting [Solved]

AuthorMessageTime
bethra
I want to port the bot that I made in VB to C++ which I'm told is a whole lot faster.

First thing I need to do is make a packet debug so that I can see what mistakes I am doing and stuff.  I'm having trouble figuring out how to do so  since all the functions are different and ofcourse VB's are "friendlier".

I am constructing a function and kind of just doing something similar to Grok's DebugOutput VB function.

what C++ function(s) could be used like Hex()  and Asc() in VB?
October 23, 2004, 8:26 PM
shadypalm88
[quote author=bethra link=topic=9292.msg85809#msg85809 date=1098563213]
I am constructing a function and kind of just doing something similar to Grok's DebugOutput VB function.[/quote]I don't have anything that generates pretty output like Grok's, but this gets me by:[code]#include <cstdio>
// later on...
inline void dumpbuf(char* buf, int len) {
    for (int i = 0; i < len; i++) printf("%02X ", (buf & 0xFF));
    printf("\n\n");
}[/code]Where [i]buf
is the data you want to dump and len is its length.  (Duh.)

[quote author=bethra link=topic=9292.msg85809#msg85809 date=1098563213]what C++ function(s) could be used like Hex()  and Asc() in VB?[/quote]Asc, AFAIK, doesn't have an equivalent, because it's not needed.  For example, in VB, you might see some ugly stuff like this:[code]Asc(Mid(strStuff, 5, 1))[/code]
In C/C++, you could simply do:[code]strStuff[4][/code]
October 23, 2004, 8:56 PM
z-stars
The debug output function of my packet builder...

[code]
int BNETsPacket::dbgShowPacket()
{
char buf[20];
char * str;
str = new char [nPacketLen * 5 + 500];
memset(buf, 0, sizeof(buf));
memset(str, 0, nPacketLen * 5 + 500);
int j, i;
j = i = 0;

while(i < nPacketLen)
{
memset(buf, 0, sizeof(buf));
ultoa(packet[i], buf, 16);
strcat(str, buf);
strcat(str, ", ");
i++;
}

MessageBox(0, (LPCSTR)packet, "dbgShowTempPacket() Ascii", MB_OK);
MessageBox(0, str, "dbgShowTempPacket() Hex", MB_OK);
        delete [] str;

return 0;
}
[/code]
October 23, 2004, 10:39 PM
bethra
Thanks, shady and star, thanks.


Hey, I found this one function its in the header file "MAPIUTIL.H"

[quote]
void HexFromBin(
  LPBYTE pb, 
  int cb,     
  LPTSTR sz   
);

Parameters
pb
[in] Pointer to the binary data to be converted.
cb
[in] Size, in bytes, of the binary data pointed to by the pb parameter.
sz
[out] Pointer to a null-terminated ASCII string representing the binary data in hexadecimal digits.

Remarks
The HexFromBin function takes a pointer to a unit of binary data whose size is indicated by the cb parameter. It returns in the sz string, within (2*cb)+1 bytes of memory, a representation of this binary information in hexadecimal numbers. If the byte value is decimal 10, for example, the hexadecimal string will be 0A, so one byte converts to two bytes in the string.
[/quote]

I can't seem to get it to work though.

[code]
LPBYTE pb;
LPTSTR sz;
int cb;

pb = 'x';
cb = sizeof(pb);

HexFromBin(pb, cb, sz);
[/code]

can you help me please?
Yes, I know I'm noop. 

VB is really really really easier.... bleh no wonder I get laughed at when  I told my friend my bot was programmed in VB.  He told me it gives you bad habits.


October 23, 2004, 11:15 PM
z-stars
[quote author=bethra link=topic=9292.msg85827#msg85827 date=1098573332]
Thanks, shady and star, thanks.


Hey, I found this one function its in the header file "MAPIUTIL.H"

[quote]
void HexFromBin(
  LPBYTE pb, 
  int cb,     
  LPTSTR sz   
);

Parameters
pb
[in] Pointer to the binary data to be converted.
cb
[in] Size, in bytes, of the binary data pointed to by the pb parameter.
sz
[out] Pointer to a null-terminated ASCII string representing the binary data in hexadecimal digits.

Remarks
The HexFromBin function takes a pointer to a unit of binary data whose size is indicated by the cb parameter. It returns in the sz string, within (2*cb)+1 bytes of memory, a representation of this binary information in hexadecimal numbers. If the byte value is decimal 10, for example, the hexadecimal string will be 0A, so one byte converts to two bytes in the string.
[/quote]

I can't seem to get it to work though.

[code]
LPBYTE pb;
LPTSTR sz;
int cb;

pb = 'x';
cb = sizeof(pb);

HexFromBin(pb, cb, sz);
[/code]

can you help me please?
Yes, I know I'm noop. 

VB is really really really easier.... bleh no wonder I get laughed at when  I told my friend my bot was programmed in VB.  He told me it gives you bad habits.

[/quote]

I think that's what my function does.
October 23, 2004, 11:37 PM
shadypalm88
[quote author=bethra link=topic=9292.msg85827#msg85827 date=1098573332]
VB is really really really easier.... bleh no wonder I get laughed at when  I told my friend my bot was programmed in VB.  He told me it gives you bad habits.[/quote]Oh yeah.  VB can lead to bad habits when the person using it doesn't understand it correctly.  While it is easier, its easiness comes at the price of a worse understanding of what's going on "under the hood".
[quote author=bethra link=topic=9292.msg85827#msg85827 date=1098573332][quote author="MAPIUTIL.H"]
void HexFromBin(
  LPBYTE pb,   
  int cb,     
  LPTSTR sz   
);

Parameters
pb
[in] Pointer to the binary data to be converted.
cb
[in] Size, in bytes, of the binary data pointed to by the pb parameter.
sz
[out] Pointer to a null-terminated ASCII string representing the binary data in hexadecimal digits.[/quote]

I can't seem to get it to work though.

[code]
LPBYTE pb;
LPTSTR sz;
int cb;

pb = 'x';
cb = sizeof(pb);

HexFromBin(pb, cb, sz);
[/code][/quote]OK then, let's have a look at what you're doing.
LPBYTE pb; Allocates a pointer to some bytes (char's, really).
LPTSTR sz; Allocates a pointer to a string (char's with an extra "null" ('\0') byte on the end).

pb = 'x'; Here's the first spot you go awry.  pb is a pointer to BYTE(s), not an actual BYTE.
cb = sizeof(pb); Again, pb is a pointer.  You're actually taking the size of the pointer, not the byte.  It'll be 4, not 1.
HexFromBin(pb, cb, sz); Here is where VB's "bad habits" show through.  sz is the string that HexFromBin will place its output in.  In C/C++, you must first allocate the actual string, not just a pointer to it.  In VB you don't.

[quote author="MAPIUTIL.H"]... It returns in the sz string, within (2*cb)+1 bytes of memory ...[/quote]You must first make a string big enough to hold this.

This should work better.  (Off the top of my head, untested.)[code]LPBYTE pb;
LPTSTR sz;
int cb;

pb = "VB causes bad habits."
cb = strlen(pb);
sz = new TCHAR[cb * 2 + 1];
HexFromBin(pb, cb, sz);

// ... do something with sz ...

delete [] sz;
[/code]

Edit: Added the delete statement at the end.
October 23, 2004, 11:38 PM
bethra
I get two errors,

[code]
pb = "VB causes bad habits.";
[/code]
'=' : cannot convert from 'char [22]' to 'unsigned char *'

[code]
cb = strlen(pb);
[/code]
'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'

uh oh.  damn.  this ain't good.

the problem has to be with this part
[quote]
pb
[in] Pointer to the binary data to be converted.
[/quote]

binary data.  must I turn it to binary? or something?
October 24, 2004, 1:42 AM
shadypalm88
[quote author=bethra link=topic=9292.msg85838#msg85838 date=1098582170]
I get two errors
[/quote]
Sorry.  Windows programming isn't my forte.  Try:

[code]unsigned char example[] = "VB causes bad habits.";
LPTSTR sz;
int cb;

cb = strlen(pb);
sz = new TCHAR[cb * 2 + 1];
HexFromBin((LPBYTE) pb, cb, sz);

// ... do something with sz ...

delete [] sz;[/code]

P.S. If you get errors about strlen, add
[code]#include <cstring>[/code]to the top of the file.
October 24, 2004, 2:09 AM
bethra
okie the strlen still didn't work, even with #include <cstring>.
[quote]
'strlen' : cannot convert parameter 1 from 'unsigned char [22]' to 'const char *'
[/quote]

here is my simple main.cpp
[code]
#include <cstring>
#include <MAPIUTIL.H>

int main()
{
unsigned char example[] = "VB causes bad habits.";
LPTSTR sz;
int cb;

cb = strlen((const char*) example);
sz = new TCHAR[cb * 2 + 1];
HexFromBin((LPBYTE) example, cb, sz);

// ... do something with sz ...

delete [] sz;

return 0;
}
[/code]

since i was getting that strlen error thingy I tried that typcasting in it.  I didn't get that error anymore.  however I did get this other error which doesn't look good at all.

[quote]
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol _HexFromBin@12
Debug/main.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

main.exe - 2 error(s), 0 warning(s)
[/quote]

oh noes



well, thanks shady for all the help and time.  I just wanted to try and use this function cause it looked like it might be good to use in a debugoutput.  but it seems like it doesn't like me :).  thanks again.
October 24, 2004, 3:35 AM
shadypalm88
[quote author=bethra link=topic=9292.msg85859#msg85859 date=1098588905]
[quote author="Microsoft Visual C++"]
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol _HexFromBin@12
Debug/main.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

main.exe - 2 error(s), 0 warning(s)
[/quote]
[/quote]All this means is that you're not linking against the library that provides HexFromBin.  Remember, the header file only contains the function's prototype, not its code.  I don't know how to do this with the Visual C++ IDE as I don't use it.

[quote]'strlen' : cannot convert parameter 1 from 'unsigned char [22]' to 'const char *'[/quote]Interesting, gcc didn't complain about that.
October 24, 2004, 4:28 AM
bethra
[quote author=shadypalm88 link=topic=9292.msg85863#msg85863 date=1098592097]
[quote author=bethra link=topic=9292.msg85859#msg85859 date=1098588905]
[quote author="Microsoft Visual C++"]
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol _HexFromBin@12
Debug/main.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

main.exe - 2 error(s), 0 warning(s)
[/quote]
[/quote]All this means is that you're not linking against the library that provides HexFromBin.  Remember, the header file only contains the function's prototype, not its code.  I don't know how to do this with the Visual C++ IDE as I don't use it.
[/quote]

When you said this, I instantly remember this C++ winsock tutorial that I had  read awhile back.  It told me to add "WS2_32.LIB" library to the project,  by going to Project/Settings/Link!!!!!

So I googled and looked for the library that HexFromBin comes from and I found it as "MAPI32.LIB".

I added it to the project,  and presto!!!  not linkage error!!!

yay, thanks alot shady!


[quote]'strlen' : cannot convert parameter 1 from 'unsigned char [22]' to 'const char *'[/quote]Interesting, gcc didn't complain about that.
[quote][/quote]

hmmmmm gcc?

[code]
cb = strlen((const char*)example);
[/code]

dunno, but do you think what I did, typing casting it will work properly?

yay!

NOTE:  Now I have to see if it works =D.
October 24, 2004, 2:52 PM
Zakath
There is a far, far easier way to accomplish this.

wsprintf( LPTSTR out, "%#x", int in )
October 30, 2004, 7:51 PM

Search