Solved

passing line number to a DLL

Posted on 2004-08-14
6
156 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 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 33

Expert Comment

by:Slick812
ID: 11806784
0
 
LVL 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

15 Experts available now in Live!

Get 1:1 Help Now