当他遇上她时

dongge2000 2004-06-08 12:57:23
//Delphi 中
library MyModule;
uses
Windows, Messages, SysUtils;

type
DGFor3 = record
x: Longint;
y: Longint;
mdc: Longint;
end;

function MouseDC(): DGFor3; stdcall;
var
Cur: TPoint;
begin
GetCursorPos(Cur);
Result.mdc := WindowFromPoint(Cur);
Result.x := Cur.x;
Result.y := Cur.y;
end;

function GetTxt(hWnd:longint):PChar;stdcall;
var
nName:PChar;
Si:integer;
begin
Si:=GetWindowTextLength(hWnd)+1;
GetMem(nName,Si);
GetWindowText(hWnd,nName,Si);
result:=nName;
FreeMem(nName,0);
end;
exports
GetTxt;

begin
end.

'VB中
Private Declare Function MouseDC Lib "D:\Mian\Delphi\MyModule\MyModule.dll" () As DGFor3
Private Declare Function GetTxt Lib "D:\Mian\Delphi\MyModule\MyModule.dll" (ByVal hWnd As Long) As String
Private Sub Timer1_Timer()
text1.Text = GetTxt(MouseDC.MDC)
End Sub

type DGFor3
x as long
y as long
mdc as long
end type

以上代码遇到个别变动标题的窗体的时候会错误。
...全文
144 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
SaKura2003 2004-06-13
  • 打赏
  • 举报
回复
高手
zswangII 2004-06-08
  • 打赏
  • 举报
回复
function GetTxt(hWnd:longint):PChar;stdcall;

你返回的是字符的地址,但这个地址里的资源已经被你释放了“FreeMem(nName,0);”~~
所以会出现内存访问的错误~~
建议改成这样,内存的开辟和释放就交给调用方~~

procedure GetTxt(hWnd:longint; lpText: PChar; iSize: Integer):;stdcall;
begin
GetWindowText(hWnd, lpText, iSize);
end;

不过象这样简单的功能有必要封装在DLL吗?~!~

5,388

社区成员

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

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