Solved

Can I Pass Record As var Argument To DLL?

Posted on 2002-07-25
4
157 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

624 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