CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2008
    Location
    San Francisco, USA
    Posts
    24

    why this clone function doesn't work?

    hello:
    i want to test to call a function in a different way, but failed.....
    the program compiled and run, it listed the files...and then the process crashed with errors...

    what's wrong with my code:

    using wxDevC++, Windows xp sp3

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main(int argc, char *argv[])
    {  
        
        
       typedef BOOL (*FINDCLOSE)(HANDLE);
        FINDCLOSE fc=(FINDCLOSE)GetProcAddress(GetModuleHandle("Kernel32"),"FindClose");
        
         void* oldProc=fc;
        BYTE* vfc=(BYTE*)oldProc;
        
        BYTE* buffer=malloc(170);  // allocate enough memory to hold the code
        memcpy(buffer,vfc,168);
        void* orif=buffer;
        PROC findc=(PROC)orif;
        
       
        
        WIN32_FIND_DATA find_data;
       HANDLE find_handle;
    
       find_handle = FindFirstFile("C:\\*.*", &find_data );
    
       if( find_handle != INVALID_HANDLE_VALUE )
      {
       /// list files in this directory
      do
         {
          printf(find_data.cFileName);
          printf("\n");
         }while( FindNextFile( find_handle, &find_data ) );
    
        findc( find_handle );      // i replace the FindClose()
       }  
        system("PAUSE");	
        return 0;
    }

  2. #2
    Join Date
    Apr 2010
    Location
    Western WA, USA
    Posts
    59

    Re: why this clone function doesn't work?

    Why do you think 168 bytes is enough to hold a copy of the FindClose() function?

    How do you know that FindClose() does not call other code that it expects to be within its memory space, but outside of the 168 bytes that you copy?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: why this clone function doesn't work?

    You also must check the return value of GetProcAddress (and of GetModuleHandle too!)
    Victor Nijegorodov

  4. #4
    Join Date
    Jun 2008
    Location
    San Francisco, USA
    Posts
    24

    Re: why this clone function doesn't work?

    thanks for your reply!
    maybe it doesn't work in that way! i just want to try some ideas, it's a good learning experience.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured