Subject: strange behaviour with LoadLibrary
From: [email-masked] ("Philippe GIACINTI")

10 Jul 1997 14:31:12 -0700
Sender: [email-masked]
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
X-Face: ^cJc2\tiS@#Ja@7laz+N]T)kL{-Kay)Oz"?}p[wuY#}}dO"enP!QVO{,}&4P2V[hEfJD]9&
             E1ou~|tY/|9d&7c,7yf>7n: Zj8FD(LtPXWHzy^OJIDi/~O~R]3x/5}"7P;|,*%f:3Z!>h8Ff/&)g?/
             }$RXQ
X-Mailer: Z-Mail (3.2.3 08feb96 MediaMail)
Original-To: [email-masked]
Mime-Version: 1.0
Content-Type: multipart/mixed;
	boundary="PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de"
Original-Sender: [email-masked]

--
--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
Content-Type: text/plain; charset=us-ascii

Hi,

I have two dlls: dll2 which defines symbol "sym2", and dll1 which defines
symbol "sym1" and uses symbol "sym2" from dll2. dll1 is linked with import lib
of dll2.
Then I have two kind of main :

	1) a main which uses "sym1" and is linked with import lib of dll1
	result:
		- dll2 is loaded and entry point is called
		- dll1 is loaded and entry point is called
		- main call "sym1" which call "sym2" ==> OK

	2) a main which opens dll1 with LoadLibrary, get symbol "sym1" and then
call the function.
	result:
		- the first LoadLibrary gives a Win32 error
