获得当前进程所有者的信息

关键字 进程所有者
原作者姓名 taishan_sword
正文
主要设计代码如下:
#include <tlhelp32.h>

.......
CListCtrl m_strList;
CString m_strValue;
.......
//获取当前进程的所有者的信息
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);
}
}
//读取当前进程的所有者的信息
void GetProcessAuth(CString strPath,long pid)
{
//获得运行进程的用户身份,此处对于8以上的进程没问题,对于8,0进程无法列出(8是Win2000下的,WinXP下为4)
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);//这个0x20008不知道什么,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);
}
//获取当前的进程列表
void ListProcess()
{
m_strList.DeleteAllItems();
while(m_strList.DeleteColumn(0));
m_strList.ModifyStyle(0,LVS_REPORT);
m_strList.InsertColumn(0,"进程ID",LVCFMT_LEFT,80);
m_strList.InsertColumn(1,"进程名称",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;
}
//获取点击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;
}

 » 相关连接:
如何使一个程序同时只能运行一个进 如何避免产生僵死进程zombie proce 利用mktemp和fcntl实现进程间互斥 Linux环境进程间通信——信号
多进程编程 如何启动另一程序的执行exec 信号 互斥锁、条件变量
互斥锁的使用 创建线程 线程、全局变量、线程参数 POSIX线程编程指南
 » 本栏目最新帖:
 » 精华帖:

Powered by PHPWind v6.0 Code © 2003-08