Autor Beitrag
prebbe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 09:38 
I have a dll dynamiclly loaded, lets call this dll A. And within this dll i want to access some functions from a second dll, lets call this dll B.

But as soon as DLL A tries to use a function from dll B, my program is not even abel to load dll A.

What have i done wrong? Is it not possible to have "a dll in a dll"?

When dll A never asks for anything in dll B, my programcode: handle := LoadLibrary(StrPCopy(buf, dllname)); gives handle a value like 256552 or something like that. Just as i want it to be.

But as soon as dll A wants to access a function in dll B, the same code gives the handle value 0.

Anyone, please help me. Maybe i should do this another way??


Moderiert von user profile iconChristian S.: Topic aus Sonstiges (Delphi) verschoben am Di 24.07.2007 um 10:41
arj
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 378

Win XP/Vista, Debian, (K)Ubuntu
Delphi 5 Prof, Delphi 7 Prof, C# (#Develop, VS 2005), Java (Eclipse), C++, QT, PHP, Python
BeitragVerfasst: Di 24.07.07 10:09 
Perhaps you can show as some of your code?
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 10:16 
user profile iconarj hat folgendes geschrieben:
Perhaps you can show as some of your code?


Ok here is some of my code:

Executed when a button is clicked:

CallDll(vDLL, 'WriteToDisplay', 'test');

This is the content of CallDLL:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
try
    Handle := LoadLibrary(StrPCopy(buf, dllname)); // THIS HANDLE IS 0 [b]ONLY[/b] WHEN ONE OR MORE OF THE CODE MARKED GREEN IS PRESENT IN THE DLL.
    if Handle <> 0 then begin
      @theFunction := GetProcAddress(Handle, StrPCopy(buf, functionname));
      if @theFunction <> nil then result := theFunction(theParameter);
    end;
    FreeLibrary(Handle);
  except
    //Error
  end;


And this is the content of my entire dll:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
library lea;

uses
  ShareMem,
  SysUtils,
  Classes,
  Dialogs,
  Windows;

const
  cLines = 10;
  cLetters = 30;

function TrInitTransportDriver(p: PChar):integer; external 'Transport.dll';
function TrSetDevice(i: Integer):integer; external 'Transport.dll';
procedure TrKillTransportDriver(); external 'Transport.dll';
function TrAnsferData(p: PChar; Len, ToMsec, WaitTimeOut, respond: integer; err: PChar):PChar; external 'Transport.dll';

function GetLines(var data: string):string; export;
begin
  result := IntToStr(cLines);
end;

function WriteToDisplay(var data: string):boolean; export;
var p, err: PChar;
    buf: array[0..2000] of Char;
    a:integer;
begin
  p := 'Devices.ini';
  a := TrSetDevice(0);
  if TrInitTransportDriver(p) > 0 then begin
    if TrSetDevice(0) = 1 then begin
      TrAnsferData(StrPCopy(buf, data),3,1000,8000,1,&err);
    end;
    TrKillTransportDriver();
  end;
end;

Exports
  WriteToDisplay,
  GetLines;

{$R *.res}

begin

end.


By the way. I know that all my dlls are correct and working. It is only when the GREEN [Now: Marked text in WriteToDisplay, Christian S.] text is present it does not work.

Moderiert von user profile iconChristian S.: I- durch Delphi-Tags ersetzt
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 24.07.07 10:39 
1) only load the dll once, not everytime you click on the button.
2) are you sure the exe, dll_1 ,dll_2 are in the same directory?
3) are you sure dll_2 exports the functions? try to use a dynamic import of them

the static import is include in your dll if you use a function of it.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
library lea;

uses
ShareMem,
SysUtils,
Classes,
Dialogs,
Windows;

const
cLines = 10;
cLetters = 30;

function TrInitTransportDriver(p: PChar):integer; external 'Transport.dll';
function TrSetDevice(i: Integer):integer; external 'Transport.dll';
procedure TrKillTransportDriver(); external 'Transport.dll';
function TrAnsferData(p: PChar; Len, ToMsec, WaitTimeOut, respond: integer; err: PChar):PChar; external 'Transport.dll';

