Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Next problem with C++ and appending to sprintf

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Thu Aug 14, 2008 3:14 am    Post subject: Next problem with C++ and appending to sprintf Reply with quote

Hello

okay this my next problem I got it deals with a little edit box I got in c++ where I copy packets from and edit up before sending

thing is it gets filled up sometimes wrong..

packet it self holds a ton of ASCII characters as char* packet;
thing is I convert the ASCII characters by size of packet into HEX and append em to Dialog edit box.

here is my current code that works.. for little packets but starts to mess up... on size 30 packets or so by putting a little X in front of packet for no reason!

Code:


   if(showLastPacket) {
      char buf[MAX_PATH];
      int i;
      for(i=0;i<size;i++)
         sprintf(strchr(buf, 0), "%02X ",BYTE(packet[i]));
      SetWindowTextA(GetDlgItem(DlgHWND, IDC_LASTPACKET),buf);
   }



It's obvious right away don't put out the obvious that MAX_PATH = 256 or 255 and if a packet is longer then that then application crashes.. and it does.. but thats not the problem

most packets are 20-50 in size big packets is if i spam in chat
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

etc...

anyways yah..

props if anyone helps I'll rep yah
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Aug 14, 2008 4:11 am    Post subject: Reply with quote

Firstly, MAX_PATH is not the fall back for all sizes when it comes to initializing variables lol. Instead, you could dynamically initialize the buffer based on the size of the packet. If you are bound to just using multibyte you can do something like this:

Code:

   char* buf = new char[ strlen(packet)+1 ];
   char byte[2] = {0};
   for( int i=0; i<strlen(packet); i++ )
   {
      sprintf( byte, "0x%02X ", packet[i] );
      strcat( buf, byte );
   }
   SetWindowTextA(GetDlgItem(DlgHWND, IDC_LASTPACKET),buf)
   delete buf;

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Thu Aug 14, 2008 5:52 am    Post subject: Reply with quote

lol sweet your a pro.. I totally forgot about strcat

also I wanted to ask you about GetDlgItem.. it works all good but DlgGWND is a global int i saved.. after the CALLBACK hooks it's value in INITDIALOG or somethin is it possible to get HWND


Code:



