Subject: dll's and LoadLibrary on NT and Win95
From: [email-masked] (Rolf Wester)

4 May 1997 07:37:51 -0700
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: Multipart/Mixed; boundary=Message-Boundary-16975
Comments: Authenticated sender is <[email-masked]>
Original-To: [email-masked]
X-mailer: Pegasus Mail for Win32 (v2.52)
X-Sender: [email-masked] (Rolf Wester)
Original-Sender: [email-masked]

--Message-Boundary-16975
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body

Hi,

I tried to make a dll and an *.exe file which dynamically loads this 
dll. I made this according to Ismael Jurado's suggestions on a 
WindowsNT and a Windows95 system. All works well on the NT system but 
not on the Win95 system. I tried combinations of the dll and the exe 
made on the NT and the Win95 system respectively. The WinNT and Win95 
exe's work with the NT dll but both exe's do not work with the Win95 
dll. The sources, the shell scripts for making the executables and 
the results are in the file attached to this mail. 

The problem with dynamically loading dll's not only occured with the 
I. Jurados example but it also was present when trying to use 
c/c++ code from a java class via the Java Native Interface. I also 
want to try using Tcl/Tk and c/c++ together and there the same 
problem could occur.

I would be very appreciative for any help on this problem.
Thank you anticipation.

With kind regards  

--Rolf

[email-masked]

--Message-Boundary-16975
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Text from file 'mail.txt'

This file contains the sources for 
   hello.dll		: the dll used by App.exe and loadLib.exe
   App.exe			: linked against import library hello.a
   Loadlib.exe		: loads hello.dll dynamically
and the shell scripts for making the three executables.

Also included in this file are the sequences of 
MessagesBoxes popping up when running LoadLib.exe.
 
-----------------------------------------------------------
-----------------------------------------------------------
/* source for hello.dll */

#include <stdio.h>
#include <windows.h>

BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{

  switch (dwReason) 
    {
    case DLL_PROCESS_ATTACH:
        MessageBox (NULL, "PROCESS_ATTACH", "Dll", MB_OK);
      break;
    case DLL_PROCESS_DETACH:
        MessageBox (NULL, "PROCESS_DETACH", "Dll", MB_OK);
      break;
    case DLL_THREAD_ATTACH:
        MessageBox (NULL, "THREAD_ATTACH", "Dll", MB_OK);
      break;
    case DLL_THREAD_DETACH:
        MessageBox (NULL, "THREAD_DETACH", "Dll", MB_OK);
      break;
    }
    return TRUE;
}

void  foof() 
{
    MessageBox(NULL, "Hello from foof", "DLL", MB_OK);
}

#ifdef	__GNUC__
	asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");
#endif
/* end dll */

-----------------------------------------------------------
# shell script for making the hello.dll 
gcc -c Dll.c -o dll.o

echo EXPORTS > hello.def
#nm <libnames> | grep " [TC] " | sed '/ _/s//  /' | awk '{print $3;}' >> hello.def
nm dll.o | grep '^........ [T] _' | sed 's/[^_]*_//' >>  hello.def
   
ld --dll --subsystem windows -e _DllMain@12 -o jnk --base-file hello.base dll.o /cygnus/win32/lib/libuser32.a
dlltool --dllname hello.dll --base-file hello.base --def hello.def --output-lib hello.a --output-exp hello.exp
ld --dll --subsystem windows -e _DllMain@12 -o hello.dll dll.o /cygnus/win32/lib/libuser32.a hello.exp
rm jnk hello.base hello.exp

-----------------------------------------------------------
-----------------------------------------------------------
/* app.ec */
#include <windows.h>

typedef void (*foop)();
void foof();

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow) 
{
    foof();
    return 0;
};
/* end app.c */

-----------------------------------------------------------
#shell script for making app.exe
gcc -c app.c 
#gcc -o appa appa.o -mwindows --subsystem windows
gcc -o app app.o hello.a -mwindows #--subsystem windows

-----------------------------------------------------------
-----------------------------------------------------------
/* LoadLib.c */
#include <windows.h>
//#include <win32/functions.h>
#include <stdio.h>

typedef void (*foop)();
void foof();

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow) 
{
	foop foo;
    char buf[126];
	DWORD erg;

    HANDLE dllh;
    
	erg=1;

    MessageBox(NULL, "Start", "WinMain", MB_OK);
    dllh=LoadLibrary("hello.dll");
	erg=GetLastError();
    sprintf(buf,"Last Error : %d", erg);
    MessageBox(NULL, buf, "WinMain", MB_OK);

    if ((UINT)dllh <= HINSTANCE_ERROR)
    {
        sprintf(buf,"Error loading dll: %d", dllh);
        MessageBox(NULL, buf, "WinMain", MB_OK);
    }
    else 
    {
        foo=(foop)GetProcAddress(dllh, "foof");
        if (foo==NULL)
            MessageBox(NULL, "foo == null", "APP", MB_OK);
        else
            foo();
    }
    erg=GetLastError();
    sprintf(buf,"Last Error : %d", erg);
    MessageBox(NULL, buf, "WinMain", MB_OK);

    if ((UINT)dllh > HINSTANCE_ERROR)
        FreeLibrary(dllh);
    return 0;
}

-----------------------------------------------------------
#shell script for making LoadLib.exe
gcc -c app.c 
#gcc -o appa appa.o -mwindows --subsystem windows
gcc -o app app.o hello.a -mwindows #--subsystem windows
#with --subsystem windows it does not work

-----------------------------------------------------------
-----------------------------------------------------------
results

sequence of MeassageBoxes popping up with all possible 
combinations of hello.dll and LoadLib.exe compiled and 
linked on a NT system and a Win95 system respectively.

hello.dll	LibLoad.exe

NT			NT		WinMain/Start
					Dll/PROCESS_ATTACH
					WinMain/Last Error: 3	
					Dll/Hello from foof	
					WinMain/Last Error: 3 
					Dll/PROCESS_DETACH

NT			95		WinMain/Start 
					Dll/PROCESS_ATTACH 
					WinMain/Last Error: 0 
					Dll/Hello from foof 
					WinMain/Last Error: 0 
					Dll/PROCESS_DETACH

95          NT		WinMain/Start 
					WinMain/Last Error: 3
					WinMain/Error loading dll:0 
					WinMain/Last Error: 3

95			95		WinMain/Start 
					WinMain/Last Error: 0
					WinMain/Error loading dll:0 
					WinMain/Last Error: 0

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