The main design code is as follows:
#include
.......
CListCtrl m_strlist; cstring m_strvalue;
.......
/ / Get information about the owner 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) {// User identity of the running process, there is no problem with 8 or more processes, no problem with the 8, 0 process (8 is Win2000, 4) SID_NAME_USE PEUSE; Handle Hp; 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); // This 0x20008 doesn't know what, 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);
/ / Get the current list of processes
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 = CreateToolhelp32Snapshot (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;
// Get the process identity of the process of clicking 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;}