473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Obtain Process ID using .NET and API Calls

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
Jul 21 '05 #1
10 4041
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)


- 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.
Jul 21 '05 #2
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,
ByVal lpdwProcessID As Long)


- 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.
Jul 21 '05 #3
Frank DeLuccia wrote:
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
Long, ByVal lpdwProcessID As Long)


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/
Jul 21 '05 #4
Frank DeLuccia wrote:
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
Long, ByVal lpdwProcessID As Long)


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/
Jul 21 '05 #5
Thanks for the response! I changed the declaration as instructed and now
I'm getting this:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred
in WindowsApplication1.exe

Additional information: Unable to find an entry point named
GetWindowThreadProcessID in DLL user32.

Here is the change:
Option Strict On

Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer

************************************************** *******

Dim objExcel As New Excel.ApplicationClass()
Dim objPtr As IntPtr
Dim intID As Integer

With objExcel
objPtr = New IntPtr(objExcel.Hwnd)
GetWindowThreadProcessID(objPtr, intID) <---- Fails
End With
Thanks again for your time,
Frank

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,ByVal lpdwProcessID As Long)


- 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.

Jul 21 '05 #6
Thanks for the response! I changed the declaration as instructed and now
I'm getting this:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred
in WindowsApplication1.exe

Additional information: Unable to find an entry point named
GetWindowThreadProcessID in DLL user32.

Here is the change:
Option Strict On

Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer

************************************************** *******

Dim objExcel As New Excel.ApplicationClass()
Dim objPtr As IntPtr
Dim intID As Integer

With objExcel
objPtr = New IntPtr(objExcel.Hwnd)
GetWindowThreadProcessID(objPtr, intID) <---- Fails
End With
Thanks again for your time,
Frank

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As Long,ByVal lpdwProcessID As Long)


- 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.

Jul 21 '05 #7
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer


The function name should en with Id, not ID. Yes, Win32 entry points
are case sensitive. Sorry I missed that before.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #8
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer


The function name should en with Id, not ID. Yes, Win32 entry points
are case sensitive. Sorry I missed that before.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #9
No need to apologize. I should have seen it.

It worked!!! Thanks for your help!!

It's greatly appreciated!!!

-Frank

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u5**************@TK2MSFTNGP12.phx.gbl...
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer


The function name should en with Id, not ID. Yes, Win32 entry points
are case sensitive. Sorry I missed that before.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 21 '05 #10
No need to apologize. I should have seen it.

It worked!!! Thanks for your help!!

It's greatly appreciated!!!

-Frank

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u5**************@TK2MSFTNGP12.phx.gbl...
Frank,
Declare Function GetWindowThreadProcessID Lib "user32" (ByVal hwnd As
IntPtr, ByRef lpdwProcessID As Integer) As Integer


The function name should en with Id, not ID. Yes, Win32 entry points
are case sensitive. Sorry I missed that before.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4....
4
by: Krzysztof Skibniewski | last post by:
Hello, I'm trying find out how to obtain and output that format ( hh:mm:ss.fff ) in my ASP. if somebody knows, pls let me know.. thx kris
8
by: Frank DeLuccia | last post by:
Hello All, Hope eveyone is having a better morning than me today. Anyway, I know the .NET framework has classes to obtain process ID's of processes running in the task manager. My question is;...
4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
3
by: envimargo | last post by:
As I can obtain the hardware installed in a computer with linux?
5
by: Jeronimo Bertran | last post by:
Hello, We have developed a socket tcpip server in .net that accepts calls from clients. Using the connected socket we are able to use the Socket.RemoteEndPoint to obtain the IP address of the...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
22
by: Zen | last post by:
Hi, My production machine has 2G of memory, when aspnet_wp.exe goes up to about ~1.2G of memory usage, I start get out-of-memory exception. Other processes don't use as much memory and I added...
2
by: ME | last post by:
How would one obtain the parameter VALUES of a method that has already run? I can find the method using the StackTrace and StackFrame classes but once I find the method I would like to obtain the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.