delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/07/05/15:12:25

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: Tue, 5 Jul 2005 12:11:53 -0700 (PDT)
Message-Id: <200507051911.j65JBrn4005874@citheronia.ucdavis.edu>
To: cygwin AT cygwin DOT com
Subject: Load a shared library using gcc/Cygwin
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,

main.c below is for loading the shared library module.dll
/* command to create main.exe
             cl -c main.c
             link main.obj
*/
#include <stdio.h>
#include <windows.h>

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.......!!!

printf("hello2\n");
   handle = LoadLibrary(modname);
printf("hello2 handle  = %p\n", handle);
   if (handle == NULL) {
      fprintf(stderr, "Can't load %s in LoadLibrary()\n", modname);
      exit(1);
   }
   fp = ( int (*)())GetProcAddress(handle, "foo");
   if (fp== NULL) {
      fprintf(stderr, "ERROR: GetProcAddress()\n");
      exit(1);
   }

   printf("ok1\n");
   fp();
   printf("ok2\n");
   FreeLibrary(handle);
}

module.c below is the source code for creating module.dll
/* command to create
     gcc -shared -o module.dll  module.c
*/
#include <stdio.h>
#include <windows.h>

/*
__declspec(dllexport)
*/
int foo(int arg){
   printf("foo() called\n");
   return (arg * 2);
}

Makefile is for creating main.exe and module.dll
CC = cl
LINK = link
#CFLAGS = /MD
CFLAGS =

all: main.exe module.dll
main.exe: main.obj
        $(LINK) main.obj
main.obj: main.c
        $(CC) $(CFLAGS) -c main.c
module.dll: module.o
        gcc -shared -o module.dll  module.c
#gcc -shared -o module.dll  module.o
module.o: module.c
        gcc -c module.c
clean:
        rm *.obj *.dll *.exe *.lib *.exp *.o main.exe.stackdump


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?

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