一些常用函数


//获得父进程ID,失败则返回0
DWORD   GetPPid()  
{          
    HANDLE            hProcessSnap  
=   NULL;    
    PROCESSENTRY32    pe32   
=    {0};
    DWORD dwPID
= GetCurrentProcessId();
   
    hProcessSnap  
=   CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,   0);    
   
   
if(hProcessSnap==(HANDLE)-1)    
       
return   (FALSE);    

    pe32.dwSize  
=   sizeof(PROCESSENTRY32);    
   
   
if   (Process32First(hProcessSnap,   &pe32))    
    {  
       
while(pe32.th32ProcessID!=dwPID)  
            Process32Next(hProcessSnap,  
&pe32);  
    }
   
    CloseHandle(hProcessSnap);
   
   
return(pe32.th32ParentProcessID);  
}

//判断程序是否以服务运行
BOOL isRuninServices()
{
    DWORD dwPPID
= GetPPid();

    HANDLE hProcessSnap  
=   NULL;    
    PROCESSENTRY32   pe32 
=   {0};    
 
    hProcessSnap  
=   CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,   0);    
   
   
if   (hProcessSnap   ==   (HANDLE)-1)    
       
return   (FALSE);    
  
    pe32.dwSize  
=   sizeof(PROCESSENTRY32);    
   
   
if   (Process32First(hProcessSnap,   &pe32))    
    {  
       
while(pe32.th32ProcessID!=dwPPID)  
            Process32Next(hProcessSnap,  
&pe32);  
    }
   
    CloseHandle(hProcessSnap);

   
if(_tcsncicmp(pe32.szExeFile,_T("services.exe"),20)==0)
       
return TRUE;
   
else return FALSE;

}

//提升为调试权限
BOOL EnableDebugPrivilege(BOOL bEnable)
{
    BOOL bOk
= FALSE;
    HANDLE hToken;
   
   
if(::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
    {
        LUID uID;
        ::LookupPrivilegeValue(NULL, SE_DEBUG_NAME,
&uID);

        TOKEN_PRIVILEGES tp;
        tp.PrivilegeCount
= 1;
        tp.Privileges[
0].Luid = uID;
        tp.Privileges[
0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
        ::AdjustTokenPrivileges(hToken, FALSE,
&tp, sizeof(tp), NULL, NULL);
        bOk
= (::GetLastError() == ERROR_SUCCESS);

        ::CloseHandle(hToken);
    }
   
return bOk;
}

//用于在release下输出调试信息
void KDbgPrint(LPCTSTR lpszFormat, ...)
{
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#ifdef KOUT_DEBUG_STRING
    va_list args;
    va_start(args, lpszFormat);

   
int nBuf;
    TCHAR szBuffer[
512];

    nBuf
= _vsntprintf(szBuffer, _countof(szBuffer), lpszFormat, args);

    OutputDebugString(szBuffer);

    va_end(args);
#endif
}

//从地址去获得模块基址
HMODULE WINAPI ModuleFromAddress(PVOID pv)
{
    MEMORY_BASIC_INFORMATION mbi;
   
if(::VirtualQuery(pv, &mbi, sizeof(mbi)) != 0)
    {
       
return (HMODULE)mbi.AllocationBase;
    }
   
else
    {
       
return NULL;
    }
}

//获得DLL自身
BOOL GetDllPath(TCHAR * path)
{
    HMODULE hDll
= ModuleFromAddress(GetDllPath);

   
if(hDll==NULL)
       
return FALSE;
   
else
    {
        GetModuleFileName(hDll, path, _MAX_PATH);
       
return TRUE;
    }
}


//格式化string
void format_string(string & str,LPCTSTR lpszFormat, ...)
{
#define _countof(array) (sizeof(array)/sizeof(array[0]))
    va_list args;
    va_start(args, lpszFormat);
   
   
int nBuf;
    TCHAR szBuffer[
1024];
   
    nBuf
= _vsntprintf(szBuffer, _countof(szBuffer), lpszFormat, args);
   
    str
= szBuffer;
   
    va_end(args);
}

//提升所需的权限
BOOL EnablePrivilege(LPCTSTR lpName,BOOL bEnable)
{
    BOOL bOk
= FALSE;
    HANDLE hToken;
   
   
if(::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
    {
        LUID uID;
        ::LookupPrivilegeValue(NULL, lpName,
&uID);
       
        TOKEN_PRIVILEGES tp;
        tp.PrivilegeCount
= 1;
        tp.Privileges[
0].Luid = uID;
        tp.Privileges[
0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
        ::AdjustTokenPrivileges(hToken, FALSE,
&tp, sizeof(tp), NULL, NULL);
        bOk
= (::GetLastError() == ERROR_SUCCESS);
       
        ::CloseHandle(hToken);
    }
   
return bOk;
}

//跳转到注册表
void Jump2Reg(string strRegPath,string strKey)
{
   
//ShellExecute(NULL,NULL,"regjump.exe",strRegPath.c_str(),NULL,SW_SHOW);
    ShellExecute(NULL,NULL,"regedit.exe",NULL,NULL,SW_SHOW);
   
   
//然后查找注册表窗口
    HWND hMain = NULL;
    HWND hTree
= NULL;
    HWND hList
= NULL;
    BOOL bFind
= FALSE;
   
   
for(int i=0;i<10;i++)
    {
        hMain
= FindWindow("RegEdit_RegEdit","注册表编辑器");
       
       
if(hMain!=NULL)
        {
//查找成功
            bFind = TRUE;
           
break;
        }
       
        Sleep(
200);
    }
   
   
if(bFind)
    {
        hTree
= FindWindowEx(hMain,NULL,"SysTreeView32",NULL);
        hList
= FindWindowEx(hMain,NULL,"SysListView32",NULL);
       
       
//选择树
        SetForegroundWindow(hTree);
        SetFocus(hTree);
       
        DWORD dwPID;
        GetWindowThreadProcessId(hMain,
&dwPID);
        HANDLE hProc
= OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
       
       
//先把树给折叠起,最多30层
        for(int i=0;i<30;i++)
        {
            SendMessage(hTree, WM_KEYDOWN, VK_LEFT,
0);
        }
       
        SendMessage(hTree, WM_KEYDOWN, VK_RIGHT,
0);
        WaitForInputIdle(hProc, INFINITE);
       
       
       
for(i=0;i<strRegPath.length();i++)
        {
           
if(strRegPath.at(i)=='//')
                SendMessage(hTree, WM_KEYDOWN, VK_RIGHT,
0);
           
else
                SendMessage(hTree,WM_CHAR,WPARAM(strRegPath.at(i)),
0);
           
        }
       
        WaitForInputIdle(hProc, INFINITE);
       
       
//然后到列表了..
        SetForegroundWindow(hList);
        SetFocus(hList);
       
        Sleep(
1000);
       
        SendMessage(hList, WM_KEYDOWN, VK_HOME,
0);
       
       
for(i=0;i<strKey.length();i++)
        {
            SendMessage(hList,WM_CHAR,WPARAM(strKey.at(i)),
0);
        }
       
        CloseHandle(hProc);
    }
   
   
return;
}

//
//显示文件属性
void ShowProperties(string strPath)
{
    SHELLEXECUTEINFO si;
   
    ZeroMemory(
&si,sizeof(SHELLEXECUTEINFO));
   
    si.cbSize
= sizeof(SHELLEXECUTEINFO);
    si.fMask
= SEE_MASK_NOCLOSEPROCESS | SEE_MASK_INVOKEIDLIST | SEE_MASK_FLAG_NO_UI ;
    si.lpVerb
= "properties";
    si.lpFile
= strPath.c_str();
    si.nShow
= SW_SHOW;
   
    ShellExecuteEx(
&si);
}

//定位文件
void Jump2File(string strPath)
{
   
string cmd = "/e,/select,"+strPath;
    ShellExecute(NULL,NULL,
"explorer",cmd.c_str(),NULL,SW_SHOW);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值