Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

The results from DLL in Delphi where the parameters are PAnsiChar

i've got a small problem. In my DLL i've got:

uses
  ShareMem,
  SysUtils,
  Classes,
  Dialogs;

function My_func (Param1, Param2, Param3: PAnsiChar) : Integer;

var
  s1,s2,s3 : string;

begin

     s1 := string(Param1);
     s2 := string(Param2);
     s3 := string(Param3);
     ShowMessage(s1);
     ShowMessage(s2);
     ShowMessage(s3);

     My_func := 0;
end;

And the call from my TestUnit.pas

unit Utestunit;

interface

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

procedure Button1Click(Sender: TObject);
type
  TMy_func = function (Param1, Param2, Param3: PAnsiChar) : Integer; StdCall;
var
  my_func : TMyfunc;

  error_code:integer;
  My_library : THandle;
  the_end : Boolean;
  path_library,
  path_library_full : string;

  test1,
  test2,
  test3 : array [0..255] of AnsiChar;
begin
  // open the DLL
  path_library := ExtractFilePath(Application.ExeName)+'DLLRS_DLL.dll';
  path_library_full := ExtractFilePath(Application.ExeName)+'DLL';
  SetCurrentDir(path_library_full);
  try
    the_end := False;
    My_library := SafeLoadLibrary(PChar(path_library));
    if My_library > 32 then
    begin
        @My_func := GetProcAddress(My_library, PChar('My_func'));
        if @My_func = nil then
        begin
          ShowMessage('There is no library in '+path_library);
          the_end := True;
        end;
    end
    else
    begin
      error_code := GetLastError;
      ShowMessage('B??d biblioteki '+ sciezka_biblioteki+' nr:'+IntToStr(error_code));
      the_end := True;
    end;

    if not the_end then
    begin
      // the calling

      test1 := 'My string nr 1';
      test2 := 'My string nr 2';
      test3 := 'My string nr 3';

      kod_bledu := My_func(
          test1,
          test2,
          test3
      );      
    end;

  finally
    FreeLibrary(My_library);
    SetCurrentDir(ExtractFilePath(Application.ExeName))
  end;
end;

The results are:

My string nr 1

trash

trash

as the messages from my DLL.

Why only the first result is good while the rest trash is? It's only the test in my app.

question from:https://stackoverflow.com/questions/65643673/the-results-from-dll-in-delphi-where-the-parameters-are-pansichar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a lot of very bad habits in this code, that I don't want to get into because it will take so much effort.

However, the reason for the behaviour is that you did not declare the function in the DLL as stdcall. Fix that and the text will be passed correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...