Click to See Complete Forum and Search --> : why this clone function doesn't work?


smallwolf
May 21st, 2010, 10:03 PM
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



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

cosmicvoid
May 22nd, 2010, 12:22 AM
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?

VictorN
May 23rd, 2010, 04:52 AM
You also must check the return value of GetProcAddress (and of GetModuleHandle too!)

smallwolf
May 23rd, 2010, 11:01 AM
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..... :)