注册 登录
自由的生活_软路由 返回首页

的个人空间 https://www.routerclub.com/?0 [收藏] [复制] [分享] [RSS]

日志

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

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2025-7-12 15:20 , Processed in 0.088166 second(s), 14 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

返回顶部