关于DLL中不能使用STRING的问题 ( 积分: 100 )

  • 主题发起人 skywater007
  • 开始时间
S

skywater007

Unregistered / Unconfirmed
GUEST, unregistred user!
to plenilune168:
谢谢了,再问一下,
如果我这个函数是在DLL中的各个单元之间调用的,可以用String 吗?因为我这个函数不给外界用的。
 
P

plenilune168

Unregistered / Unconfirmed
GUEST, unregistred user!
内部之间没影响
 
S

skywater007

Unregistered / Unconfirmed
GUEST, unregistred user!
to:plenilune168 非常感谢了,再问问你:
function TForm1.PcharSplit(In_Str_NeedSplit:pchar;Var Out_Arr_Splited:Array of Pchar):Boolean ;
Var
Pv_Int_postion,pv_Int_Rows : integer;
Pv_Str_NeedSplit : String;
BEGIN
Try
Result:=True;
Pv_Str_NeedSplit := In_Str_NeedSplit;
Pv_Int_postion := pos('|',Pv_Str_NeedSplit) ;
if Pv_Int_postion =1 then
begin
delete(Pv_Str_NeedSplit, 1,Pv_Int_postion );
Pv_Int_postion := pos('|',Pv_Str_NeedSplit);
end;
pv_Int_Rows:=0;

while ( Pv_Int_postion > 0 ) do
begin
Out_Arr_Splited[pv_Int_Rows] := Pchar(copy(Pv_Str_NeedSplit, 1, Pv_Int_postion - 1));
delete(Pv_Str_NeedSplit,1,Pv_Int_postion);
//showmessage(Out_Arr_Splited[pv_Int_Rows]);
pv_Int_Rows := pv_Int_Rows + 1;
Pv_Int_postion := pos('|',Pv_Str_NeedSplit);
end ;
Except
Result := False;
exit;
end;

end ;

为什么我调用的时候是空植呢?
我这样调用它:
procedure testf;
var
b : array of Pchar;
begin
setlength(b,10);
test(b);
showmessage(b[1]);
end;
 
P

plenilune168

Unregistered / Unconfirmed
GUEST, unregistred user!
把完整的代码贴出来吧,包括DLL工程和调用的工程,不然让我们猜我们可没有这个本事。
 
K

kofbee

Unregistered / Unconfirmed
GUEST, unregistred user!
对dll,导入,导出用PChar就可以了,里面可以用string的.再说string到pchar也比较简单的.
 
S

skywater007

Unregistered / Unconfirmed
GUEST, unregistred user!
to:plenilune168
“PcharSplit”这个函数我在applation里运行的,不用DLL;在button单击事件中调用这个函数(PcharSplit)时返回值和预期的结果不同
procedure button.click();
var
b : array of Pchar;
begin
setlength(b,10);
PcharSplit('|1|2|3|',b);
showmessage(b[1]);
end
预期结果应该是1,但是得出的是空值,为什么呢?
 
P

plenilune168

Unregistered / Unconfirmed
GUEST, unregistred user!
我试过用array of pchar是得不到预期结果的,可能是和指针有关系,具体我也搞不清楚,如果只是内部调用改为string算了
 
V

vvyang

Unregistered / Unconfirmed
GUEST, unregistred user!
To 楼主:
1、动态数组和 string 类型是不能作为 dll 函数的参数和返回值的。另外老兄,我一般不写“麻烦”的代码,你仔细看看再下结论。我的目的是扫描一次字符串,将凡是“|”的地方都变为#0,同时记录被分割字符串的第一个字符的位置:|ab|cd|e|fghi|——>#0ab#0cd#0e#0fghi#0,取字符串的时候自然在 #0 处截断。
2、你的程序为什么得不到东西,原因很简单:
“Out_Arr_Splited[pv_Int_Rows] := PChar(Copy(Pv_Str_NeedSplit......)”这句有问题:
他相当于
var s: string;
s := Copy(Pv_Str_NeedSplit, 1......);
Out_Arr_Splited[pv_Int_Rows] := PChar(s);
一个临时变量在函数生存期后就会被回收,你光把他的地址保存起来,能得到什么??
3、另外你的这个函数是有缺陷的,连续的“|”(如“|1||||2|3|”)就歇菜了。
4、我就知道这些了,你嫌麻烦问其他人吧。
 
S

skywater007

Unregistered / Unconfirmed
GUEST, unregistred user!
to :vvyang
谢谢了,请问你可以把你的方法用代码实现出来吗,让我参考一下
 
R

radtek

Unregistered / Unconfirmed
GUEST, unregistred user!
vvyang上面给出的难道是外星文字,不是代码?你仔细看没有啊?
 
V

vvyang

Unregistered / Unconfirmed
GUEST, unregistred user!
To skywater007:
7 楼、15 楼还有 27 楼的那个家伙都是我,所以,算上这次我一共回复了 4 遍。
代码、程序错误及其原因都解释的很清楚,别的无话可说。
 
L

lingsg

Unregistered / Unconfirmed
GUEST, unregistred user!
用FastMM吧,不用DLL也可以实现DLL和EXE传递字符串了,下载网址
http://fastmm.sourceforge.net
Dephi 2006里的内存管理器已经是 FastMM 了
 
Z

zqw0117

Unregistered / Unconfirmed
GUEST, unregistred user!
to Lingsg兄
我也听说2006里面就是fastmm了,可是为什么用2006创建dll的时候,还是提示带上sharemem呢?我找了下,Source里面没FastMM这样的单元。请指教。
 
T

tseug

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi 2006帮助

Shared-Memory Manager (Win32 Only)
On Win32, if a DLL exports routines that pass long strings or dynamic arrays
as parameters or function results(whether directly or nested in records or
objects), then the DLL and its client applications (or DLLs) must all use the
ShareMem unit. The same is true if one application or DLL allocates memory
with New or GetMem which is
deallocated by a call to Dispose or FreeMem in another module. ShareMem should
always be the first unit listed in
any program or library uses clause where it occurs.
ShareMem is the interface unit for the BORLANDMM.DLL memory manager, which allows modules to share
dynamically allocated memory. BORLANDMM.DLL must be deployed with applications and DLLs that use ShareMem. When an application or DLL uses ShareMem, its
memory manager is replaced by the memory manager in BORLANDMM.DLL.
 
L

lingsg

Unregistered / Unconfirmed
GUEST, unregistred user!
To: zqw0117
开始我也觉得迷惑,后来打开 GetMem.inc 一看就什么都明白了:)(里面的代码就是FastMM,作者就是:Pierre le Riche)
至于怎么用可以看Delphi自带例子:
Demos/DelphiWin32/VCLWin32/MemMgr/SimpleShareMem
在主程序和DLL的项目引用第一行加上: SimpleShareMem, 就可以了
 
顶部