How to create a .dll which contains a function defined in another program?

Yu-Cheng Chou cycchou@ucdavis.edu
Wed Jul 13 15:36:00 GMT 2005


Hi,

bar() is in the main.c which is compiled using VC++.
bar() is called inside module.c which is compiled as module.dll using 
cygwin's gcc. 
module.dll is loaded by LoadLibrary() in main.exe.

/* main.c */
#include <stdio.h>
#include <windows.h>

extern "C" int mainCRTStartup();

extern "C" int __stdcall 
cygloadCRTStartup() {
    char padding[4096];
    return mainCRTStartup();
}

__declspec(dllexport)
int bar() {
    printf("bar() is called\n");
    return 0;
}

int main() {   
    char *modname = "module.dll";  
    HMODULE h;
    HMODULE handle;
    void (*init)();
    int (*fp)(int);
    int ret;

    h = LoadLibrary("cygwin1.dll");
    init = (void (*)())GetProcAddress(h, "cygwin_dll_init");
    init();
    handle = LoadLibrary(modname);
    fp = (int (*)(int))GetProcAddress(handle, "foo");
    ret = fp(125);
    printf("ret = %d\n", ret);
    return 0;
}

/* module.c */
#include <stdio.h>

extern __declspec(dllimport)
int bar();

__declspec(dllexport)
int foo(int arg){   
   printf("foo() is called in main.exe\n");
   printf("arg * 2 = %d\n", arg * 2);
   printf("\n");
   bar();
   return arg * 2;
}

Thanks


--
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/



More information about the Cygwin mailing list