Solved

passing line number to a DLL

Posted on 2004-08-14
6
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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