delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/02/12/19:21:17

From: zow AT mdbs DOT com (Zow Terry Brugger)
Subject: RE: Runtime GNU-DLL with MSVC
12 Feb 1998 19:21:17 -0800 :
Message-ID: <01BD37A5.CBC356B0.zow.cygnus.gnu-win32@mdbs.com>
Reply-To: "zow AT mdbs DOT com" <zow AT mdbs DOT com>
Mime-Version: 1.0
To: "'ISGStuttgart'" <ISGStuttgart AT compuserve DOT com>,
GNU-Win32
<gnu-win32 AT cygnus DOT com>

There is a binary incompatibility between MSVC 4.2 & 5.0 . This bit the 
last project I was on a couple times. Hence my question: in the FAQ it 
states that MSVC & gcc objects can be mixed (I know that's not exactly 
what's happening here, but it's close); Was this determined with MSVC 4.2 ? 
I saw this problem before using an executable built with 4.2 and a DLL from 
5.0. What it amounted to was that the two use different calling conventions 
for DLLs, so the executable would think that the function in the DLL 
cleaned up the stack but the DLL thought the caller would do it, or maybe 
it was the other way around. In any case, the solution was to build 
EVERYTHING with MSVC 5.0 . Not elegant, but then again I don't think having 
that kind of binary incompatibility was either.

Hope this helps!
Terry

-----Original Message-----
From:	ISGStuttgart
Sent:	Wednesday, February 11, 1998 8:26 AM
To:	GNU-Win32
Subject:	Runtime GNU-DLL with MSVC

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 AT 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 AT 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 AT 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 AT 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
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


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