#998(ERROR_NOACCESS)
		- try LoadLibrary again, now it works but the entry point of
		dll1 is not called
		- when "sym1" is called I got an exception ( I can't know where
		because the first Win32 error stop debugging in gdb ...

I'm using b18 with Sergey's itimer patch on a NT4.0 Pentium Pro 200 box
( you can find sources and makefile attached to this mail )

Any help will be welcome.
Best regards.

-- 

 ==========================================================================
|                         Philippe GIACINTI                                |
|                                                                          |
| DALiM GmbH R&D                                email:  [email-masked]      |
| Daimler Strasse 2,                            tel:    +49.7851.9196-28   |
| D-77694 Kehl-Sundheim Germany                 fax:    +49.7851.73576	   |
|                                                                          |
 ==========================================================================

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: GNUmakefile
Content-Description: Text
Content-Type: text/plain ; name="GNUmakefile" ; charset=us-ascii

all: main1.exe main2.exe dll1.dll dll2.dll

clean:
	rm -f *.o *.a *.exe *.dll

%.o : %.cc
	gcc -g -c $< -o $@

LDFLAGS:= -L. -L/CYGWIN32/H-i386-cygwin32/i386-cygwin32/lib -L/CYGWIN32/H-i386-cygwin32/lib

%entry:= _cygwin_dll_entry@12

dll2.dll : dll2main.o dll2.def
	ld $(LDFLAGS) --dll -e $(%entry) -o tmp.dll --base-file tmp.base dll2main.o -lcygwin -lkernel32
	rm tmp.dll
	dlltool --dllname $@ --base-file tmp.base --output-exp tmp.exp --def dll2.def
	rm tmp.base
	ld $(LDFLAGS) --dll -e $(%entry) -o $@ tmp.exp dll2main.o -lcygwin -lkernel32
	rm tmp.exp

libdll2.a : dll2.def
	dlltool --dllname dll2.dll --def dll2.def --output-lib $@

dll1.dll : dll1main.o dll1.def libdll2.a
	ld $(LDFLAGS) --dll -e $(%entry) -o tmp.dll --base-file tmp.base dll1main.o -lcygwin -lkernel32 -ldll2
	rm tmp.dll
	dlltool --dllname $@ --base-file tmp.base --output-exp tmp.exp --def dll1.def
	rm tmp.base
	ld $(LDFLAGS) --dll -e $(%entry) -o $@ tmp.exp dll1main.o -lcygwin -lkernel32 -ldll2
	rm tmp.exp

libdll1.a : dll1.def
	dlltool --dllname dll1.dll --def dll1.def --output-lib $@

main2.exe : main2.o
	gcc $(LDFLAGS) -o $@ $?

main1.exe : main1.o libdll1.a
	gcc $(LDFLAGS) -o $@ main1.o -ldll1

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: dll1.def
Content-Description: Text
Content-Type: text/plain ; name="dll1.def" ; charset=us-ascii

EXPORTS
function1

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: dll1main.cc
Content-Description: Text
Content-Type: text/plain ; name="dll1main.cc" ; charset=us-ascii

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

extern "C"
{
  int WINAPI cygwin_dll_entry( HANDLE, DWORD, void* );
  int function1();
  extern int function2();
}

static int val = 1;

int WINAPI cygwin_dll_entry( HANDLE, DWORD reason, void* )
{
  switch( reason )
    {
    case DLL_PROCESS_ATTACH:
      val = 11;
      break;

    case DLL_PROCESS_DETACH:
      break;

    case DLL_THREAD_ATTACH:
      break;

    case DLL_THREAD_DETACH:
      break;
    }
  return 1;
}

int function1()
{
  int ret = function2() + val;
  return ret;
}

asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: dll2.def
Content-Description: Text
Content-Type: text/plain ; name="dll2.def" ; charset=us-ascii

EXPORTS
function2

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: dll2main.cc
Content-Description: Text
Content-Type: text/plain ; name="dll2main.cc" ; charset=us-ascii

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

extern "C"
{
  int WINAPI cygwin_dll_entry( HANDLE, DWORD, void* );
  int function2();
}

static int value = 2;

int WINAPI cygwin_dll_entry( HANDLE, DWORD reason, void* )
{
  switch( reason )
    {
    case DLL_PROCESS_ATTACH:
      value = 22;
      break;

    case DLL_PROCESS_DETACH:
      break;

    case DLL_THREAD_ATTACH:
      break;

    case DLL_THREAD_DETACH:
      break;
    }
  return 1;
}

int function2()
{
  return value;
}

asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: main1.cc
Content-Description: Text
Content-Type: text/plain ; name="main1.cc" ; charset=us-ascii

#include <stdio.h>

extern "C"
{
  extern int function1();
}

int main( int, char** )
{
  printf( "result=%d\n", function1() );
  return 0;
}

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de
X-Zm-Content-Name: main2.cc
Content-Description: Text
Content-Type: text/plain ; name="main2.cc" ; charset=us-ascii

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

typedef int (*ExternFunc)();

int main( int argc, char* argv[] )
{
#if 0
  const char* libname = argv[1];
  const char* funcname = argv[2];
#else
  const char* libname = "dll1";
  const char* funcname = "function1";
#endif
  
  printf( "Try to load dll \"%s\"\n", libname );
  HINSTANCE handle = LoadLibrary( libname );
  if( !handle )
    {
      printf( "LoadLibrary error #%d\n", GetLastError() );
      printf( "retrying ... \n" );
      handle = LoadLibrary( libname );
      if( !handle )
	{
	  printf( "LoadLibrary error #%d\n", GetLastError() );
	  return 1;
	}
    }
  printf( "Dll \"%s\" successfully loaded. handle=%p\n\n", libname, handle );

  printf( "Now try to get function \"%s\"\n", funcname );
  ExternFunc func = (ExternFunc)GetProcAddress( handle, funcname );
  if( !func )
    {
      printf( "GetProcAddress error #%d\n", GetLastError() );
      return 1;
    }
  printf( "Function \"%s\" found. func=%p\n\n", funcname, func );

  printf( "Now get result from function \"%s\"\n", funcname );
  printf( "result=%d\n", (*func)() );

  return 0;
}

--PART-BOUNDARY=.19707101204.ZM4741.kehl.dalim.de--

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