473,572 Members | 3,074 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 21123
"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
5140
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...
2
10689
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...
0
1052
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...
1
4951
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
12575
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
4050
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...
1
1430
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
7223
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
1307
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
7733
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...
0
7957
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
7705
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
8000
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...
1
5524
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...
0
3685
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...
1
2138
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
1240
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
975
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.