public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: Another problem with DLL's
  1997-03-21  6:04 Another problem with DLL's Valery Fine
@ 1997-03-21  6:04 ` John Cerney
  0 siblings, 0 replies; 6+ messages in thread
From: John Cerney @ 1997-03-21  6:04 UTC (permalink / raw)
  To: Valery Fine; +Cc: gnu-win32

>
>  > I'm not sure what is wrong with your code, but I know this does
>  > work. Here is a simple example of loading a DLL at runtime using
>  > LoadLibraryEx calls. This compiles and links fine on both win95 and
>  > winNT.
>  
>     Hello John!
>  
>     Just one question. Did you try to compile your MAIN with VC++ but 
>  DLL with gnu-win32 ? Does it work ?


I just tried compiling the main2.c with VC++. 

When running, the thing crashes when the DLL function is called. An access
violation occurs in cygwin.dll. The foo.dll that is built uses cygwin.dll for
its printf function. If I take the printf out of the foo.dll code the VC++
main2.exe will run just fine. 

I think when a gnuwin32 main program is linked, some code to initialize the
cygwin system is included. Since we compile the main program here using VC++, I
suspect that the cygwin.dll never gets initialized, and crashes when its
functions are called. It is possible that this might be fixed by adding some
initialization routines to the dll_entry routine for the dll.


Regards,

John
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Another problem with DLL's
@ 1997-03-21  6:04 Valery Fine
  1997-03-21  6:04 ` John Cerney
  0 siblings, 1 reply; 6+ messages in thread
From: Valery Fine @ 1997-03-21  6:04 UTC (permalink / raw)
  To: John Cerney; +Cc: gnu-win32

On 20 Mar 97 at 13:17, John Cerney wrote:

> 
> >I've got a problem with DLL's...
> >
> >Folowing the instruccions of the home page of Cygnus, everything
> >works OK when I link the DLL with my executable but... if you try
> >to load the library indirectly via LoadLibrary(), it returns an
> >HRESULT of 0
> >
> >Someone knows how to solve this or is just a bug?
> 
> I'm not sure what is wrong with your code, but I know this does
> work. Here is a simple example of loading a DLL at runtime using
> LoadLibraryEx calls. This compiles and links fine on both win95 and
> winNT.

   Hello John!

   Just one question. Did you try to compile your MAIN with VC++ but 
DLL with gnu-win32 ? Does it work ?
=================================================================
Dr. Valery Fine                  Telex : 911621 dubna su
    -----------
LCTA/Joint Inst.for NuclearRes   Phone : +7 09621 6 40 80
141980 Dubna, Moscow region      Fax   : +7 09621 6 51 45
Russia                           mailto:fine@main1.jinr.dubna.su
                                 mailto:fine@vxcern.cern.ch
Dr. Valeri Faine
    ------------                 Phone: +41 22 767 6468
CERN                             FAX  : +41 22 782 2601
CH-1211 Geneva, 23               mailto:fine@vxcern.cern.ch 
Switzerland                      http://nicewww.cern.ch/~fine
                                 
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Another problem with DLL's
@ 1997-03-22  0:39 Sergey Okhapkin
  0 siblings, 0 replies; 6+ messages in thread
From: Sergey Okhapkin @ 1997-03-22  0:39 UTC (permalink / raw)
  To: Valery Fine, 'John Cerney'
  Cc: gnu-win32, 'Geoffrey Noer', 'Jeremy Allison'

John Cerney wrote:
>
> When running, the thing crashes when the DLL function is called. An 
access
> violation occurs in cygwin.dll. The foo.dll that is built uses cygwin.dll 
for
> its printf function. If I take the printf out of the foo.dll code the 
VC++
> main2.exe will run just fine.
>

