This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

strange behaviour with LoadLibrary


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:  giac@dalim.de      |
| Daimler Strasse 2,                            tel:    +49.7851.9196-28   |
| D-77694 Kehl-Sundheim Germany                 fax:    +49.7851.73576	   |
|                                                                          |
 ==========================================================================
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

EXPORTS
function1

#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");
EXPORTS
function2
#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");
#include <stdio.h>

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

int main( int, char** )
{
  printf( "result=%d\n", function1() );
  return 0;
}
#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;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]