发现个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.

posted @ 2017-05-15 21:05  wzjhoutai  阅读(204)  评论(0编辑  收藏  举报