msvc调用使用cygwin编译的共享库dll
已有 178 次阅读2007-12-16 20:56
/* main.c */
#include <stdio.h>
#include <windows.h>
extern "C" int mainCRTStartup();
extern "C" int __stdcall
cygloadCRTStartup() {
char padding[4096];
return mainCRTStartup();
}
int main() {
char *modname = "module.dll";
HMODULE h;
HMODULE handle;
void (*init)();
int (*fp)(int);
int ret;
printf("1\n");
h = LoadLibrary("cygwin1.dll");
printf("h = %p\n", h);
init = (void (*)())GetProcAddress(h, "cygwin_dll_init");
printf("init = %p\n", init);
init(); // after this, I/O will be redirected
printf("2\n");
handle = LoadLibrary(modname);
if(handle == NULL) {
fprintf(stderr, "Can't load %s in LoadLibrary()\n", modname);
exit(1);
}
printf("handle = %p\n", handle);
fp = (int (*)(int))GetProcAddress(handle, "foo");
if(fp== NULL) {
fprintf(stderr, "ERROR: GetProcAddress()\n");
exit(1);
}
printf("fp = %p\n", fp);
ret = fp(100);
printf("ret = %d\n", ret);
return 0;
}
/* module.c */
#include <stdio.h>
int foo(int arg){
printf("foo() is called in main.exe\n");
printf("arg * 2 = %d\n", arg * 2);
return arg * 2;
}
Here is the output from MS command prompt
C:\users\ycchou\VC1>main.exe
1
h = 61000000
init = 61005790
foo() is called in main.exe
arg * 2 = 200
Thanks for anyone's help