无法从 GetProcessId(.. hWnd) (pInvoke) 中提取 processID
我使用以下方法
[DllImport("kernel32.dll", SetLastError=true)] 静态 extern int GetProcessId(IntPtr hWnd);
尝试获取正在运行的进程的 processId,我拥有的唯一信息是 HWND。 我的问题是它总是返回错误代码 6,即 ERROR_INVALID_HANDLE。 我想我可以将参数更改为 int 类型,但这也不起作用。 我无法枚举正在运行的进程,因为任一时间可能有超过 1 个实例在运行。
谁能看看我是否做错了什么?
注意:该进程是从暴露给框架的自动化对象生成的,并且仅提供 HWND 属性。 也许还有另一种方法来获取 processID,因为我编写的代码首先负责运行它?
我的代码看起来与此类似......
自动化应用程序.应用程序 extApp = 新 AutomationApplication.Application(); extApp.Run(); ...
im using the following method
[DllImport("kernel32.dll", SetLastError=true)] static extern int GetProcessId(IntPtr hWnd);
to try and get the processId for a running process and the only information I have is the HWND. My problem is that it is always returning error code 6 which is ERROR_INVALID_HANDLE. I thought i might change the parameter to be of type int but that also didnt work. Im not able to enumerate the running processes because there may be more than 1 instance running at any one time.
Can anyone see if i am doing anything wrong?
NB: The process is spawned from an automation object exposed to the framework and only provides the HWND property. Perhaps there is another way to get the processID seeing as the code i wrote was responsible for running it in the first place?
My code looks something similar to this...
AutomationApplication.Application
extApp = new
AutomationApplication.Application();
extApp.Run(); ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GetProcessId 在给定进程句柄而不是窗口句柄时获取进程 ID 。 实际上:
如果您有窗口句柄,那么您需要 GetWindowThreadProcessId 函数:
返回线程 ID,并将进程 ID 放入输出参数中。
GetProcessId gets the process ID when given a process handle, not a window handle. It's actually:
If you've got a window handle, then you want the GetWindowThreadProcessId function:
This returns the thread id, and puts the process id in the out-param.
什么是“AutomationApplication.Application”类? 那是你写的吗? .Run() 返回 PID 吗?
What is the 'AutomationApplication.Application' class? Did you write that? Does .Run() return a PID?
请参阅 Pinvoke 上的示例,无需
WIN32 调用,因为您可以使用托管 API:
See an example on Pinvoke, no need for the
WIN32
call, as you can use a managed API :