Solved

Can I Pass Record As var Argument To DLL?

Posted on 2002-07-25
4
154 Views
Last Modified: 2012-05-04
       I want to write a DLL contain specified function that return result in Delphi Record Variable for example

//Define Record
        TDLLParams = Record
        (strParam1 : PChar,
         intDbl1   : integer);
...
var
        recDLLParams = TDLLParams;

Assume that function name : dllFunct(dllparams : TDLLParams):Boolean;

then I want to pass recDLLParams to dllFunct and let it internally process result and return to me....
        All the problems is that I don't know how to pass it to function, I mean I'm not sure if it's work....
        Show me an example, I'll give you 100 points. and Thanks
   
0
Comment
Question by:DOOM_DAEMON
  • 2
4 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7179384
Better make that

TDllParams = packed record
  strParam1: array [0..WHATEVEERMAX] of Char;
  intDbl1: Integer;
  ...
end;

This will avoid problems with allocation.

function dllFunct(var dllparams: TDLLParams): Boolean; stdcall;

Better use stdcall so the DLL is callable from other languages as well.
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 7179388
DLL code:

library test;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes;

{$R *.res}
Type
  TDLLParams = Record
  strParam1 : PChar;
  intDbl1   : integer;
end;
Function dllFunct(var dllparams : TDLLParams):Boolean;stdcall;
begin
 DllParams.intDbl1:=100; //sample returns
 DllParams.strParam1:='DrDelphi was here';
 Result:=True;
end;




Exports dllFunct;

begin
end.





Application code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

Type
  TDLLParams = Record
    strParam1 : PChar;
    intDbl1   : integer;
 end;


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Dparams:TDLLParams;
implementation

function dllFunct(var dllparams : TDLLParams):Boolean;stdcall; external 'test.dll';
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
   if DllFunct(dparams) then
   begin
     showmessage(Dparams.strParam1);
     Showmessage(inttostr(Dparams.intDbl1));
   end;

end;

end.





Good luck!!
0
 
LVL 1

Author Comment

by:DOOM_DAEMON
ID: 7192521
  Thanks for your kindness... Also my thanks go to Robert for his useful comments....
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7193408
Cheers.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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