BOOL CALLBACK DlgThread(HWND hDlg,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   switch(uMsg)
   {
   case WM_INITDIALOG:
      {
         //save the hwnd to this to use buttons,textbox's outside the DlgThread.
         DlgHWND = hDlg;
         return TRUE;
      }

blah blah
}


is it possible to get HWND without doing that little global thing?

I know it works like this but I want to know if there is a cleaner way.

and should I use size or strlen?? i heard strlen can get wrong size if it finds a zero in the middle of packet.

Code:


   if(showLastPacket) {
      char* buf = new char[ /*strlen(packet)+1*/ size ];
      char byte[2] = {0};
      for( int i=0; i<size/*strlen(packet)*/; i++ )
      {
        sprintf( byte, "0x%02X ", packet[i] );
        strcat( buf, byte );
      }
      SetWindowTextA(GetDlgItem(DlgHWND, IDC_LASTPACKET),buf)
      delete buf;
   }



Hehe thanks again man!!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Aug 14, 2008 7:45 am    Post subject: Reply with quote

If you directly have the packet size in a variable, just use that.

Other methods of getting the hWnd would be using FindWindow with FindWindowEx, it is basically cleaner to do what you did with it being global like that.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Thu Aug 14, 2008 8:48 pm    Post subject: Reply with quote

solved

Last edited by pkedpker on Mon Aug 18, 2008 12:40 am; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Aug 14, 2008 9:59 pm    Post subject: Reply with quote

Try setting the buffer data to 0 before using it, you can use memset to do that:

Code:
char* buf = new char[(size*2)+1];
memset( buf, 0x0, (size*2)+1 );

// .. other code

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
jackyyll
Expert Cheater
Reputation: 0

Joined: 28 Jan 2008
Posts: 143
Location: here

PostPosted: Thu Aug 14, 2008 10:08 pm    Post subject: Reply with quote

pkedpker wrote:
Cool somewhat unexcepted results in front.. also it does crash the program for some reason I believe the crash has something with the

delete buf;





using

Code:


   if(showLastPacket) {
      char* buf = new char[size+1];
      char byte[2] = {0};
      char byte2[3] = {0};
      for(int i=0; i<size; i++)
      {
         if(i+1==size) { //remove last space.
         sprintf(byte, "%02X", packet[i]);
         strcat(buf, byte);
         } else {
         sprintf(byte2, "%02X ", packet[i]);
         strcat(buf, byte2);
         }
      }
 //strcpy(buf+1, (buf)-1);
      SetWindowTextA(GetDlgItem(DlgHWND, IDC_LASTPACKET),buf);
      delete buf;
   }


nvm maybe i solved it?? because cuz the initlization of char* shoulda been size+1 im used to not using +1 cuz of vb6 byte arrays forgot char* has a /0 at end.

And also the sprintf uses a extra space which takes 3 bytes?

no still crashes


new version using strncat for (3 or 2) space appender

ALSO finally found bug.. haha i was starring at code for 2 mins and figured out.. NO WAY!! buff has to whole spaces too!! so i fixed a new size for dynamic allocation with space calculator


Lol still crashes! omg..

Code:


   if(showLastPacket) {
      char* buf = new char[(size*2)+1]; //size-1 for number of spaces in between doh!!!
      char byte[4] = {0};


      for(int i=0; i<size; i++)
      {
Sleep(5000);
         printf("i is %d\n", i);
         if(i+1==size) { //remove last space.
printf("final line %d size\n", size);
         sprintf(byte, "%02X", packet[i]);
         strncat(buf, byte, 2);
printf("DONE\n");
         } else {
         sprintf(byte, "%02X ", packet[i]);
         strncat(buf, byte, 3);
         }
printf("current: %s\n", buf)
         //strcpy(buf+1, (buf)-1);
      }
Sleep(3000);
printf("print results \n");
      SetWindowTextA(GetDlgItem(DlgHWND, IDC_LASTPACKET),buf);
      delete buf;
   }


turns out its a stupid buffer problem..!!





tried

delete[] buf

doesn't help either


To get rid of the extra shit do packet[i] & 0xFF
Back to top
View user's profile Send private message AIM Address MSN Messenger
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Thu Aug 14, 2008 10:11 pm    Post subject: Reply with quote

doesn't show up crap anymore but still crashes.. I think the size of buffer still not big enough

game caused an Access Violation (0xc0000005)
in module Hack.dll at 001b:0a7e5170.


yah thanks I did

BYTE(packet[i])

i forgot it got removed when Wiccaan posted his method I just replaced it and forgot but still crashes..
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Aug 14, 2008 10:14 pm    Post subject: Reply with quote

Probably the size still then, how is the packet setup when you are reading it?
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Thu Aug 14, 2008 10:15 pm    Post subject: Reply with quote

solved

Last edited by pkedpker on Mon Aug 18, 2008 12:40 am; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Aug 14, 2008 10:22 pm    Post subject: Reply with quote

Can you paste a packet that is causing a crash by chance?
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Fri Aug 15, 2008 12:44 am    Post subject: Reply with quote

OMFG im so stupid 2 hours working on it and i finally figured out

D6 = 2 bytes not 1!!!!!! FFS....... thats what happpens when you know what hex means you think its 1 byte but you never think about ASCII..

so size = 1 byte but its really 2 bytes when translated to ASCII + a space.

so the real algorithum is like

AA BB DD = (SIZE (3)*2 = 6) + (size-1 (SPACES)) +1 for /0 null terminiator

(((size*2)+(size-1))+1)



but since the size-1+1 -1+1 cancels out so its.. really

((size*2)+size)
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Aug 15, 2008 6:42 am    Post subject: Reply with quote

pkedpker wrote:
OMFG im so stupid 2 hours working on it and i finally figured out

D6 = 2 bytes not 1!!!!!! FFS....... thats what happpens when you know what hex means you think its 1 byte but you never think about ASCII..

so size = 1 byte but its really 2 bytes when translated to ASCII + a space.

so the real algorithum is like

AA BB DD = (SIZE (3)*2 = 6) + (size-1 (SPACES)) +1 for /0 null terminiator

(((size*2)+(size-1))+1)



but since the size-1+1 -1+1 cancels out so its.. really

((size*2)+size)


Heh, thats why I requested what the packets looked like, cause your size looked wrong to start with. Just wanted to make sure the game didn't set things up differently itself. :s

Glad to hear its working. Smile

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
pkedpker
Master Cheater
Reputation: 1

Joined: 11 Oct 2006
Posts: 412

PostPosted: Fri Aug 15, 2008 6:33 pm    Post subject: Reply with quote

haha yup now i got another problem.. I tried to hook the recv decrypt route but I only know assembly 1 week now so I know nothing.. but jmps and mov's and pop's and push's thats about it.

Anyways I found where packet is decrypted blah i'll make a new topic .. I wanna learn this anyways.. for future i have problem with retn's going to different place after I hooked it really assembly like to give you problems like that Mad.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites