The main design code is as follows:
# Include <tlhelp32.h>
.......
Clistctrl m_strlist;
Cstring m_strvalue;
.......
// Obtain the owner information of the current process
Void ongetuser ()
{
// Todo: add your control notification handler code here
Updatedata ();
If (m_strvalue.isempty ())
{
Afxmessagebox (_ T ("process ID is empty! "));
Return;
}
Int nid = atol (m_strvalue );
Cstring STR;
Getprocessauth (STR, NID );
If (strpathvalid. isempty ())
{
Afxmessagebox (_ T ("Get the path failed! "));
}
Else
{
Afxmessagebox (strpathvalid );
}
}
// Read the information of the owner of the current process
Void getprocessauth (cstring strpath, long PID)
{
// Obtain the user identity of the running process. Here there is no problem for processes above 8, and 8 or 0 processes cannot be listed (8 is Win2000, and 4 is WINXP)
Sid_name_use peuse;
Handle HP;
Handle htoken;
Int isok;
Char Buf [0x400];
Char buf1 [100];
Char buf2 [100];
DWORD dwnumbytesret;
DWORD dwnumbytesret1;
HP = OpenProcess (0x400, 0, pid); // 0x400 is process_query_information
Isok = openprocesstoken (HP, 0x20008, & htoken); // What Is This 0x20008 unknown, token_query?
If (isok)
{
Isok = gettokeninformation (htoken, tokenuser, & Buf, 0x400, & dwnumbytesret );
If (isok)
{
Dwnumbytesret = 100;
Dwnumbytesret1= 100;
Isok = lookupaccountsid (null, (DWORD *) (* (DWORD *) BUF), buf1, & dwnumbytesret, buf2, & dwnumbytesret1, & peuse );
If (isok)
{
Strpath. Format ("Run Auth: % S // % s", buf2, buf1 );
Strpathvalid = strpath;
}
Closehandle (htoken );
}
}
Closehandle (HP );
}
// Obtain the current process list
Void listprocess ()
{
M_strlist.deleteallitems ();
While (m_strlist.deletecolumn (0 ));
M_strlist.modifystyle (0, lvs_report );
M_strlist.insertcolumn (0, "process ID", lvcfmt_left, 80 );
M_strlist.insertcolumn (1, "process name", lvcfmt_left, 150 );
Handle handle = createconlhelp32snapshot (th32cs_snapprocess, 0 );
Processentry32 * info = new processentry32;
Info-> dwsize = sizeof (processentry32 );
Int I = 0;
If (process32first (handle, Info ))
{
If (getlasterror () = error_no_more_files)
{
Afxmessagebox ("no more Process ");
}
Else
{
Cstring ID;
Id. Format ("% d", info-> th32processid );
M_strlist.insertitem (I, ID );
M_strlist.setitemdata (I, info-> th32processid );
Id. Format ("% s", info-> szexefile );
M_strlist.setitemtext (I, 1, ID );
I ++;
While (process32next (handle, Info )! = False)
{
Id. Format ("% 5d", info-> th32processid );
M_strlist.insertitem (I, ID );
M_strlist.setitemdata (I, info-> th32processid );
Id. Format ("% s", info-> szexefile );
M_strlist.setitemtext (I, 1, ID );
I ++;
}
}
}
Closehandle (handle );
Delete Info;
}
// Obtain the process ID of the process that clicks the list
Void cgetprocessusermfcdlg: onclicklistctrl (nmhdr * pnmhdr, lresult * presult)
{
// Todo: add your control notification handler code here
Position Pos = m_strlist.getfirstselecteditemposition ();
Int select = m_strlist.getnextselecteditem (POS );
Setdlgitemint (idc_process_id, m_strlist.getitemdata (select ));
* Presult = 0;
}