This problem is due to struct reent_data in a dll and pointer to it 
(_impure_ptr) remains uninitialized after dll loading. The patch below 
solves this problem. This patch moves reent_data from application's private 
address space to cygwin.dll and adds pointer to this structure to 
cygwin.dll export table. Auto-initialisation code of your dll now can 
initialize _impure_ptr properly. Also, this patch modifies fork() code to 
copy contents of parent's reent_data to the child. Allpy this patch (it 
modifies files dcrt0.cc, fork.cc and winsup.h), add "reent_data" to export 
list in cygwin.din, and rebuild cygwin.dll. Add the following file to your 
dll:

#include <windows.h>
#include <stdio.h>
extern struct _reent *_impure_ptr, 
*__imp_reent_data;
int WINAPI dll_main(HANDLE a, DWORD reason, void *q)
{ 
switch (reason){
   case DLL_PROCESS_ATTACH: break;
   case 
DLL_PROCESS_DETACH: break;
   case DLL_THREAD_ATTACH:  break;
   case 
DLL_THREAD_DETACH:  break;
  };
  _impure_ptr=__imp_reent_data;
  return 
1;
}
main(){}

and build your dll (I've used three-pass method as for cygwin.dll). Ld's 
command line should contain "-e _dll_main@12".

 

--
Sergey Okhapkin
Moscow, Russia
Looking for a job

begin 600 dll.diff
M*BHJ(&1C<G0P+F-C"51U92!&96(@,#0@,#<Z,#<Z,#,@,3DY-PHM+2T@93ID
M8W)T,"YC8PE4:'4@36%R(#(P(#(R.C$U.C0R(#$Y.3<**BHJ*BHJ*BHJ*BHJ
M*BHJ"BHJ*B S,BPS-R J*BHJ"BTM+2 S,BPS." M+2TM"B @("!C:&%R("HJ
M96YV:7)O;CL*(" @("\J(%]?<')O9VYA;64@=7-E9"!I;B!G971O<'0@97)R
M;W(@;65S<V%G92 J+PH@(" @8VAA<B J7U]P<F]G;F%M93L**R @('-T<G5C
M="!?<F5E;G0@<F5E;G1?9&%T83L*("!].PH@( H@('-T871I8R!C;VYS="!I
M;G0@8WEG=VEN7V1L;%]V97)S:6]N7VUA:F]R(#T@0UE'5TE.7T1,3%]615)3
M24].7TU!2D]2.PHJ*BHJ*BHJ*BHJ*BHJ*BH**BHJ(#(V-"PR-S @*BHJ*@H@
M(" @:6YT(&%R9V,["B @("!C:&%R("HJ87)G=CL*(" @(&EN="!I.PHM(" @
M<W1R=6-T(%]R965N="!R965N=%]D871A.PH@(" @+RH@06-C;W)D:6YG('1O
M(&]N;F] <W1A8VLN=7)C+G1U92YN;"P@=&AE(&5X8V5P=&EO;B!H86YD;&5R
M(')E8V]R9"!M=7-T"B @(" @("!B92!O;B!T:&4@<W1A8VLN(" J+PH@(" @
M+RH@1DE8344Z(%9E<FEF>2!F;W)K960@8VAI;&1R96X@9V5T('1H96ER(&5X
M8V5P=&EO;B!H86YD;&5R('-E="!U<"!O:RX@("HO"BTM+2 R-C4L,C<P("TM
M+2T**BHJ(&9O<FLN8V,)5'5E($9E8B Q,2 P,#HQ,#HQ.2 Q.3DW"BTM+2!E
M.F9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YT7V1A
M=&$["B @("!C:&EL9"T^<')O9VYA;64@/2!U+3YS96QF+3YP<F]G;F%M93L*
M(" *(" @("\J($-O<'D@86QL('1H92!H86YD;&5S('=E(&YE960@:6X@=&AE
M(&-H:6QD+B @*B\**BHJ*BHJ*BHJ*BHJ*BHJ"BHJ*B S.3$L,SDV("HJ*BH*
M+2TM(#,Y,RPS.3D@+2TM+0H@(" @(" @(&1E8G5G7W!R:6YT9B H(F9O<FM?
M:&5L<&5R.B!C:&EL9"!I<R!R=6YN:6YG7&XB*3L*(" *(" @(" @("!U+3YS
M96QF(#T@<RT^<%MX73L**R @(" @("!R965N=%]D871A(#T@=2T^<V5L9BT^
M<F5E;G1?<V%V93L*(" @(" @("!D96)U9U]P<FEN=&8@*")F;W)K(&-H:6QD
M.B!S96QF("5P+"!P:60@)60L('!P:60@)61<;B(L"B @"0D@(" @=2T^<V5L
M9BP@>"P@=2T^<V5L9BT^<'!I9"D["B @"BHJ*B!W:6YS=7 N:"YO<FEG"49R
M:2!&96(@,30@,#$Z,#$Z,3D@,3DY-PHM+2T@=VEN<W5P+F@)5&AU($UA<B R
M," Q-SHP,SHR-R Q.3DW"BHJ*BHJ*BHJ*BHJ*BHJ*@HJ*BH@,C,X+#(T,R J
M*BHJ"BTM+2 R,S@L,C0T("TM+2T*(" *(" @("\J($]P96X@9FEL92!T86)L
M92X@*B\*(" @(&AI;F9O7W9E8R!H;6%P.PHK(" @<W1R=6-T(%]R965N="!R
M965N=%]S879E.PH@( H@(" @<W1R=6-T('-I9V%C=&EO;B!S:6=S6TY324==
M.PH@(" @<VEG<V5T7W0@<VEG7VUA<VL["0DO*B!O;F4@<V5T(&9O<B!E=F5R
5>71H:6YG('1O(&EG;F]R92X@*B\*
`
end

-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Another problem with DLL's
@ 1997-03-21 11:43  Ismael  Jurado
  0 siblings, 0 replies; 6+ messages in thread
From:  Ismael  Jurado @ 1997-03-21 11:43 UTC (permalink / raw)
  To: j-cerney1; +Cc: gnu-win32

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1159 bytes --]

>>I've got a problem with DLL's...
>>
>>Folowing the instruccions of the home page of Cygnus, everything works
>>OK when I link the DLL with my executable but... if you try to load the
>>library indirectly via LoadLibrary(), it returns an HRESULT of 0
>>
>>Someone knows how to solve this or is just a bug?
>
>I'm not sure what is wrong with your code, but I know this does work. Here is
a
>simple example of loading a DLL at runtime using LoadLibraryEx calls. This
>compiles and links fine on both win95 and winNT.


 Thanks for your code but... It still don´t works. I've copied your code
and folowed all the compilations steps and is always the same history:
when linked with the program the DLL works, but from the outside, it can't be
load. If it works in your system, I can't understand why don't works in
mine. Thanks anyway.

Ismael Jurado <ismaelj@hotmail.com>

---------------------------------------------------------
Get Your *Web-Based* Free Email at http://www.hotmail.com
---------------------------------------------------------
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Another problem with DLL's
@ 1997-03-20 11:37 John Cerney
  0 siblings, 0 replies; 6+ messages in thread
From: John Cerney @ 1997-03-20 11:37 UTC (permalink / raw)
  To:  Ismael  Jurado; +Cc: gnu-win32

>I've got a problem with DLL's...
>
>Folowing the instruccions of the home page of Cygnus, everything works
>OK when I link the DLL with my executable but... if you try to load the
>library indirectly via LoadLibrary(), it returns an HRESULT of 0
>
>Someone knows how to solve this or is just a bug?

I'm not sure what is wrong with your code, but I know this does work. Here is a
simple example of loading a DLL at runtime using LoadLibraryEx calls. This
compiles and links fine on both win95 and winNT.

  Note: This uses the same 3-pass ld/dlltool process that Cygnus used to build
    cygwin.dll. I just adapted it from the cygwin.dll makefile included in the
     source distribution. You might be able to get by with a procedure that
uses
     only 1 or 2 passes. However, I have had problems with executables not
running
     on both winNT and win95 when a 1 or 2 pass procedure is used. I believe
the
     3-pass approach to be the safest.



-John

***** File main2.c *******
// 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));
       
}
***** 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.cc ******
// DLL entry point module
//
#include <windows.h> 

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

};


int WINAPI dll_entry (HANDLE , 
		     DWORD reason,
		     void *)
{
  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 buildlib *****
#! /bin/sh
# script to build the simple test case DLL and a the main executable that runs
it
#   jc 3/12/97
#


LIBPATH=/gnuwin32/H-i386-cygwin32/i386-cygwin32/lib

gcc -g -c foo.c
gcc -c init.cc
gcc -c fixup.c
echo EXPORTS > foo.def
nm foo.o init.o fixup.o | grep '^........ [T] _' | sed 's/[^_]*_//' >> foo.def

# 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 

# Build the foo.a lib to link to: (not really needed for run-time loading)
dlltool --as=as --dllname foo.dll --def foo.def --output-lib foo.a

# Linking with main
gcc -g main2.c -o main2.exe
 
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Another problem with DLL's
@ 1997-03-19 10:28  Ismael  Jurado
  0 siblings, 0 replies; 6+ messages in thread
From:  Ismael  Jurado @ 1997-03-19 10:28 UTC (permalink / raw)
  To: gnu-win32

I've got a problem with DLL's...

Folowing the instruccions of the home page of Cygnus, everything works
OK when I link the DLL with my executable but... if you try to load the
library indirectly via LoadLibrary(), it returns an HRESULT of 0

Someone knows how to solve this or is just a bug?

Thanks, 
     ismaelj@hotmail.com


code folows:
----------------------------------------------------------------------
mydll.h
----------------------------------------------------------------------
#include <windows.h>

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

----------------------------------------------------------------------
mydll.c
----------------------------------------------------------------------
#include "dll.h"

int WINAPI DllMain(HINSTANCE H, DWORD d, PVOID p) {
  return TRUE;
}

void WINAPI foof() {
  MessageBox(NULL, "Hello from the DLL", "DLL", MB_OK);
}
asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");

----------------------------------------------------------------------
mydll.def
----------------------------------------------------------------------
EXPORTS
foof
----------------------------------------------------------------------
main.c
----------------------------------------------------------------------
#include <stdio.h>
#include "dll.h"

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR
szCmdLine, int iCmdShow) {
  HANDLE dllh;
  foop foo;
  char buf[126];
  dllh=LoadLibrary("mydll.dll");
  if ((UINT)dllh <= HINSTANCE_ERROR){
    sprintf(buf,"Error loading DLL: %d", dllh);
    MessageBox(NULL, buf, "APP", MB_OK);
  }
  else {
   foo=(foop)GetProcAddress(dllh, "foof");
  if (foo==NULL)
      MessageBox(NULL, "foo== null", "APP", MB_OK);
    else
      foo();
  }
  if ((UINT)dllh > HINSTANCE_ERROR)
    FreeLibrary(dllh);
  return 0;
}
----------------------------------------------------------------------
Dll link process:
----------------------------------------------------------------------
gcc -c mydll.c
ld --dll --subsystem windows -e _DllMain@12 -o jnk \
   --base-file dll.base mydll.o -L%LIBRARY_PATH% -luser32
dlltool --dllname mydll.dll --base-file dll.base \
    --def mydll.def --output-lib mydll.a --output-exp mydll.exp
ld --dll --subsystem windows -e _DllMain@12 -o mydll.dll \
   mydll.exp mydll.o -L%LIBRARY_PATH% -luser32
rm jnk dll.base

---------------------------------------------------------
Get Your *Web-Based* Free Email at http://www.hotmail.com
---------------------------------------------------------
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1997-03-22  0:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-21  6:04 Another problem with DLL's Valery Fine
1997-03-21  6:04 ` John Cerney
  -- strict thread matches above, loose matches on Subject: below --
1997-03-22  0:39 Sergey Okhapkin
1997-03-21 11:43  Ismael  Jurado
1997-03-20 11:37 John Cerney
1997-03-19 10:28  Ismael  Jurado

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).