Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 05-16-2018, 02:40 AM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 325
Default call dll function pansichar parameter

i have a function in a dll

function Send_Value(aValue : pAnsChar):Integer; stdcall;
var
myStr : string;
begin
...
myStr := 'Test';
System.AnsiStrings.strlcopy(aValue, pAnsiChar(AnsiString(myStr)), Length(myStr));
end;

i am loading the dll dynamically and calling that function but what do i need to do in the exe app to call that function and get the value of 'Test' back.

aValue : string;
...
aInteger := Send_Value(pAnsiChar(aValue));

What do i need to do to aValue in the calling app to allocate space for it so when i call the function i get the value 'Test' back?
  #2  
Old 05-16-2018, 09:57 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 840
Default

Contrary to the popular belief, it's perfectly fine to pass strings over dll boundary, like this(see attachment, Delphi 2007):
Code:
library CallbackDll;

uses
  SysUtils,
  Classes;

{$R *.res}

type
  PCallback = ^TCallback;
  TCallback = record
    FResult: Integer;
    FInStr: String;
    FOutStr: String;
  end;
  TOnCallback = procedure(ACallback: PCallback);

var
  OnCallback: TOnCallback = nil;
  Callback: PCallback;

procedure Send_Value;
begin
  Callback.FResult := 1000 - StrToIntDef(CallBack.FInStr, 0);
  Callback.FOutStr := 'Test ' + Callback.FInStr;
  if Assigned(OnCallback) then
    OnCallback(Callback);
end;

procedure InitCallback(AOnCallback: TOnCallback; ACallback: PCallback); stdcall;
begin
  OnCallback := AOnCallback;
  Callback := ACallback;
  Send_Value;
end;  
                 
exports
  InitCallback;

begin
end.
Code:
unit uMain;

interface

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

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

var
  fMain: TfMain;

implementation

{$R *.dfm}

type
  PCallback = ^TCallback;
  TCallback = record
    FResult: Integer;
    FInStr: String;
    FOutStr: String;
  end;

var
  Callback: PCallback;
  LibHandle: THandle;

procedure DoOnCallback(ACallback: PCallback);
begin
  ShowMessage('Callback from dll:' + sLineBreak +
              'FResult: ' + IntToStr(ACallback.FResult) + sLineBreak +
              'FOutStr: ' + ACallback.FOutStr);
end;

procedure CallDll(const ALibName, AInStr: String);
type
  TOnCallback = procedure(ACallback: PCallback);
  TInitCallback = procedure(AOnCallback: TOnCallback; ACallback: PCallback); stdcall;
var
  InitCallBack: TInitCallback;
begin
  @InitCallBack := GetProcAddress(LibHandle, PChar('InitCallback'));
  if @InitCallBack <> nil then
  begin
    CallBack^.FInStr := AInStr;
    InitCallBack(@DoOnCallback, Callback);
  end;
end;

procedure TfMain.Button1Click(Sender: TObject);
begin
  Randomize;
  CallDll('CallbackDll.dll', IntToStr(Random(1000) + 1));
end;

procedure TfMain.FormCreate(Sender: TObject);
begin
  LibHandle := LoadLibrary(PChar('CallbackDll.dll'));
  New(Callback);
end;

procedure TfMain.FormDestroy(Sender: TObject);
begin
  DisPose(Callback);
  if LibHandle > 0 then
    FreeLibrary(LibHandle)
end;

end.
Attached Files
File Type: zip CallBack.zip (54.6 KB, 1 views)

Last edited by kolbasz; 05-16-2018 at 10:01 AM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:21 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.