function GetLines(var data: string):string; export;
begin
  result := IntToStr(cLines);
end;

function WriteToDisplay(var data: string):boolean; export;
var p, err: PChar;
  buf: array[0..2000] of Char;
  a:integer;
begin
  p := 'Devices.ini';
  a := TrSetDevice(0);
end;

Exports
  WriteToDisplay,
  GetLines;

{$R *.res}

begin

end.


if you use this example (which works for you) the static import of the dll is not include (even if you wrote it at he begining) so I would say #3 is you problem - the dll is not found or one of the static import is incorrect.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 10:51 
user profile iconuall@ogc hat folgendes geschrieben:

1) only load the dll once, not everytime you click on the button. the button is just my way of testing this code. it would take forever if i had to test it at it is supposed to be in the future..
2) are you sure the exe, dll_1 ,dll_2 are in the same directory? yes
3) are you sure dll_2 exports the functions? try to use a dynamic import of them. i am sure. it is not my own dll. it works when i load it from my exe, but not from my dll as you saw in the code. i have tried dynamic import. same result.

the static import is include in your dll if you use a function of it.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
library lea;

uses
ShareMem,
SysUtils,
Classes,
Dialogs,
Windows;

const
cLines = 10;
cLetters = 30;

function TrInitTransportDriver(p: PChar):integer; external 'Transport.dll';
function TrSetDevice(i: Integer):integer; external 'Transport.dll';
procedure TrKillTransportDriver(); external 'Transport.dll';
function TrAnsferData(p: PChar; Len, ToMsec, WaitTimeOut, respond: integer; err: PChar):PChar; external 'Transport.dll';

function GetLines(var data: string):string; export;
begin
  result := IntToStr(cLines);
end;

function WriteToDisplay(var data: string):boolean; export;
var p, err: PChar;
  buf: array[0..2000] of Char;
  a:integer;
begin
  p := 'Devices.ini';
  a := TrSetDevice(0);
end;

Exports
  WriteToDisplay,
  GetLines;

{$R *.res}

begin

end.


if you use this example (which works for you) the static import of the dll is not include (even if you wrote it at he begining) so I would say #3 is you problem - the dll is not found or one of the static import is incorrect. i guess you meant number two??
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 10:57 
ANSWER!

There was a dependency of the second DLL that i did not see. So after retreiving the right dependencies, everything works just great.
Thanks for at least leading me on the right track!
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Di 24.07.07 11:37 
user profile iconprebbe hat folgendes geschrieben:
But as soon as dll A wants to access a function in dll B, the same code gives the handle value 0.
In this case you can use GetLastError() to look what went wrong with the last call.
To get a more verbose output, you can use FormatMessage like this:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  errorString: PChar;
begin
  ...
  SetLastError(0);
  LoadLibrary(...);
  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_FROM_SYSTEM,
    nil,
    GetLastError(),
    0,
    @errorString,
    0,
    nil);
(Hope this works, haven't written a line of Delphi code for a long time)

prebbe hat folgendes geschrieben:
Of course it is. If you include windows.pas, for example, you always link against Windows core libraries which come along as an ordinary DLL, no magic there. If you could not link a DLL against another DLL, you could not do much of anything useful in a DLL.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 12:48 
I used GetLastError and got the result '126' and that also put me on the right track for finding the solution.

user profile icontommie-lie hat folgendes geschrieben:
user profile iconprebbe hat folgendes geschrieben:
But as soon as dll A wants to access a function in dll B, the same code gives the handle value 0.
In this case you can use GetLastError() to look what went wrong with the last call.
To get a more verbose output, you can use FormatMessage like this:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  errorString: PChar;
begin
  ...
  SetLastError(0);
  LoadLibrary(...);
  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_FROM_SYSTEM,
    nil,
    GetLastError(),
    0,
    @errorString,
    0,
    nil);
(Hope this works, haven't written a line of Delphi code for a long time)

prebbe hat folgendes geschrieben:
Of course it is. If you include windows.pas, for example, you always link against Windows core libraries which come along as an ordinary DLL, no magic there. If you could not link a DLL against another DLL, you could not do much of anything useful in a DLL.