Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can I Pass Record As var Argument To DLL?

Posted on 2002-07-25
4
Medium Priority
?
164 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 300 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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