DLL的函数用到string的问题

lovefox_zoe 2009-07-24 10:09:06
我在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
...全文
218 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyljruby775 2009-08-09
  • 打赏
  • 举报
回复
打错.
这样...~~~~~
CreateCommandLine(1, 'abc', aa[0]);
zyljruby775 2009-08-09
  • 打赏
  • 举报
回复
其实那个变成过程, 不用函数也行的

dll中过程定义
CreateCommandLine(Command: DWORD; Param: PChar; var aa :pChar);
主程序定义aa变量,取返回值.
var
aa :pChar;

调用 DLL中的过程

CreateCommandLine(Command: DWORD; Param: PChar; var aa[0]);

iamduo 2009-08-08
  • 打赏
  • 举报
回复
还有一招。Variant
ye091032 2009-08-08
  • 打赏
  • 举报
回复

uses 里引用sharemem即可
newfang 2009-08-08
  • 打赏
  • 举报
回复
19楼说的可以试试……
lovefox_zoe 2009-08-08
  • 打赏
  • 举报
回复
哪个大哥愿意QQ在线辅导解决。送上100分。
lovefox_zoe 2009-08-08
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 starluck 的回复:]
都用PAnsiChar  先。
[/Quote]

没用啊。哎。绝望了。
starluck 2009-08-06
  • 打赏
  • 举报
回复
都用PAnsiChar 先。
lovefox_zoe 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 i_mimi 的回复:]
试了一下没有问题,可能楼主函数名字写错了代码如下
[/Quote]

函数名不会错的。否则也调用不了啊。我用的是D2009 啊。你呢?会不会是pchar类型在D2009里面变成了Pwidechar或者pansiChar的缘故啊。
lovefox_zoe 2009-08-06
  • 打赏
  • 举报
回复

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;

simon_reg 2009-07-28
  • 打赏
  • 举报
回复
调用的方法改为用变量就可以了.
var pc1: pchar;
begin
pc1 := pchar('2222222');
CreateCommandLine(3,pc1);
end;
为什么要这样做呢?
因为你的格式是传立即数,是传值的,而delphi在这里字符的处理机制是传地址的.
wxsan 2009-07-28
  • 打赏
  • 举报
回复
widestring类型应该也可以的,我就这么用。
wxsan 2009-07-27
  • 打赏
  • 举报
回复
同意用widestring类型,以前这样用过。
hujias 2009-07-27
  • 打赏
  • 举报
回复
边灌水 边学习 ∧∧
米的向日葵 2009-07-27
  • 打赏
  • 举报
回复
试了一下没有问题,可能楼主函数名字写错了代码如下
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;

interface

uses
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.
Harryfin 2009-07-27
  • 打赏
  • 举报
回复
D7直接传不是不会出错,会不会出错关键在于看谁释放这个字符串,
dfasri 2009-07-27
  • 打赏
  • 举报
回复
DELPHI 2009不知道是不是改過處理, DELPHI7裡面, 直接傳是不會有問題的, 但是你返回的PChar就有可能有問題.
或者你需需要在DLL當中屏閉使用UNICODE, 可能DLL當中默認定編譯字符為UNICODE不定...
像那種調用方式, 隻會存在這兩種可能的錯誤, 不會有第三種的
xisiyong 2009-07-26
  • 打赏
  • 举报
回复
用WideString就不会出错...
newfang 2009-07-25
  • 打赏
  • 举报
回复
是不是Freemem的太早了,你不要把PChar设成局部变量,设置成全局试试,在窗口关闭的时候再Freemem
Harryfin 2009-07-25
  • 打赏
  • 举报
回复
声明个变量,然后GETMEM,赋值,传送,再FREEMEM看看
加载更多回复(9)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