发现个delphi调用vc写的Dll中包含pchar参数报错奇怪现象

发现个delphi调用vc写的Dll中包含pchar参数奇怪现象

procedure中的第一行语句不能直接调用DLL的函数,否则会运行报错,在之前任意加上条语句就不报错了奇怪!

vc的DLL源码地址 http://blog.csdn.net/lqena/article/details/46357165

Delphi源码如下:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetStr(s:PChar):PChar; stdcall; external 'MyDLL.dll';
procedure TForm1.btn1Click(Sender: TObject);
var
  p  :PChar;
  s:string;
begin
  s:='123'; //procedure中的第一行语句不能直接调用GetStr,否则会运行报错,加上这行语句不报错了奇怪 
  p:= GetStr('63024823'); //接收必须是PChar,如果String可以编译通过,但运行报错
  ShowMessage(p);
end;

end.

你可能感兴趣的:(Delphi)