473,626 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find owner process ID?

The base process owns this thread. But the visible window is owned by
the thread.

How do I get the owner Process ID from a Thread ID?
To understand, look at this "<<--" pointer in the following code.
sample code:

using HWND = IntPtr;
public struct ProcInfo
{
public string ProcessName;
public int ProcessID;
public string ProcessFileName ;
public int ProcessInstance ;
public string ProcessWindowTi tle;
};

public static IDictionary<HWN D, ProcInfogetOpen Windows()
{
Dictionary<HWND , ProcInfodictWin dows = new Dictionary<HWND ,
ProcInfo>();

HWND hwndShellWindow = GetShellWindow( );

EnumWindows(
delegate(HWND hWnd, int lParam)
{
if (hWnd == hwndShellWindow )
return true;

if (!IsWindowVisib le(hWnd))
return true;

int lengthWindowTit le = GetWindowTextLe ngth(hWnd);
if (lengthWindowTi tle == 0)
return true;
//A process who has a window is found!!!! Great!

ProcInfo tempProc = new ProcInfo();

StringBuilder strWindowTitle = new
StringBuilder(l engthWindowTitl e);
GetWindowText(h Wnd, strWindowTitle, lengthWindowTit le + 1);
tempProc.Proces sWindowTitle = strWindowTitle. ToString();

HWND processInstance = GetWindowLong(h Wnd,
(int)Indices.GW L_HINSTANCE);
tempProc.Proces sInstance = (int)processIns tance;

HWND processID = GetWindowThread ProcessId(hWnd,
IntPtr.Zero);
tempProc.Proces sID = (int)processID;

foreach (Process proc in Process.GetProc esses())
{
if (proc.Id == (int)processID) // <<-- This never
matches :((
{
tempProc.Proces sName = proc.ProcessNam e;
tempProc.Proces sFileName =
proc.MainModule .FileName;
break;
}
}

dictWindows[hWnd] = tempProc;

return true;
},
0
);

return dictWindows;
}

delegate bool EnumWindowsProc (HWND hWnd, int lParam);

[DllImport("USER 32.DLL")]
static extern HWND GetShellWindow( );
[DllImport("USER 32.DLL")]
static extern bool IsWindowVisible (HWND hWnd);
[DllImport("USER 32.DLL")]
static extern int GetWindowText(H WND hWnd, StringBuilder lpString, int
nMaxCount);
[DllImport("USER 32.DLL")]
static extern int GetWindowTextLe ngth(HWND hWnd);
[DllImport("USER 32.DLL")]
static extern HWND GetWindowLong(H WND hWnd, int nIndex);
[DllImport("USER 32.DLL")]
static extern HWND GetWindowThread ProcessId(HWND hWnd, HWND
lpdwProcessId);

Thanks,
Nayan

Dec 9 '06 #1
4 21128
"Nayan" <cn****@gmail.c omwrote:
>The base process owns this thread. But the visible window is owned by
the thread.
How do I get the owner Process ID from a Thread ID?
HWND processID = GetWindowThread ProcessId(hWnd,
IntPtr.Zero) ;
in c++/win32 you'd write
DWORD processId;
DWORD threadId = GetWindowThread ProcessId(hwnd, &processId);

I think you got your arguments the wrong way round. You need to pass
processId as an "out" argument.

--
Lucian
Dec 9 '06 #2
Yes, you are right!!

But i m not very good in C#. How can i get the value in a variable like
this by reference??

Is this correct??
HWND processID;
HWND threadID = GetWindowThread ProcessId(hWnd, processID);
Thanks a million! :)
Nayan
On Dec 9, 3:36 am, Lucian Wischik <l...@wischik.c omwrote:
"Nayan" <cna...@gmail.c omwrote:
The base process owns this thread. But the visible window is owned by
the thread.
How do I get the owner Process ID from a Thread ID?
HWND processID = GetWindowThread ProcessId(hWnd,
IntPtr.Zero);in c++/win32 you'd write
DWORD processId;
DWORD threadId = GetWindowThread ProcessId(hwnd, &processId);

I think you got your arguments the wrong way round. You need to pass
processId as an "out" argument.

--
Lucian
Dec 9 '06 #3
"Nayan" <cn****@gmail.c omwrote:
>But i m not very good in C#. How can i get the value in a variable like
this by reference?
[DllImport("user 32.dll")] static extern UInt32
GetWindowThread ProcessId(HWND hwnd, out UInt32 processId);

....
UInt32 pid;
UInt32 tid = GetWindowThread Processid(hwnd, out pid);

--
Lucian
Dec 9 '06 #4
Ya, I got the solution at the same time :)

Thanks a million for your help, Lucian!

Regards,
Nayan

On Dec 9, 3:51 am, Lucian Wischik <l...@wischik.c omwrote:
"Nayan" <cna...@gmail.c omwrote:
But i m not very good in C#. How can i get the value in a variable like
this by reference?[DllImport("user 32.dll")] static extern UInt32
GetWindowThread ProcessId(HWND hwnd, out UInt32 processId);

...
UInt32 pid;
UInt32 tid = GetWindowThread Processid(hwnd, out pid);

--
Lucian
Dec 9 '06 #5

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

Similar topics

1
5141
by: neo | last post by:
Hi, we're having a problem with SQL 2000 and Opta 2000 JDBC driver where there is large update running and at the same time, read is blocked for a while. We're looking for a way to catch this blocking process and if it last more than 10 minutes, then email or send out a message. I know sp_lock returns all current locks but how do you know which one is blocking other processes? Thanks for your help in advance.
2
10692
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel = CreateObject("Excel.Application") objWorkBook = objExcel.Workbooks.Open(targetfilename) * targetfilename is a variable that stores the excel file name and path. In the error handler,
0
1054
by: Christoph Kunz | last post by:
Hello ! Detecting, whether a serial port exists and is availabe (not occupied by some other program / process) is quit simple. But how can I detect, which process (name of the program) occupies a specific Comport? We often have problems with customers that cannot use some software product we sell because the serial ports are not available - although
1
4954
by: Jan Jeitziner | last post by:
Hi folk, is there a possibility to unload a (DLL-) Module from its owner Process within a VB.NET Application? With other words, how can I unload at Runtime a DLL, which my Application has loaded, from the memory. I'm very thankful for your Help! Jan
1
12587
by: coroto | last post by:
I know how to use Process.GetCurrentProcess to find out the PID of the current application (vb.net) but I'd like to know the PARENT process PID somehow. I've been looking at various methods including some of the WMI stuff but inside VB.NET I'm still kind of confused about how to do it. Any ideas?
5
4054
by: su | last post by:
to find which process dumped core at the promt we give $ file core.28424 core.28424: ELF 32-bit LSB core file of 'soffice.bin' (signal 11), Intel 80386, version 1 (SYSV), from 'soffice.bin' from this command we know 'soffice.bin' process dumped core. Now can i do the same using python i.e. finding which process dumped core? if so how can i do it?
1
1434
by: M D | last post by:
I want to find out how much my php program is occupying cpu process My web server allow ssh access. I remeber ps -aux( or something similar) lists all process running at the server. Which parameter i should be looking at? Thanks in advance. MD
8
7226
by: hd95 | last post by:
How do i find a process that has a file locked and kill it? I am using VB.net
0
1315
by: gloris | last post by:
Hi, What is the best way in python find the process name and owner? Now i use WMI, but this version is too slow.
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.