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



BeitragVerfasst: Fr 27.07.07 13:15 
Hi! Strange problem... i ditched the if-loops and it worked fine. but if i do not check the result, i will eventually get into trouble if the dll is not present or something.

question is sort of answered, but if anyone knows why, i would be happy!!!



Zitat:
Hi! Great forum! Helps me a lot. Need another answer to a new problem that occured.

The following code produces a stupid error:

Loading of Transport.dll:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function TrSetDevice(i: Integer):integer; stdcall; external 'Transport.dll';
procedure TrKillTransportDriver(); stdcall; external 'Transport.dll';
function TrVariableWrite(a, b: string):PChar; stdcall; external 'Transport.dll';
function TrInitTransportDriver(s: PChar):integer; stdcall; external 'Transport.dll';
function TrAnsferData(p: string; Len, ToMsec, WaitTimeOut, respond: integer; err: PInt):PChar; stdcall; external 'Transport.dll';


In my application:

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:
25:
26:
27:
28:
29:
procedure TForm1.Button2Click(Sender: TObject);
var zErr : PInt;
begin
  //Loads all communicationdrivers:
  if TrInitTransportDriver('Devices.ini') > 0 then begin
    
    //Chooses communication:
    if TrSetDevice(0) = 1 then begin
      
      //Prepares word on each line on the external usb display:
      TrVariableWrite('TEXT_1','Det');
      TrVariableWrite('TEXT_2','var');
      TrVariableWrite('TEXT_3','en');
      TrVariableWrite('TEXT_4','gang');
      TrVariableWrite('TEXT_5','er');
      TrVariableWrite('TEXT_6','stor');
      TrVariableWrite('TEXT_7','stygg');
      TrVariableWrite('TEXT_8','ulv');
      TrVariableWrite('TEXT_9','der');
      TrVariableWrite('TEXT_10','borte');

      //Writes each line with the new word.
      TrAnsferData('SEP0,100',8,1000,8000,1,@zErr);
      
      //Ends the communication
      TrKillTransportDriver();
    end;
  end;
end;


The error is like this: Access Violation at 0x0045e443: read of address 0x000000000. But i can not understand what I do that should throw an access violation.

If i comment TrAnsferData and TrKillTransportDriver the access violation is found at address 0012f524 writing to address 00000000. When looking at the pcu assembly the code tries to pop esi (esi=0012f154).

The problem is never present when i try to write just a couple of lines, but occurs when i try to write all ten.

Anyone got a clue?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 27.07.07 14:36 
Well, I can't see any problems based on the posted code only. But there is in general a problem with sending strings to a DLL using string as data type as Strings are handled in such a way that memory is allocated dynamically. You should never transfer dynamic data types (like String or dynamic Arrays) between different modules (e.g. EXE<->DLL or DLL<->DLL). Instead use static data types like PChar instead (as replacement for strings) and Pointers to the underlaying datastructure for Arrays.

Furtheremore: What language was the DLL written in? Delphi or C(++)??? Based on this there are in general some rules that can help avoid problems.

P.S.: Please avoid using bold at places where not required (e.g. in quotes ;-)). It's ugly reading ;-) Bold font should be used for highlighting (which can be done in this forum using the highlight-tag too).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.