delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/07/13/11:36:13

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Wed, 13 Jul 2005 08:35:52 -0700 (PDT)
Message-Id: <200507131535.j6DFZqwi005679@diometes.ucdavis.edu>
To: cygwin AT cygwin DOT com
Subject: How to create a .dll which contains a function defined in another program?
From: "Yu-Cheng Chou" <cycchou AT ucdavis DOT edu>
X-Errors-To: cycchou AT magenta DOT ucdavis DOT edu
X-User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
X-IsSubscribed: yes

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019