转自http://hi.baidu.com/1981633/item/04c78cceb3cf563399b4983b

XP下OpenProcess( PROCESS_ALL_ACCESS...失败

在编译器下调试时可以打开进程,单独运行时不行。代码来自网上。
解决方法:

int   AddPrivilege(const   char   *Name) 

 HANDLE   hToken; 
 TOKEN_PRIVILEGES   tp; 
 LUID   Luid;

 if   (!OpenProcessToken(GetCurrentProcess(), 
  TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, 
  &hToken)) 
 { 
#ifdef   _DEBUG 
  printf( "OpenProcessToken   error.\n "); 
#endif 
  return   1; 
 }

 if   (!LookupPrivilegeValue(NULL,Name,&Luid)) 
 { 
#ifdef   _DEBUG 
  printf( "LookupPrivilegeValue   error.\n "); 
#endif 
  return   1; 
 }

 tp.PrivilegeCount   =   1; 
 tp.Privileges[0].Attributes   =   SE_PRIVILEGE_ENABLED; 
 tp.Privileges[0].Luid   =   Luid;

 if   (!AdjustTokenPrivileges(hToken, 
  0, 
  &tp, 
  sizeof(TOKEN_PRIVILEGES), 
  NULL, 
  NULL)) 
 { 
#ifdef   _DEBUG 
  printf( "AdjustTokenPrivileges   error.\n "); 
#endif 
  return   1; 
 }

 return   0; 



在程序起始处调用 AddPrivilege(SE_DEBUG_NAME);即可。

posted on 2013-05-29 02:26  ximenchuixie  阅读(649)  评论(0编辑  收藏  举报