Howdy all and anyone who cares to read this. I was hoping you could help me with one small issue I am having. OK to cut a long story short...

I need to be able to search through all running and then sendmessage(hwnd, blah,blah, MIN_WINDOW);
In other words I need find notepad by name in the running processes and then get the Handle. Then send message to that handle to minimize the window or Set the Window Position.

Now my problem is that when I get the Handle using:

string pname="notepad";

foreach (Process theprocess in Myprocess)
{
if (theprocess.ProcessName == pname)
{
textBox1.Text = theprocess.Id.ToString();

//_________________________________//

AS you can see that the textbox1.text will show that the handle value() is an int. Now when I pass it to the showwindows(hwnd.......). It's not the Hexadecimal value so it doesn’t exist...It seem that when it's pulled using the Process[] method it converts it to a short type of Int. Please is there away to collect the Process.Id and the pass it to Handle name as Hex...

int handle = 0x00D7A;
postmessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);

Now this would work if a window with that PID exist...Which It did AS I used WinSpy to get the value for notepad then check it and it worked...but soon as I pass the handle via the above collected method it only pass a short version of the handle. I need to convert it to HEX to pass it. Please can you point me in the right direction...?

Cheers
Sound

Dani AI

Generated

Quick diagnosis and what fixed it for this thread: the value you were seeing in the textbox was a numeric representation, not a “shortened” handle. Process.Id is the PID, Process.Handle is a process (kernel) handle, and the property that gives the window HWND you want is MainWindowHandle — that is the correct thing to pass to window APIs. Printing a handle in decimal vs hex is just formatting (ToString("X") etc.). (learn.microsoft.com)

Why MainWindowHandle can look wrong (or be zero) and how to reliably find the HWND: a process only has a main window if it has a visible top-level GUI; the value can be zero when the window isn’t created yet or is hidden. If you start the process yourself, let it finish initialization (WaitForInputIdle) or call Refresh on the Process object before reading MainWindowHandle. If that still returns zero, enumerate top-level windows and use GetWindowThreadProcessId to match windows back to the process id. (learn.microsoft.com)

Practical cautions and a short checklist (ties back to and ): use the process’s main-window handle (not the process handle), declare P/Invoke signatures with IntPtr (don’t cast HWND to int — it truncates on x64), and then either call ShowWindow or post/send WM_SYSCOMMAND with SC_MINIMIZE. Message sending is subject to UIPI and process/session differences, so prefer ShowWindow/ShowWindowAsync when possible. If MainWindowHandle is missing, Refresh/WaitForInputIdle or enumerate windows as above. (learn.microsoft.com)

Short checklist to apply immediately:

  • use MainWindowHandle (not Id or process.Handle);
  • if zero, call Refresh or WaitForInputIdle, then re-check;
  • fallback: EnumWindows + GetWindowThreadProcessId to find HWND;
  • use IntPtr in P/Invoke and call ShowWindow or SendMessage(PostMessage) to minimize.

Recommended Answers

All 6 Replies

Why not just use its handle?

string pname="notepad";

foreach (Process theprocess in Myprocess)
{
if (theprocess.ProcessName == pname)
{
postmessage( theprocess.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
break;
}
}

Thanks jerry for your niffty response....but It seems that the value is still being passed as (short or not HEX) it will only pass a int value.... the value (theprocess.Handle) outputs a small in (1140) this is not eh Real HAndle name it should be 003B0510..any idea to convert the theprocess.handle to a hex and passit or add 0x0 value to the start...?

Well, you can see what happens when you turn the int into a hex.

int x = 123;
string hex = "0x" + x.ToString("X");

Well, you can see what happens when you turn the int into a hex.

int x = 123;
string hex = "0x" + x.ToString("X");

Thankyou so much for replying...I didn't have much faith in the old forums run for help scenario...

Here ther basic Code for what I need....The basic Premiss is that when the prog is run, it checks for given program names as varible in running process list then gets the Handle using this method

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // window handle
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags); // window positioning flags

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
int clicked = 0;
string pname = "notepad";
int whandle;
string whandled;
void checkproc(string pname)
{
int SW_SHOWNOACTIVATE = 4;
int HWND_TOPMOST = -1;
uint SWP_NOACTIVATE = 0x0010;
clicked++;


Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == pname)
{
// int dothiswin=0x0104A;
ShowWindow(theprocess.Handle, SW_SHOWNOACTIVATE);
MessageBox.Show(theprocess.Handle.ToString());
SetWindowPos(theprocess.Handle.ToInt32(), HWND_TOPMOST, 100, 100, 100, 100, SWP_NOACTIVATE);

break;
}
}

}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = whandle.ToString();
checkproc(pname);
}

this is a quick idea of what i want to happen but setWindowPos(THEPROCESS.HANDLE..... is the problem.....Any Idea....Thanks for your time......BTW

Yes, because you must use the window handle of the Application:

theprocess.MainWindowHandle

use that one...

Jerry thankvery much for spending the time to try and work this crap out...I really do appreciate it....Hope to do business again...lol..

_r0ckbaer - THANKS - THANKS SO MUCH FOR typing such a quick response and solving it in one statement I can't believe i spent a whole day trying everything including writing about 10 differnt version...and never even noticed the MainWindowHandle.....ARGGGG...IS all can Say....But ONCE Karma +10 to all.. and R

BTW R0ckbaer Nice Tounge...You could rest a Beer can on that handy....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.