调用api的问题

亲密数 2003-11-05 12:32:33
我把一个c++编写的程序改成delphi的,在调用api的时候出现问题
GetClassName(hWnd,ClassName,MAX_PATH);//得到窗口类名
GetWindowText(hWnd,TitleName,MAX_PATH);//得到窗口的标题名
执行完这两个函数,ClassName,TitleName用showMessage()显示是乱码,谁能知道一下,非常感谢.其中ClassName,TitleName:array[0..MAX_PATH-1] OF CHAR;
...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
karach 2003-11-05
  • 打赏
  • 举报
回复
我看不出什么问题,是不是其他地方的问题
lvloj 2003-11-05
  • 打赏
  • 举报
回复
function GetClassNameFromHandle(const hWnd: HWND): string;
var
ClassName: string;
begin
SetLength(ClassName, 255);
GetClassName(hWnd, PChar(ClassName), 255);
Result := StrPas(PChar(ClassName));
end;
aiirii 2003-11-05
  • 打赏
  • 举报
回复
感覺不出問題啊, 那你可在 GET前,用
ZeroMemory試試!!

在我的程序我再測試下, 正常顯示:
procedure TForm1.Button1Click(Sender: TObject);
var ClassName,TitleName:array[0..MAX_PATH-1] OF CHAR;
begin
GetClassName(handle,ClassName,MAX_PATH);
showmessage(StrPas(className));
end;
亲密数 2003-11-05
  • 打赏
  • 举报
回复
感谢大家,我找到问题出在那里了。我把 EnumWndProc的声明后面加上 stdcall,然后就没有这个问题了。与调api函数没有关系,EnumWindowProc函数是要由EnumWindows函数来调用的。我只知道加上 stdcall问题解决了,但不知道是什么原因
naughtyboy 2003-11-05
  • 打赏
  • 举报
回复
GetClassName(hWnd,@ClassName,MAX_PATH);//得到窗口类名
GetWindowText(hWnd,@TitleName,MAX_PATH);//得到窗口的标题名
亲密数 2003-11-05
  • 打赏
  • 举报
回复
//这是源代码,有问题的地方在FindChildWnd( hwnd:HWND;classTree,titleTree:pchar);里
//整个程序是用来枚举窗口树,和标题树的,盼指点

unit UEnumWindow;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UDealCommonProcess;

const
MAX_PROCESS_COUNT = 100;
MAX_WND_COUNT = 1024;

Type
TProcessPhoto = TProcessInfo;
TProcessPhotoArray = array[0..MAX_PROCESS_COUNT] OF TProcessPhoto;
TWindowsPhoto = record
hWnd:HWND;
TitleTree,classTree:TszFileName;
end;
TWindowsPhotoArray = array[0..MAX_WND_COUNT] OF TWindowsPhoto;

function ListProcess():integer;
function GetProcPathByWnd( hwnd:HWND;var path:Pchar):DWORD;
procedure FindChildWnd( hwnd:HWND;classTree,titleTree:pchar);
function EnumWndProc(hwnd:HWND;lParam:LPARAM): BOOL; //call back function

var
gIdxProcPhoto :integer = 0; //当前进程快照索引
gIdxWindPhoto :integer = 0; //当前窗口快照索引
gProcPhoto:TProcessPhotoArray; //当前进程快照
gWindPhoto:TWindowsPhotoArray; //当前窗口快照

implementation

//得到当前系统的进程快照
function ListProcess():integer;
begin
showMessage('listProcess()');//debug
gIdxProcPhoto := QueryProcessInfo( gProcPhoto,MAX_PROCESS_COUNT );
result := gIdxProcPhoto;
end;

//得到指定窗口进程与pid
function GetProcPathByWnd( hwnd:HWND;var path:Pchar):DWORD;
var
pid : dword;
i : integer;
begin
showMessage('GetProcPathByWnd'+intToStr(hwnd) );//debug
pid := 0;
GetWindowThreadProcessId( hwnd,pid );
showMessage( intToStr(pid) +path);//debug
for i:=0 to gIdxProcPhoto-1 do
begin
if (pid = gProcPhoto[i].pid) then
begin
strcopy(path,gProcPhoto[i].path);
result := pid;
exit;
end;
end;
result := pid;
end;

//枚举子窗口过程
procedure FindChildWnd( hwnd:HWND;classTree,titleTree:pchar);
var
hChildWnd:cardinal;
newClassTree,className,
newTitleTree,titleName:array[0..MAX_PATH-1] OF CHAR;

begin
//showMessage('findChildWnd'+intToStr(hwnd) );//debug
if ( strLen(classTree) > MAX_PATH - 40 ) then exit;
if ( strLen(titleTree) > MAX_PATH - 40 ) then exit;
if ( gIdxWindPhoto >= MAX_WND_COUNT ) then exit;


GetClassName( hwnd,ClassName,MAX_PATH);
GetWindowText( hwnd,TitleName,MAX_PATH );
showMessage(StrPas(ClassName));//debug
StrCat( newClassTree, classTree);
StrCat( newClassTree, '\\' );
StrCat( newClassTree, ClassName);

StrCat( newTitleTree, titleTree );
StrCat( newTitleTree, '\\' );
StrCat( newTitleTree, titleName );
{StrLFmt(@newClassTree,MAX_PATH,'%s\\%.40s',[@classTree,@className]);
StrLFmt(@newTitleTree,MAX_PATH,'%s\\%.40s',[@titleTree,@titleName]);}
//@newClassTree := PChar(String(@classTree) + '\\'+ String(@className));
//newTitleTree := @titleTree + '\\'+ @titleName;
hChildWnd := GetWindow( hwnd,GW_CHILD );
if ( hChildWnd<>0 ) then
begin
repeat
FindChildWnd( hChildWnd,newClassTree,newTitleTree);
hChildWnd := GetWindow( hChildWnd,GW_HWNDNEXT);
until hChildWnd = 0;
end
else //纪录最深层次的Tree
begin
if ( gIdxWindPhoto <> 0 )
and ( Stricomp( gwindPhoto[gIdxWindPhoto-1].classTree, newClassTree) = 0)
and ( Stricomp( gwindPhoto[gIdxWindPhoto-1].TitleTree, newTitleTree ) =0 ) then
begin
exit;
end;
gWindPhoto[gIdxWindPhoto].hWnd := hWnd;
strcopy( gWindPhoto[gIdxWindPhoto].classTree,newClassTree) ;
strCopy( gWindPhoto[gIdxWindPhoto].titleTree,newTitleTree) ;
inc(gIdxWindPhoto);
end;
end;

function EnumWndProc(hwnd:HWND;lParam:LPARAM): BOOL; //call back function
VAR
ClassName:array[0..MAX_PATH-1] of char;
begin
//showMessage('EnumWndProc'+intTostr(hwnd) );//debug
//zeromemory(@ClassName,MAX_PATH);
//GetClassName( hwnd,ClassName,MAX_PATH);
//showMessage( 'in enumWndProc'+ClassName );//debug
FindChildWnd( hwnd,pchar(''),pchar(''));
result := true;
end;

end.

1,183

社区成员

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

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