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



BeitragVerfasst: Di 24.07.07 12:56 
I have simplified this code a little bit so it would be easier to see:[/b]

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:
library lea;

uses
  ShareMem,
  SysUtils,
  Classes,
  Dialogs,
  Windows;

const
  cLines = 10;
  cLetters = 30;

function TrInitTransportDriver(petffsd: string):integer; external 'Transport.dll';
function TrSetDevice(i: Integer):integer; external 'Transport.dll';

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

Exports
  WriteToDisplay

{$R *.res}

begin
end.


Ok. The problem is that when i run this code and the function WriteToDisplay, the 'TrSetDevice(0)'-code is executed as i could expect. But when i come to the next line a problem occurs. In the debugger i jump from the code up to the top where TrInitTransportDriver is defined(just as expected). And at the next step i suddenly jump all the way to 'end;' in the code in the exe. (At the bottom of where the quoted dll is dynamically loaded).

Anyone?

I just checked GetLastError and the result was: '2'

Moderiert von user profile iconTino: Fett- und Kursiv-Tag entfernt.
Lossy eX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1048
Erhaltene Danke: 4



BeitragVerfasst: Di 24.07.07 14:00 
For Errorcode look there. msdn2.microsoft.com/...ibrary/ms681381.aspx

The error 2 is named "The system cannot find the file specified.". It's possible that you current directory isn't right so the "UsbDevice.ini" dosn't exists. If you can use absolute paths you should do it. I don't know if it necessary for an inifile.

But GetLastErrorCode only defines some error code. That dosn't mean that you will get an exception or an real error. Some errors are handled internal in some functions (systemfunctions too) but the LastError is still set.


Some other fact. You simple defines some methods but you dosn't define an calling convention. It's better you define an convention for all methods. I prefer stdcall or cdecl. You must define it on the export and import side of your code.

_________________
Nur die Menschheit ist arrogant genug, um zu glauben sie sei die einzige intelligente Lebensform im All. Wo nicht mal das nachhaltig bewiesen wurde.
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 14:23 
user profile iconLossy eX hat folgendes geschrieben:
For Errorcode look there. msdn2.microsoft.com/...ibrary/ms681381.aspx

The error 2 is named "The system cannot find the file specified.". It's possible that you current directory isn't right so the "UsbDevice.ini" dosn't exists. If you can use absolute paths you should do it. I don't know if it necessary for an inifile.

But GetLastErrorCode only defines some error code. That dosn't mean that you will get an exception or an real error. Some errors are handled internal in some functions (systemfunctions too) but the LastError is still set.


Some other fact. You simple defines some methods but you dosn't define an calling convention. It's better you define an convention for all methods. I prefer stdcall or cdecl. You must define it on the export and import side of your code.


I am keeping all my files (for this project of cours) including dlls, exe and so on in ONE FOLDER. So the file exists. I saw that ErrorCode 2 was file not found, so the ErrorCode has probably nothing to do with the error i experience.

I am not sure i understand what you mean by define convention for all methods. I suppose you mean something like this. Correct me if i am wrong:
ausblenden Delphi-Quelltext
1:
2:
  function TrSetDevice(i: Integer):integer; stdcall; external 'Transport.dll';
  function TrInitTransportDriver(s: String):integer; stdcall; external 'Transport.dll';


But that did not help. Same error. Anything else it might be?
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 24.07.07 14:43 
When i look at the dll-description from the manufacturer i see that they use stdcall in the dll. So i will use that from now on.

Quote from that document where they describe all the functions in the dll: "int DLL_EXP _stdcall TrInitTransportDriver(char *DeviceDefFile)"

I guess that is c. And with my experience with c i translated it to Delphi and got:
"function TrInitTransportDriver(s: String):integer; stdcall; external 'Transport.dll';" (Strings are pointers, right?)

Is that correct or what?



user profile iconprebbe hat folgendes geschrieben:
user profile iconLossy eX hat folgendes geschrieben:
For Errorcode look there. msdn2.microsoft.com/...ibrary/ms681381.aspx

