我用Delphi编写的DLL函数如下,在VC++中调用出现乱码,请问我要如何编写,在VC++中调用才不会出现乱码.function Init(pszCom: Pointer; out pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
function Close(pszCom: Pointer; out pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
function Write(InBuff: Pointer; out OutBuff, pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
function Read(InBuff: Pointer; out OutBuff, pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
****************************************************************************
这种我也试过了,不行,我要传出的是字符串指针,我要如何做,请高手指教.
function Init(pszCom: PChar; out pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Close(pszCom: PChar; out pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Write(InBuff: PChar; out OutBuff, pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Read(InBuff: PChar; out OutBuff, pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Close(pszCom: Pointer; out pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
function Write(InBuff: Pointer; out OutBuff, pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
function Read(InBuff: Pointer; out OutBuff, pszRcCode: Pointer): Integer; stdcall; external 'Zhou.dll';
****************************************************************************
这种我也试过了,不行,我要传出的是字符串指针,我要如何做,请高手指教.
function Init(pszCom: PChar; out pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Close(pszCom: PChar; out pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Write(InBuff: PChar; out OutBuff, pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
function Read(InBuff: PChar; out OutBuff, pszRcCode: PChar): Integer; stdcall; external 'Zhou.dll';
var i: Integer;
File01Info, File02Info, AllFileInfo: string;
ErrCode: Integer;
// pErrCode,pAllFileInfo: PChar;
begin
Result := 1;
AllFileInfo := '';
if ReadInfo01(File01Info, ErrCode) = 0 then AllFileInfo := ',,' + Copy(File01Info, 17, 2) + ',,' + Copy(File01Info, 7, 10) + ',' + Copy(File01Info, 25, 4) + ',,,,,' + Copy(File01Info, 29, 6) + ',' + Copy(File01Info, 35, 6) + ',' + Copy(File01Info, 41, 6) + ','
else begin
OutBuff:=pointer(AllFileInfo);
pszRcCode := pointer(Trim(ChangeErrCode(ErrCode)));
Exit;
end;
if ReadInfo02(File02Info, ErrCode) = 0 then AllFileInfo := AllFileInfo + Copy(File02Info, 7, 6) + ',' + Copy(File02Info, 13, 6) + ',' + Copy(File02Info, 19, 6) + ',' + Copy(File02Info, 25, 6) + ',,,'
else begin
OutBuff:=pointer(AllFileInfo);
pszRcCode := pointer(Trim(ChangeErrCode(ErrCode)));
Exit;
end;
OutBuff:=pointer(AllFileInfo);
pszRcCode := pointer(Trim(ChangeErrCode(ErrCode)));
Result := 0;
end;我用Delphi调用是没问题的,还有一些过程没有贴上,PChar类型的只是把Poister换成PChar就行了
问题:对象TDllNumber在objdll.dll中定义:objdll.dll输出一个函数:CreateObject
type
TDllNumber=class
public
function GetOk:string;virtual;abstract;
end;
TDllNumber1=class(TDllNumber)
public
constructor create;
function GetOk:string;override;
end;
function CreateObject:TDllNumber;export;implementationconstructor TDllNumber1.create;
begin
inherited create;
end;function TDllNumber1.GetOk: string;
begin
result:='ok';
end;
function CreateObject:TDllNumber;
begin
Result:=TDllNumber1.Create;
end;
在程序中调用对象:
typeTDllNumber=class
public
function GetOk:string;virtual;abstract;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function CreateObject:TDllNumber;stdcall;external 'objdll.dll';
implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
Ok:string;
TestObj:TDllNumber;
begin
TestObj:=CreateObject;
Ok:=TestObj.GetOk;
ShowMessage(ok);
TestObj.Free;
end;当程序运行到button1click最后是包错:
Invalid pointer Operation!!!
但是为integer,pchar不报错!!!!
总结:
1。 在dll中定义返回string字符串的函数时,如果没有特殊处理调用该函数
在退出调用该函数的过程(函数/事件)时,会出现以下错误:
Invalid Pointer Operator!2。当在单元中添加ShareMem时,在退出程序的时候会出现同样的错误3。当在调用dll的项目文件中(不是在单元文件)添加ShareMem时,能够正常
运行.4. String类型是Borland自定的特殊类型,使用起来真是无比方便,
但是,它需要borladmm.dll的支持,在动态链接库中想使用STRING类型
要经过特殊的手段,因此还是使用PCHAR类型吧。PCHAR也挺方便的。
另外如果没有特殊处理的话: WideString-可以
AnsiString-不可以