使用.NET和API调用获取进程ID [英] Obtain Process ID using .NET and API Calls
问题描述
Hello All,
我想知道如何在.NET中调用API函数。我收到了:
类型未处理的异常
''System.Runtime.InteropServices.MarshalDirectiveEx ception''发生在
WindowsApplication1.exe
附加信息:PInvoke限制:无法返回变体。
运行此代码时
:
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
*********************** *************************** **********
Dim objExcel作为新Excel .ApplicationClass()
Dim lngProcessID As Long
Dim lngHwnd As Long
使用objExcel
lngHwnd = .Hwnd
GetWindowThreadProcessID(lngHwnd,lngProcessID)< ----错误发生
.Quit()
结束
我不知道为什么会发生这种情况。任何想法或指导都会很高兴。
谢谢,
Frank
Hello All,
I was wondering how to call API functions within .NET. I''m getting:
An unhandled exception of type
''System.Runtime.InteropServices.MarshalDirectiveEx ception'' occurred in
WindowsApplication1.exe
Additional information: PInvoke restriction: can not return variants.
when running this code:
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
************************************************** **********
Dim objExcel As New Excel.ApplicationClass()
Dim lngProcessID As Long
Dim lngHwnd As Long
With objExcel
lngHwnd = .Hwnd
GetWindowThreadProcessID(lngHwnd, lngProcessID) <---- error occurs
.Quit()
End With
I have no clue why this is happening. Any ideas or guidance would be
greatly appreciated.
Thanks,
Frank
推荐答案
Frank,
Frank,
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
- 打开Option Strict
- 将声明更改为
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As
IntPtr,ByRef lpdwProcessID As Integer)作为整数
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
- Turn on Option Strict
- Change the declaration to
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Frank,
Frank,
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)
- 打开Option Strict
- 将声明更改为
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As
IntPtr,ByRef lpdwProcessID As Integer)作为整数
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
- Turn on Option Strict
- Change the declaration to
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Frank DeLuccia写道:
Frank DeLuccia wrote:
声明函数GetWindowThreadProcessID Lib" user32" (ByVal hwnd As
Long,ByVal lpdwProcessID As Long)
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
Long, ByVal lpdwProcessID As Long)
你错过了一个as System.UInt32
正确的定义是(对于C#):
[DllImport(" user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd,
out IntPtr lpdwProcessId);
欲了解更多信息,请参阅 www.pinvoke.net
-
问候
Jochen
你需要一个内存泄漏查找器吗?
http://www.codeproject.com/tools/leakfinder.asp
您是否需要来自服务器的每日报告?
http://sourceforge.net/projects/srvreport/
You missed an "as System.UInt32"
And the correct definition is (for C#):
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd,
out IntPtr lpdwProcessId);
For more info see www.pinvoke.net
--
Greetings
Jochen
Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
这篇关于使用.NET和API调用获取进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!