Solved

passing line number to a DLL

Posted on 2004-08-14
6
160 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:lloydie-t
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:_Katka_
ID: 11803427
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
0
 

Author Comment

by:lloydie-t
ID: 11803466
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;
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 125 total points
ID: 11804686
I can not understand what you are trying to do here?

It seems that you could just add another parameter to your DLL functions, and send the line Number with the line String?


Function GrpNum (S: String; LineNum: Integer): 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
if LineNum = 0 then  // you can use the Linenum for whatever test you need, this is just for example
  Result := '0'
  else
  Result := '1';
end;


 - - - - - - - - - - - - - - - - -  - - - - - - - - -  - - -
also I wonder why you use the    stdcall;
is there a purpose in this? it seems that since you use the ShareMem this DLL can NOT be used by a C code or VB code program anyway, so why use  stdcall;

also ShareMem will complicate the use of a DLL in a program, it is better (much more efficient) if you do not use the ShareMem, and use PChar instread of strings. .


Function GrpNum (inPchar: Pchar; LineNum: Integer): PChar;
var
Str1: String;
begin
Str1 := copy(inPChar,1,1);
if  Str1 = 'G' then  begin
Str1 := copy(inPChar,2,5);
Str1 := StringReplace(Str1, ' ', '', [rfReplaceAll]); // is on line 1
end
else
if LineNum = 0 then  // you can use the Linenum for whatever test you need, this is just for example
  Str1 := '0'
  else
  Str1 := '1';
Result := PChar(Str1);
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lloydie-t
ID: 11805332
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.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11806784
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11806825
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question