The error 2 is named "The system cannot find the file specified.". It's possible that you current directory isn't right so the "UsbDevice.ini" dosn't exists. If you can use absolute paths you should do it. I don't know if it necessary for an inifile.

But GetLastErrorCode only defines some error code. That dosn't mean that you will get an exception or an real error. Some errors are handled internal in some functions (systemfunctions too) but the LastError is still set.


Some other fact. You simple defines some methods but you dosn't define an calling convention. It's better you define an convention for all methods. I prefer stdcall or cdecl. You must define it on the export and import side of your code.


I am keeping all my files (for this project of cours) including dlls, exe and so on in ONE FOLDER. So the file exists. I saw that ErrorCode 2 was file not found, so the ErrorCode has probably nothing to do with the error i experience.

I am not sure i understand what you mean by define convention for all methods. I suppose you mean something like this. Correct me if i am wrong:
ausblenden Delphi-Quelltext
1:
2:
  function TrSetDevice(i: Integer):integer; stdcall; external 'Transport.dll';
  function TrInitTransportDriver(s: String):integer; stdcall; external 'Transport.dll';


But that did not help. Same error. Anything else it might be?
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 15:03 
Instead of
s:string
use
s: pchar

_________________
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: Mi 25.07.07 07:21 
user profile iconuall@ogc hat folgendes geschrieben:
Instead of
s:string
use
s: pchar


I tried. Same result. Isn't a string in Delphi really a pointer?

I find it weird that TrSetDevice works, but not TrInitTransportDriver. And why is it jumping all the way back to the exe, not even executing the rest of my dll? I have also tried other functions from the dll, and they work. What is wrong with TrInitTransportDriver? I know it should work, because i have seen it earlier.

Anything else?
Lossy eX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1048
Erhaltene Danke: 4



BeitragVerfasst: Mi 25.07.07 07:55 
Strings: You must differ between shortstrings and longstrings. By default delphi 5+ (i dont know it right) using longstrings. Longstrings are pointers but they had an length before the pointer and delphi counts the usage of it to free them correctly. But this could make some problems if you are using dll without shared memory. And it's not really compatible to c/c++. So it's better to use real pointer types (pchar).

DLL: The definition of the functions looks okay for me so i don't know where they could an problem. Is it possible that the function can have an error? Have you checked the result of it? Or the function before. For me you code looks okay. What happens when you use the Tr* function directly in you application?

_________________
Nur die Menschheit ist arrogant genug, um zu glauben sie sei die einzige intelligente Lebensform im All. Wo nicht mal das nachhaltig bewiesen wurde.
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 25.07.07 08:06 
user profile iconLossy eX hat folgendes geschrieben:
Strings: You must differ between shortstrings and longstrings. By default delphi 5+ (i dont know it right) using longstrings. Longstrings are pointers but they had an length before the pointer and delphi counts the usage of it to free them correctly. But this could make some problems if you are using dll without shared memory. And it's not really compatible to c/c++. So it's better to use real pointer types (pchar).

DLL: The definition of the functions looks okay for me so i don't know where they could an problem. Is it possible that the function can have an error? Have you checked the result of it? Or the function before. For me you code looks okay. What happens when you use the Tr* function directly in you application?


Ok from now on i will use pchar.

If i load the dll in my exe the normal way(i have not tried dynamically) everything works fine. Even the TrInitTransportDriver. But when i do it in a dll (and i have to do it like this) it does not work.

Getting crazy now.
prebbe Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 25.07.07 08:44 
This is the code in my exe:

I use a button here just to make testing easier.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
  CallDll(vDLL, 'WriteToDisplay', PChar('test'));
end;


ausblenden 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:
function CallDLL(dllname, functionname, theParameter : string):string;
var
  Handle : THandle;
  theFunction : TTheFunction;
  buf: array[0..544] of Char;
begin
  try
    Handle := LoadLibrary(StrPCopy(buf, dllname));
    if Handle <> 0 then begin
      @theFunction := GetProcAddress(Handle, StrPCopy(buf, functionname));
      if @theFunction <> nil then begin
        result := theFunction(theParameter);
      end;
    end;
    FreeLibrary(Handle);
  except
    //Noe fryktelig fælt har skjedd...
     on E : Exception do
     begin
       ShowMessage('Exception class name = '+E.ClassName);
       ShowMessage('Exception message = '+E.Message);
     end;
  end;
