Solved

Can I Pass Record As var Argument To DLL?

Posted on 2002-07-25
4
149 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
Comment Utility
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
Comment Utility
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
Comment Utility
  Thanks for your kindness... Also my thanks go to Robert for his useful comments....
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
Cheers.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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