473,513 Members | 3,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4044
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
4971
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. forkme.pl calls the process creation script (createme.pl) 5. createme.pl creates my notepad.exe process, but no window shows up on my PC. The...
4
5511
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
1790
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; Does .NET have anything in it's bag of tricks to give me the process ID of an instance I create through code? Example: Dim objExcel as...
4
2016
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 name? Thanks, GP.
77
4505
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 process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a...
3
1268
by: envimargo | last post by:
As I can obtain the hardware installed in a computer with linux?
5
6653
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 client. Is there any way to obtain the MAC address for the client? Thanks Jeronimo
6
3170
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 list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to...
22
2984
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 all the peak memory usage of all the processes (including aspnet_wp.exe), it goes up to no more than 1.5. How is that possible? Would anyone know...
2
2465
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 value of one of the parameters that were passed to the method. Is this possible? Even if I have to use PInvoke to do so? I can currently find...
0
7178
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7125
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.