Subject: Runtime GNU-DLL with MSVC
From: [email-masked] (ISGStuttgart)

11 Feb 1998 11:42:26 -0800
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
To: GNU-Win32 <[email-masked]>

Hallo,

we have the following problem:
We want to use a DLL (generated with gnu-win32) with a program generated
with MSVC 5.0.
We can load the library and we can get the ProcAdress of the function in
the DLL. By calling the function the program crashes. Compiling the same
program with gnu-win32 the program works fine. We tried all byte
allignments in MSVC, but nothing happens.

Here's what we've done:

--------------------------------------------------------------------------
// ***** File foo.c *****
--------------------------------------------------------------------------

// Test file to check out building DLLs with gnuwin32
// This uses printf from the std lib

#include <stdio.h>

//* function declarations: ***
int doit (int i);

int doit (int i)
{
  printf("In foo.c inside of doit with printf\n"); 
  return( 4 );
}

--------------------------------------------------------------------------
// ***** File init.c *****
--------------------------------------------------------------------------

#include <windows.h>

extern int WINAPI dll_entry (HANDLE h, DWORD reason, void *ptr);

int WINAPI dll_entry (HANDLE h,
DWORD reason,
void *ptr)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}

--------------------------------------------------------------------------
//***** file fixup.c ******
--------------------------------------------------------------------------

/* This is needed to terminate the list of import stuff */
/* Copied from winsup/dcrt0.cc in the cygwin32 source distribution. */
asm(".section .idata$3\n" ".long 0,0,0,0, 0,0,0,0");

--------------------------------------------------------------------------
// **** File foo.def ****
--------------------------------------------------------------------------

EXPORTS
        doit            @1
        dll_entry@12    @2

--------------------------------------------------------------------------
// Main file to try linking with a DLL under gnuwin32
--------------------------------------------------------------------------

// Updated to load dll at runtime (as opposed to loadtime), using
// win32 dll load functions
#include <stdio.h>
#include <windows.h>

int (*doitptr)(int); // pointer to the doit routine

int
main()
{

char filename[]="foo.dll";
HINSTANCE dllhandle; // handle to the dll

// Load the dll into memory:
if( (dllhandle = LoadLibraryExA(filename, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH )) 
== NULL){
printf("Error, Could not load %s\n",filename);
exit(0);
}

doitptr = GetProcAddress(dllhandle, "doit");
if( doitptr == NULL){
printf("Error, Could not load doit symbol\n"); 
} 
printf("doit(5) returns %d\n", (*doitptr)(5));

}

--------------------------------------------------------------------------
#Compile and Link Sequence for foo.dll
--------------------------------------------------------------------------

gcc -g -c foo.c
gcc -c init.cc
gcc -c fixup.c

# Link DLL.
ld --base-file foo.base --dll -o foo.dll foo.o init.o fixup.o \
$LIBPATH/libcygwin.a -e _dll_entry@12 
dlltool --as=as --dllname foo.dll --def foo.def --base-file foo.base
--output-exp foo.exp
ld --base-file foo.base foo.exp --dll -o foo.dll foo.o init.o fixup.o \
$LIBPATH/libcygwin.a -e _dll_entry@12 
dlltool --as=as --dllname foo.dll --def foo.def --base-file foo.base
--output-exp foo.exp
ld foo.exp --dll -o foo.dll foo.o init.o fixup.o\
$LIBPATH/libcygwin.a -e _dll_entry@12 

--------------------------------------------------------------------------
# Generating main
--------------------------------------------------------------------------
gcc -g main.c -o main.exe

Gerd from ISGStuttgart
-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".