我在dll的projiect里面定义了
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;export;function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
var
TmpStr: string;
begin
//
end;
=====exe程序调用===========
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;external 'AnyGameCom.dll' name '_CreateCommandLine';
调用CreateCommandLine(3,pchar('2222222'));当我在dll的工程里面调试的时候,发现exe程序传递到dll的参数param是乱码啊。为什么呢?编程环境:winxp sp3 + delphi 2009 Version 12.0.3420.21218
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;export;function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
var
TmpStr: string;
begin
//
end;
=====exe程序调用===========
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;external 'AnyGameCom.dll' name '_CreateCommandLine';
调用CreateCommandLine(3,pchar('2222222'));当我在dll的工程里面调试的时候,发现exe程序传递到dll的参数param是乱码啊。为什么呢?编程环境:winxp sp3 + delphi 2009 Version 12.0.3420.21218
CreateCommandLine(3, '2222222');
或者将PChar改为string类型,然后将ShareMem单元做为DLL和EXE工程的第一个use单元,程序发布时带上borlndmm.dll。
CreateCommandLine(3, '2222222');
直接调用当然可以,我不是想封装到DLL里面嘛。
用这个DLL貌似有不好的地方,内存泄露?
To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. 为了避免使用宝兰妹妹,那么可以使用PCHAR来传递string参数嘛。为什么我的回错误啊。救命啊~~~
或者用sharemem
或者你需需要在DLL當中屏閉使用UNICODE, 可能DLL當中默認定編譯字符為UNICODE不定...
像那種調用方式, 隻會存在這兩種可能的錯誤, 不會有第三種的
dll 中library dllPro;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes,
Types,Dialogs;{$R *.res}function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
var
TmpStr: string;
begin
TmpStr := Param;
ShowMessage(TmpStr);
end;exports
CreateCommandLine;begin
end.
主程序中unit TestForm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;external 'dllPro.dll' name 'CreateCommandLine'; implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
CreateCommandLine(1,'aaa');
end;end.
var pc1: pchar;
begin
pc1 := pchar('2222222');
CreateCommandLine(3,pc1);
end;
为什么要这样做呢?
因为你的格式是传立即数,是传值的,而delphi在这里字符的处理机制是传地址的.
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;export;function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
var
TmpStr: string;
begin
TmpStr:=StrPas(Param);//----断点这里,看param就是乱码
GetMem(Result,Length(TmpStr)+1);
strpcopy(Result,TmpStr);
end;
函数名不会错的。否则也调用不了啊。我用的是D2009 啊。你呢?会不会是pchar类型在D2009里面变成了Pwidechar或者pansiChar的缘故啊。
uses 里引用sharemem即可
CreateCommandLine(Command: DWORD; Param: PChar; var aa :pChar);
主程序定义aa变量,取返回值.
var
aa :pChar;调用 DLL中的过程
CreateCommandLine(Command: DWORD; Param: PChar; var aa[0]);
这样...~~~~~
CreateCommandLine(1, 'abc', aa[0]);