C # Gets a handle to a process based on the process name?

Source: Internet
Author: User

C # Gets a handle to a process based on the process name or how C # gets a handle to another process?

Sometimes the title name is changed dynamically, so do not use the FindWindow Method!



[StructLayout (LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public UINT dwsize;
public UINT Cntusage;
public UINT Th32processid;
Public IntPtr Th32defaultheapid;
public UINT Th32moduleid;
public UINT Cntthreads;
public UINT Th32parentprocessid;
public int pcpriclassbase;
public UINT DwFlags;


[MarshalAs (unmanagedtype.byvaltstr,sizeconst=260)]
public string Szexefile;
}


[DllImport ("KERNEL32. DLL ")]
public static extern IntPtr CreateToolhelp32Snapshot (uint flags,uint ProcessID);
[DllImport ("KERNEL32. DLL ")]
public static extern int CloseHandle (INTPTR handle);
[DllImport ("KERNEL32. DLL ")]
public static extern int Process32First (IntPtr handle,ref ProcessEntry32 PE);
[DllImport ("KERNEL32. DLL ")]
public static extern int Process32Next (IntPtr handle,ref ProcessEntry32 PE);

[DllImport ("User32.dll", entrypoint= "SendMessage")]
private static extern int SendMessage (int hwnd,int msg,int wparam,string lParam);



Public IntPtr Gethandlebyprocessname (string ProcessName)
{
List<processentry32> list=new list<processentry32> ();
IntPtr Handle=createtoolhelp32snapshot (0x2,0);
IntPtr Hh=intptr.zero;
if ((int) handle>0)
{
ProcessEntry32 pe32=new ProcessEntry32 ();
Pe32.dwsize= (UINT) marshal.sizeof (PE32);
int Bmore=process32first (Handle,ref pe32);
while (bmore==1)
{
INTPTR Temp=marshal.allochglobal ((int) pe32.dwsize);
Marshal.structuretoptr (pe32,temp,true);
ProcessEntry32 pe= (ProcessEntry32) marshal.ptrtostructure (temp,typeof (ProcessEntry32));
Marshal.freehglobal (temp);
List. ADD (PE);
if (pe.szexefile==processname)
{
bmore=2;
Hh=getcurrentwindowhandle (PE.TH32PROCESSID);
Break
}
Bmore=process32next (Handle,ref pe32);
}
}
return HH;
}
public static INTPTR Getcurrentwindowhandle (UINT proid)
{
IntPtr Ptrwnd=intptr.zero;
UINT Uipid=proid;
Object Objwnd=processwnd[uipid];
if (objwnd!=null)
{
Ptrwnd= (INTPTR) Objwnd;
if (Ptrwnd!=intptr.zero&&iswindow (Ptrwnd))//get handle from cache
{
return ptrwnd;
}
Else
{
Ptrwnd=intptr.zero;
}
}
BOOL Bresult=enumwindows (new Wndenumproc (Enumwindowsproc), uipid);
Enumeration form returns false and no error number indicates success
if (!bresult&&marshal.getlastwin32error () ==0)
{
OBJWND=PROCESSWND[UIPID];
if (objwnd!=null)
{
Ptrwnd= (INTPTR) Objwnd;
}
}
return ptrwnd;
}


private static bool Enumwindowsproc (INTPTR hwnd,uint LParam)
{
UINT Uipid=0;
if (GetParent (HWND) ==intptr.zero)
{
GetWindowThreadProcessId (Hwnd,ref uipid);
if (Uipid==lparam)//Find the process corresponding to the main form handle
{
Processwnd.add (Uipid,hwnd); Cache the handle.
SetLastError (0); Set no error
return false; Returns false to terminate the enumeration form
}
}
return true;
}


Call:

IntPtr hh=gethandlebyprocessname ("notepad.exe");
if (Hh!=intptr.zero)
{
SendMessage ((int) hh,0x000c,0, "A"); Open Notepad, send the letter A
}


C # Gets a handle to a process based on the process name?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.