Autor Beitrag
pathx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 02.06.04 14:48 
Hallo!

Ich bin fast am verzweifeln.
Ich habe einen Kartenleser und spreche ihn wie folgt an:
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:
var ctn: word;
    pn: word;
    lenc: word;
    lenr: word;
    dad: Byte;
    sad: Byte;


function CT_init(ctn : word;
                  pn : word): Shortint; cdecl; external 'CTORG32.dll';
function CT_data(ctn : word;
                  var dad : Byte;
                  var sad : Byte;
                  lenc : Word;
                  command : PChar;
                  var lenr : Word;
                  response : PChar
                  ) : Shortint; cdecl; external 'CTORG32.dll';
function CT_close(ctn : word
                  ) : Shortint; cdecl; external 'CTORG32.dll';


function request: boolean;
var res: Shortint;
    command: PChar;
    response: PChar;
begin
result := false;
dad := 1;
sad := 2;
lenc := 6;
lenr := 2;
command := chr($20) + chr($12) + chr($01) + chr($00) + chr($01) + chr($03);
res := CT_Data(ctn, dad, sad, lenc, command, lenr, response);
Result := (res=0) AND (ord(response[0])=$90);
end;


Beim Aufruf dieser Funktion wird die Karte auch ordungsgemäß angefordert. ist aber das end; von function request erreicht, kommt es zu einer Exception.
Da zum Einlesen einer Karte vom Kartenleser noch mehr Funktionen gehören, hab ich alle Functionen in eine große gepackt. Auch hier wird alles sauber ausgeführt (Karte angefordert, Daten ausgelesen...) ist aber das Ende der großen Function erreicht kommt es wieder zur Exception. Weiß jemand woran das liegen kann? Bzw. wie kann ich eine Exception an dieser Stelle abfangen?

Vielen Dank,
Peter
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 02.06.04 14:53 
Na ja ne Exception abfangen kannst Du, indem du um die gesamte Funktion, die Du hier gepostet hast einen try..except-Block legst:

try
(...dein Quelltext...)
except
end;
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mi 02.06.04 15:04 
Hallo,

das Problem mit der Exception könnte an den nicht initialisierten PChar-Variablen liegen?

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: Mi 02.06.04 15:42 
Daran habe ich auch gedacht - einfach mal per GetMem dem PCHAR Speicher der Größe des Zieltyps zuweisen. DES PASST.

Normalerweise kann man bei API-Funktionen davon ausgehen, dass, wenn solch ein Parameter übergeben wird und dort ein NIL drinsteht, dann wird nur die Größe des benötigten Speichers (in diesem Fall sicher in lenr [lengthResponse!?]) zurückgegeben wird (in lenr).

Also einfach mal versuchen. Ansonsten einfach brutal per 64k festlegen (mehr trägt ein WORD nicht) und den Speicher mit 65.536 Bytes initialisieren.

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca
pathx Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: Mi 02.06.04 22:48 
Hallo!

Vielen Dank ersteinmal für die Ansätze. Ich habe versucht, den PChar Variablen Speicher zuzuweisen und den Code wie folgt mit GetMem ergänzt:
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:
var ctn: word;
    pn: word;
    lenc: word;
    lenr: word;
    dad: Byte;
    sad: Byte;


function CT_init(ctn : word;
                  pn : word): Shortint; cdecl; external 'CTORG32.dll';
function CT_data(ctn : word;
                  var dad : Byte;
                  var sad : Byte;
                  lenc : Word;
                  command : PChar;
                  var lenr : Word;
                  response : PChar
                  ) : Shortint; cdecl; external 'CTORG32.dll';
function CT_close(ctn : word
                  ) : Shortint; cdecl; external 'CTORG32.dll';


function request: boolean;
var res: Shortint;
    command: PChar;
    response: PChar;
begin
result := false;
dad := 1;
sad := 2;
lenc := 6;
lenr := 2;
GetMem(response, 65536);
GetMem(command, 65536);
command := chr($20) + chr($12) + chr($01) + chr($00) + chr($01) + chr($03);
res := CT_Data(ctn, dad, sad, lenc, command, lenr, response);
Result := (res=0) AND (ord(response[0])=$90);
end;

Leider bekomme ich immernoch am Ende der Funktion eine Exception. :(
Ich hoffe, dass ich das mit DetMem richtig verstanden habe.
Kann ich vielleicht noch irgendetwas verändern?

Vielen Dank,
Peter

PS: Zur Erklärung: ctn->Cardterminal; dad->Destination Address; sad->Source Address; lenc->Length Command; lenr->Length Response
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: Do 03.06.04 02:35 
Das command, da es statisch ist, brauchste nicht per GetMem initialisieren, sondern kannste einfach setzen, dann ist es schon in der Anwendung als Speicherbereich integriert.

Einfach command als array [0..5]of byte definieren und dann

command[0]:=$20;
command[1]:=$12;
command[2]:=$01;
command[3]:=$00;
command[4]:=$01;
command[5]:=$03;

und dann hast per @command einen pchar (wenn mans genau nimmt ein pbyte, aber byte ist genauso groß wie ein byte, bzw. char ist das entsprechende Zeichen in der ASCII-Tabelle an der Stelle "byte").

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca