Yu-Cheng Chou wrote: > int main(){ > void *handle; > int (*fp)(); > char *modname = "./module.dll"; > HMODULE h; > void (*init)(); > printf("hello1\n"); > h = LoadLibrary("cygwin1.dll"); > printf("hello1 h = %p\n", h); > init = ( void (*)())GetProcAddress(h, "cygwin_dll_init"); > printf("init = %p\n", init); > init(); // CRASH HERE.......!!!
Of course it crashes, you aren't doing anything to address the fact that the bottom CYGTLS_PADSIZE bytes of the stack will be clobbered by Cygwin's tls structure. This is mentioned in the FAQ and explained further in how-cygtls-works.txt and the cygload sample. In a Cygwin program, this is handled for you automatically by the startup code, but not so if your program is compiled with VC++ and uses its runtime. This means you have to add the necessary scratch space to the stack, as low on the stack as possible after the entry to main(). But the arguments to main() as well as its return location will already be on the stack (as well as whatever arbitrary values the MSVCRT chooses to put there) which means they will be clobbered as well. You can work around this by making copies of the arguments and not returning from main(). Or you can save the state of the stack pre-clobbering and then restore it before returning from main(), as cygload does. > I followed the instructions from FAQ to load a shared library. > But the program main.exe crashed at the line init() highlighted in the > main.c > > How can I fix the problem? Go back and read the FAQ again and make sure you understand it all. Brian -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/