end;


The exception looks like this: "Exception class name = EAccessViolation" and "Exception message = Access violation at address 01588008 in module 'Transport.dll'. Write of address 000050B8.

But why? I have seen the function working earlier. With exactly the same Transport.dll. And it works on other functions. Not TrInitTransportDriver.
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: Mi 25.07.07 11:14 
You must use PChar instea of String (you dont do it in this example).
And please dont load the Library for every call, only on program start:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
  var bla: procedure;
  dll: Integer;
  

procedure programstart;
begin
  dll := LoadLibraryA('mydll.dll');
  @bla := GetProcAddress(dll, 'THeFunction');
end;

procedure useexport;
begin
  bla;
end;

procedure programend;
begin
  FreeLibrary(dll)
end;


If you have a string cast it with PChar:

LoadLibrary(PChar(thestring)), you dont need to use StrPCopy

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
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: Mi 25.07.07 11:27 
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:
48:
49:
50:
51:
52:
53:
54:
library mdll;

uses windows, sharemem;


var
  dllTransport: Integer = 0;
  TrSetDevice: function(i: Integer):integer; stdcall; 
  TrInitTransportDriver: function(s: PChar):integer; stdcall;   
 
procedure DllLoadStuff;
begin
  dllTransport := LoadLibraryA('transport.dll');
  if (dllTransport <> 0) then
  begin
    @TrSetDevice := GetProcAddress(dllTransport, 'TrSetService');
    @TrInitTransportDriver := GetProcAddress(dllTrasnport, 'TrInitTransportDriver');
  end;
end;

function DoSomething(s: string): integer; stdcall;
begin
  // dont know ;P
  TrSetDevice(100);
  result := TrInitTransportDriver(PChar(s)); // cast string to pchar 
end;

procedure DllUnloadStuff;
begin
  if (dllTransport <> 0) then
    FreeLibrary(dllTransport);
end;

exports
  DoSomething;

procedure DllMain(reason: integer);
begin
   case reason of
     DLL_PROCESS_ATTACH:
     begin
        DllLoadStuff;
     end;
     DLL_PROCESS_DETACH
     begin
       DllUnloadStuff;
     end;
   end;
end;

begin
   DllProc := @DllMain;
   DllProc(DLL_PROCESS_ATTACH);
end.


it should look like this ;)

_________________
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: Fr 27.07.07 13:01 
Problem solved!

There was nothing wrong with the code. My computer just needed a little reboot. Booring to trouble with such a problem for so long, and all the time it is just the computer.

user profile iconuall@ogc hat folgendes geschrieben:
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:
48:
49:
50:
51:
52:
53:
54:
library mdll;

uses windows, sharemem;


var
  dllTransport: Integer = 0;
  TrSetDevice: function(i: Integer):integer; stdcall; 
  TrInitTransportDriver: function(s: PChar):integer; stdcall;   
 
procedure DllLoadStuff;
begin
  dllTransport := LoadLibraryA('transport.dll');
  if (dllTransport <> 0) then
  begin
    @TrSetDevice := GetProcAddress(dllTransport, 'TrSetService');
    @TrInitTransportDriver := GetProcAddress(dllTrasnport, 'TrInitTransportDriver');
  end;
end;

function DoSomething(s: string): integer; stdcall;
begin
  // dont know ;P
  TrSetDevice(100);
  result := TrInitTransportDriver(PChar(s)); // cast string to pchar 
end;

procedure DllUnloadStuff;
begin
  if (dllTransport <> 0) then
    FreeLibrary(dllTransport);
end;

exports
  DoSomething;

procedure DllMain(reason: integer);
begin
   case reason of
     DLL_PROCESS_ATTACH:
     begin
        DllLoadStuff;
     end;
     DLL_PROCESS_DETACH
     begin
       DllUnloadStuff;
     end;
   end;
end;

begin
   DllProc := @DllMain;
   DllProc(DLL_PROCESS_ATTACH);
end.


it should look like this ;)