Solved

Can I Pass Record As var Argument To DLL?

Posted on 2002-07-25
4
150 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

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

Title # Comments Views Activity
Intraweb submit form as a POST request 4 263
indy Tmemory stream non latin charcter issue 2 98
delphi parse string to params 3 100
SUM 2 INTEGER ARRAYS INTO 1 10 93
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

13 Experts available now in Live!

Get 1:1 Help Now