• Welcome to Valhalla Legends Archive.
 

[C++] DebugOutputting [Solved]

Started by Sorc.Polgara, October 23, 2004, 03:26 PM

Previous topic - Next topic

Sorc.Polgara

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?

shadypalm88

Quote from: bethra on October 23, 2004, 03:26 PM
I am constructing a function and kind of just doing something similar to Grok's DebugOutput VB function.
I don't have anything that generates pretty output like Grok's, but this gets me by:#include <cstdio>
// later on...
inline void dumpbuf(char* buf, int len) {
    for (int i = 0; i < len; i++) printf("%02X ", (buf[i] & 0xFF));
    printf("\n\n");
}
Where buf is the data you want to dump and len is its length.  (Duh.)

Quote from: bethra on October 23, 2004, 03:26 PMwhat C++ function(s) could be used like Hex()  and Asc() in VB?
Asc, AFAIK, doesn't have an equivalent, because it's not needed.  For example, in VB, you might see some ugly stuff like this:Asc(Mid(strStuff, 5, 1))
In C/C++, you could simply do:strStuff[4]

z-stars

#2
The debug output function of my packet builder...


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;
}

Sorc.Polgara

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

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


LPBYTE pb;
LPTSTR sz;
int cb;

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

HexFromBin(pb, cb, sz);


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.



z-stars

Quote from: bethra on October 23, 2004, 06:15 PM
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.

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


LPBYTE pb;
LPTSTR sz;
int cb;

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

HexFromBin(pb, cb, sz);


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.


I think that's what my function does.

shadypalm88

#5
Quote from: bethra on October 23, 2004, 06:15 PM
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.
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 from: bethra on October 23, 2004, 06:15 PM
Quote from: 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.

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


LPBYTE pb;
LPTSTR sz;
int cb;

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

HexFromBin(pb, cb, sz);
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 from: MAPIUTIL.H... It returns in the sz string, within (2*cb)+1 bytes of memory ...
You must first make a string big enough to hold this.

This should work better.  (Off the top of my head, untested.)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;


Edit: Added the delete statement at the end.

Sorc.Polgara

#6
I get two errors,


pb = "VB causes bad habits.";

'=' : cannot convert from 'char [22]' to 'unsigned char *'


cb = strlen(pb);

'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.

binary data.  must I turn it to binary? or something?

shadypalm88

#7
Quote from: bethra on October 23, 2004, 08:42 PM
I get two errors
Sorry.  Windows programming isn't my forte.  Try:

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;


P.S. If you get errors about strlen, add
#include <cstring>to the top of the file.

Sorc.Polgara

#8
okie the strlen still didn't work, even with #include <cstring>.
Quote
'strlen' : cannot convert parameter 1 from 'unsigned char [22]' to 'const char *'

here is my simple main.cpp

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


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)

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.

shadypalm88

Quote from: bethra on October 23, 2004, 10:35 PM
Quote from: 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)
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 *'
Interesting, gcc didn't complain about that.

Sorc.Polgara

#10
Quote from: shadypalm88 on October 23, 2004, 11:28 PM
Quote from: bethra on October 23, 2004, 10:35 PM
Quote from: 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)
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.

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 *'
Interesting, gcc didn't complain about that.
Quote

hmmmmm gcc?


cb = strlen((const char*)example);


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.

Zakath

There is a far, far easier way to accomplish this.

wsprintf( LPTSTR out, "%#x", int in )
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.