为什么我写的dll执行到最后就报这个错误:invalid pointer operation?(20分)

  • 主题发起人 主题发起人 Boblee
  • 开始时间 开始时间
B

Boblee

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:
commfuns.dll:
library commFuns;

{ 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,
WinSock,
Registry,
Graphics,
Windows,
MemMap,
Search,
Classes;

type
TCollector=set of char;//定义一个char类型集合。

{$R *.res}

{其它的函数定义。。}

{
function Name: getSelfPCName
para:无
function:
返回本机的机器名。
Return:
type:Sting
Value:返回本机的机器名。
}
function getSelfPCName:String;stdcall;export;
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
begin
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
WSACleanup;
Result := p^.h_Name;
end;
exports
getIPByHostName index 1,
getSelfPCName index 2, //测试函数,其它函数我就不列出来了。
getWorkGroupList index 3,
getNetNeighborList index 4,
IsDBCSLeadByte index 5,
getKeyValueFromRegistry index 6,
checkKeyValueExistsInRegistry index 7,
addKeyValueToRegistry index 8,
ColorToStringEx index 9,
ColorToStringExStd index 10,
UnescapeStr index 11,
EscapeStr index 12,
keyboardInpuFilter index 13,
GetDirectoryName index 14,
FindFiles index 15,
searchAndDelCommentsEx index 16;
begin
end.


testDll.dpr:
unit testDll;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OoMisc, AdPager;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
ApdSNPPPager1: TApdSNPPPager;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function getSelfPCName:string;stdcall;external 'CommFuns.dll';
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(getSelfPCName);
end;
end.
 
你没看到上面的注释吗?
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.
 
procedure getSelfPCName(var ret:pchar);stdcall;export;
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
begin
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
WSACleanup;
Result := p^.h_Name;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
pd:pchar;
begin
getmem(pd,255);
getselfpcname(pd);
showmessage(pd);
freemem(pd)
end;

 
看到注解了,但没有很好的理解。因为第一次用DLL。:)
那现在我想知道的是:
1.string,TStrings等与String有关的,是否都要用地址传递的方式给而不能用function返回,
并且要自已管理内存, 是吗?
2.其它如自定义的类型如对象等就没有内存管理问题吗?
3.如果我传一个TStrings过去并想返回一个TStrings,但传过去之前的TStrings知道,但返回的
TStrings可能变大也可能变小,这内存怎么处理?也用getmem(pd,???)这样吗?
 
To:newyj
我用你的方式改好后,编译都能不过,出现以下错误:

更改后的:dll function:
{
function Name: getSelfPCName
para:无
function:
返回本机的机器名。
Return:
type:Sting
Value:返回本机的机器名。
}
procedure getSelfPCName(var hostName:string);stdcall;
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
begin
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
GetHostName(@s, 128);
p := GetHostByName(@s);
WSACleanup;
hostName := p^.h_Name;
end;

测试文件:

。。。。。
procedure getSelfPCName(var hostName:string);stdcall;external 'commFuns.dll';
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
pd:pchar;
begin
getmem(pd,255);
getselfpcname(pd); //51 行
showmessage(pd);
freemem(pd)
end;

出错信息:
[Error] testDll.pas(51): Types of actual and formal var parameters must be identical
 

Similar threads

I
回复
0
查看
628
import
I
I
回复
0
查看
738
import
I
I
回复
0
查看
711
import
I
I
回复
0
查看
948
import
I
后退
顶部