我有个winAPI函数问题,非常困惑。高手请进来看看!!!!!!!!!!!!!!

ldyhai 2003-07-27 09:42:10
我编了个函数想要从应用的窗口标题得到应用程序对应文件(包括路径)。
这里是我的两个API函数的声明:
function GetWindowThreadProcessId(var hwnd :cardinal; lpdwProcessId :cardinal):cardinal;stdcall;external 'user32.dll';
function GetModuleFileName(var hModule :Cardinal; var lpFileName :String; var nSize :cardinal):cardinal;stdcall; external 'kernel32.dll' name 'GetModuleFileNameA';
这里是我的主程序:
procedure TForm1.Button1Click(Sender: TObject);
var
FileName:str;
windowId:THandle;
proId,threadId:cardinal;
maxlen:integer;
begin
windowId :=Findwindow(nil,'Form1');
threadId :=GetWindowThreadProcessId(windowId,proId);
maxlen :=max_path;
num :=GetModuleFileName(proId,FileName,maxlen);
end;

但不知道为什么,FileName中始终都是空!很困惑??????????
...全文
49 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
taozc 2003-07-29
  • 打赏
  • 举报
回复
同意楼上,我来晚一步。
Eastunfail 2003-07-29
  • 打赏
  • 举报
回复
你应该先分配空间。或者直接使用ShortString
procedure TForm1.Button1Click(Sender: TObject);
var
FileName:ShortString;
windowId:THandle;
proId: hModule;
threadId:cardinal;
maxlen:integer;
num: DWORD;
begin
windowId :=Findwindow(nil,'Form1');
threadId :=GetWindowThreadProcessId(windowId,proId);
if proId <> nil then
begin
maxlen :=max_path;
num :=GetModuleFileName(proId,@FileName ,255);
showMessage( FileName );
end;
end;
chenjunccnu 2003-07-29
  • 打赏
  • 举报
回复
代码修改后,filename 有值,但出现的是烂码,我也不知道为什么

unit Unit1;

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;

const
max_path = 255;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
FileName:pchar ;
windowId:THandle;
proId: hModule;
threadId:cardinal;
maxlen:integer;
num: DWORD;
begin
windowId :=Findwindow(nil,'Form1');
threadId :=GetWindowThreadProcessId(windowId,proId);
if proId <> null then
begin
maxlen :=max_path;
num :=GetModuleFileName(proId,Pchar(FileName),maxlen);
showMessage(string(FileName));
end;
end;

end.



ldyhai 2003-07-28
  • 打赏
  • 举报
回复
str就是string;
在windows.pas中定义的那几个函数的名字分别是什么?
Eastunfail 2003-07-27
  • 打赏
  • 举报
回复
那几个API已经在windows.pas中定义了.没有必要重新定义.而且你这样定义的也不好.没有为FileName申请足够的空间,
你应该使用ShortString,
Eastunfail 2003-07-27
  • 打赏
  • 举报
回复
你的这个str是?????

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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