Link to home
Start Free TrialLog in
Avatar of lloydie-t
lloydie-t

asked on

passing line number to a DLL

I have an app which reads strings from a file processes it in a DLL before showing the output. The problem I have is that the information is kept over to lines and I need the DLL to recognise which line it is reading from.

So the DLL is as follows
---------------------------------------
library collexconv;

uses
  ShareMem,
  SysUtils,
  Classes;

{$R *.res}
Function GrpNum (S: String): String; stdcall;
begin
Result := copy(S,1,1);
if  Result = 'G' then  begin
Result := copy(S,2,5);
Result := StringReplace(Result, ' ', '', [rfReplaceAll]); // is on line 1
end
else
Result := '';
end;
Function StartNum (S: String): String; stdcall;
begin
Result := copy(S,1,1);
if  Result = 'A' then  begin
Result := copy(S,2,5);
Result := StringReplace(Result, ' ', '', [rfReplaceAll]); // Is on line 2
end
else
Result := '';
end;

exports
GrpNum, StartNum;
end.
-----------------------------------------------------------------------------------------------

and the app is

----------------------------------------------------------------------------------------------
Uses
ShareMem;
Function GrpNum (S: String): String; stdcall; external 'collexconv.dll';
Function StartNum (S: String): String; stdcall; external 'collexconv.dll';

type
.........
........
.........

sl.LoadFromFile(CdrImportEdit.Text);
    //loop though each line of text
    for i := 0 to sl.count - 1 do
    begin
      group_no := GrpNum(sl[i]);
      start_no := StartNum(sl[i]);
     end;
-----------------------------------------------------------------------------------------------
I assume that in the app I can set an integer var, so that it is either line 0 or 1, but I then need to pass that info to the DLL so it can check it is reading the correct line.
Avatar of _Katka_
_Katka_
Flag of Czechia image

Hi,

what about simply put another function to dll, like this:

-----------------------------------------------------------------------------

ibrary collexconv;

uses
  ShareMem,
  SysUtils,
  Classes;

{$R *.res}

{INSERTED BY KATE}

var
  ActualLine:Integer; // here the actual line is stored

Procedure SetActualLine (Line: Integer); stdcall;
begin
  ActualLine:=Line;
end;

{END OF INSERTION}

Function GrpNum (S: String): String; stdcall;
begin
Result := copy(S,1,1);
if  Result = 'G' then  begin
Result := copy(S,2,5);
Result := StringReplace(Result, ' ', '', [rfReplaceAll]); // is on line 1
end
else
Result := '';
end;
Function StartNum (S: String): String; stdcall;
begin
Result := copy(S,1,1);
if  Result = 'A' then  begin
Result := copy(S,2,5);
Result := StringReplace(Result, ' ', '', [rfReplaceAll]); // Is on line 2
end
else
Result := '';
end;

exports
{INSERTED BY KATE}
SetActualLine,
{END OF INSERTION}
GrpNum, StartNum;
end.

------------------------------------------------------------------------------

and then in application call this function every single cycle:

------------------------------------------------------------------------------

Uses
ShareMem;
Function GrpNum (S: String): String; stdcall; external 'collexconv.dll';
Function StartNum (S: String): String; stdcall; external 'collexconv.dll';

type
.........
........
.........

sl.LoadFromFile(CdrImportEdit.Text);
    //loop though each line of text
    for i := 0 to sl.count - 1 do
    begin
{INSERTED BY KATE}
      SetActualLine(I);
{END OF INSERTION}
      group_no := GrpNum(sl[i]);
      start_no := StartNum(sl[i]);
     end;

-----------------------------------------------------

than you'll have ActualLine variable available in GrpNum and StartNum functions..

regards,
Kate
Avatar of lloydie-t
lloydie-t

ASKER

Thanks for that Kate. I just need to inicate that the line number is either 0 or 1. Do you think the following would do that?

sl.LoadFromFile(CdrImportEdit.Text);
    //loop though each line of text
    for i := 0 to sl.count - 1 do
    begin
{INSERTED BY KATE}
      SetActualLine(Index);
{END OF INSERTION}
      group_no := GrpNum(sl[i]);
      start_no := StartNum(sl[i]);
{INSERTED BY LLOYD}
      Inc (index)
      if Index > 1 then
      Index := 0;
{END OF INSERTION}
     end;
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was already thinking along the same lines and had already changed part of my code. Thanks for the confirmation. I suppose you can probably tell I am new to delphi so any info about the use of DLL's is helpful.
also you can put very many parameters in a procedure or function in a DLL

procedure aProc(hWnd, aNum, aRef: Integer; aPnt: TPoint;  aPchar: PChar; aRect: TRect);

you might look up   "Parameters" and  "Parameter semantics" and  "Parameter passing" and "Calling conventions"  in the delphi help, you can use the   "var, const, and out "  as types for a paraneter