WinAPI DWORD GetModuleFileName( HMODULE hModule, // handle to module to find filename for LPTSTR lpFilename, // pointer to buffer for module path DWORD nSize // size of buffer, in characters );
to chechy(简简单单的活): 我发现一般的Windows程序在入口处都会调运这个函数
chechy(简简单单的活) 老大,你写错了。 这个API不是通过窗口句柄来得到文件名的
推论1。C中Win32 PE的入口点声明: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd); 可以看出,在调用WinMain之前,就可能调用了GetModuleFileName(如果karl(多功能算术逻辑运算单元) 说的是对的话)。而在那个时候,没有任何窗口被创建。所以说,利用GetModuleFileName通过窗口句柄来得到文件名是不可能的。推论2。MSDN中没有说它的第一个参数是传入HWND类型。 推论3。从该函数的定义上来看,就知道第一个参数不是要求传入HWND。
对不起,对不起,各位,我刚才弄错了 应该是 GetModuleHandle(),我刚才看错了
UnitSystemCategorymiscellaneous routinesfunction GetModuleFileName(Module: HMODULE; Buffer: PChar; BufLen: Integer): Integer;DescriptionCall GetModuleFileName to obtain the fully qualified name of a module, given its handle. This function is not available on Windows.//窗口?Module is the handle of the module whose file name is required.Buffer is a buffer that receives the file name.BufLen is the number of bytes in Buffer.GetModuleFileName returns the number of bytes written to Buffer.
我是这样的到窗体句柄的 var hwnd: THandle; hwnd := Findwindow(pchar(listbox1.items[listbox1.itemindex]), nil); 我如何通过 hwnd得到程序的路径
DWORD GetModuleFileName( HMODULE hModule, // handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer for module path
DWORD nSize // size of buffer, in characters
);
老大,你写错了。
这个API不是通过窗口句柄来得到文件名的
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
可以看出,在调用WinMain之前,就可能调用了GetModuleFileName(如果karl(多功能算术逻辑运算单元) 说的是对的话)。而在那个时候,没有任何窗口被创建。所以说,利用GetModuleFileName通过窗口句柄来得到文件名是不可能的。推论2。MSDN中没有说它的第一个参数是传入HWND类型。
推论3。从该函数的定义上来看,就知道第一个参数不是要求传入HWND。
应该是
GetModuleHandle(),我刚才看错了
This function is not available on Windows.//窗口?Module is the handle of the module whose file name is required.Buffer is a buffer that receives the file name.BufLen is the number of bytes in Buffer.GetModuleFileName returns the number of bytes written to Buffer.
var
hwnd: THandle;
hwnd := Findwindow(pchar(listbox1.items[listbox1.itemindex]), nil);
我如何通过 hwnd得到程序的路径
得到窗口柄,得到进程的ID,
枚举进程,找到这个ID,就有进程的一些信息了,当然包括进程名称。
首先给系统拍一个快照(好象是CreateSnapshot32),这样来得到所有的进程ID,然后在对窗口(前提是该窗口应该是主窗口)发送WM_QUIT,连续发送,直到该应用程序关闭,再来做一个快照,检查哪个进程被消灭了,于是就得到了该进程ID,再来重建该进程~~HOHO~~当你得到进程ID的时候该ID也随PCB一起消灭了。