Solved

passing line number to a DLL

Posted on 2004-08-14
6
155 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_
Comment Utility
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
Comment Utility
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 33

Accepted Solution

by:
Slick812 earned 125 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:lloydie-t
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now