Thread: Question about using memcmp ()

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Question about using memcmp ()

    Hi,

    I do try to write a function like this one:
    This is only a sample - I don't post the whole code, because it will be useless info:

    Code:
    BYTE *bBuffer = 0;
    
    bBuffer = (LPBYTE)GlobalAlloc(GMEM_FIXED, ModInfo.SizeOfImage+1);//allocating of memory
    
    ReadProcessMemory(hProcess, ModInfo.lpBaseOfDll, bBuffer, ModInfo.SizeOfImage, &cbRead);
    
    for( i = 0; i < ModInfo.SizeOfImage; i++)
    if (!memcmp(&bBuffer[i], bPattern, 10))// we have a match
     {	
    								
    // we calculate lpBaseOfDll (beginning addr. of executable module +  i)
    
    DWORD dwPatchAddress = (DWORD)ModInfo.lpBaseOfDll + i;
    // we replace
    WriteProcessMemory(hProcess, (LPVOID)dwPatchAddress, LPVOID(szReplaceBytes), 10, &cbRead);
    CloseHandle(hProcess);
    MessageBox(NULL,"Done!","OK",MB_OK|MB_ICONINFORMATION);
    break;
    }
    Normally I need to include a condition if there is no match with bPattern : MessageBox(something) -> break;
    but when I write such - I get it always even if there is a match with bPattern and I don't understand why..
    Does anyone of you have an idea how to make it work...?
    Last edited by Vortex; 06-03-2006 at 02:59 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean that this
    Code:
    if (!memcmp(&bBuffer[i], bPattern, 10))// we have a match
    is always true?

    [edit]
    Perhaps the data is always the same, because you seem to be using memcmp() correctly: http://www.cprogramming.com/fod/memcmp.html
    [/edit]
    Last edited by dwks; 06-03-2006 at 03:06 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Hi dwks,
    I am not sure what you mean - the way it is written, this code always does do the job...

    Code:
    if (!memcmp(&bBuffer[i], bPattern, 10))// we have a match
    of course it could be written like this:
    Code:
    int result;
    result=(memcmp(&bBuffer[i], bPattern, 10));
    if (result==0) //which means that we have a match here
    {
    (...)
    }
    if (result >0 || result <0) //there is no match 
    {
    (...)
    }
    The problem is that condition number 2 is what I run into even if there is a match.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The problem is that condition number 2 is what I run into even if there is a match.
    Well then you must not be passing memcmp() the right values. (Either that or there wasn't really a match. Perhaps there is a discreprency in the first 10 bytes of data?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Well then you must not be passing memcmp() the right values. (Either that or there wasn't really a match. Perhaps there is a discreprency in the first 10 bytes of data?)
    Ah, well
    I suppose that my problems start somewhere here:

    Code:
    for( i = 0; i < ModInfo.SizeOfImage; i++)
    while counting ...condition number 2 is always true, unless the address where the bytes do match is reached... So I need maybe to extend it somehow... Many thanks for your replies.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why don't you print (or pop up a message box with) the values of the data before you compare them? Then you can see if they really match.

    Or just fire up your debugger.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM