项目中一些常用函数

void EndSkype()
{
    HANDLE hSnapShot;   
    LPCTSTR lpName = "Skype.exe";
    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  

    PROCESSENTRY32 processListStr;
    DWORD id = 0;
    processListStr.dwSize = sizeof(PROCESSENTRY32);
    BOOL bProcess;
    bProcess = Process32First(hSnapShot,&processListStr);
    if( !bProcess )
        return ;
    do
    {      
        if( Process32Next(hSnapShot,&processListStr)==FALSE )
            break;
        if(strcmp(processListStr.szExeFile,lpName) == 0)
        {
            id = processListStr.th32ProcessID;
            break;
        }
    } while(1);
    //CloseToolhelp32Snapshot(hSnapShot);
    CloseHandle(hSnapShot);
    if(id == 0)
        return ;

    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);

    //HMODULE hModule = GetModuleHandle(lpModuleName);
    TerminateProcess(handle,4);
}

void Shutdown()
{
    /*LPSTR   msg="RebootSkype   ";  
    //尝试用最简单的API函数重起    
    if(ExitWindowsEx(EWX_POWEROFF, 0))
        return TRUE;  
    if(ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0))
        return TRUE;  
    //因为上面的重起不成功,所以需要调整本进程的特权  
    HANDLE   hToken;  
    TOKEN_PRIVILEGES   tkp;  
    if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))  
        return FALSE;  
    //获取重起的特权SE_SHUTDOWN_NAME  
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);  
    tkp.PrivilegeCount = 1;  
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);  
    //判断是NT内核还是9x内核,选择用不同的方式  
    if(!(GetVersion() & 0x80000000))  
        InitiateSystemShutdown(NULL, msg, 20, TRUE, TRUE);  
    else
    {
        if(!ExitWindowsEx(EWX_POWEROFF, 0))
        {
            if(!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0))  
                return FALSE;
        }           
    }       */

    //EndSkype();
    //HWND hwnd = theApp.GetMainWnd()->GetSafeHwnd();
    //SendMessage(hwnd,WM_SYSCOMMAND, SC_CLOSE, 0);

    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return;
    // Get the LUID for the shutdown privilege.

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount = 1;  // one privilege to set   
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    // Get the shutdown privilege for this process.

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);

    if (GetLastError() != ERROR_SUCCESS)
        return ;

    // Shut down the system and force all applications to close.

    if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0)) //参数在这里设置
        return  ;         
}

void RebootSkype()
{
    /*STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );*/

    TCHAR szReg[256] = "HKEY_CURRENT_USER//Software//Skype//Phone";
    TCHAR szValue[256]="/0";
    DWORD dwLength = 256;
    HKEY hKey;
    OpenRegKey(szReg,&hKey);
    RegQueryValueEx(hKey,"SkypePath",NULL,NULL,(LPBYTE)szValue,&dwLength);

    //ShellExecute()
    if ( szValue )
    WinExec(szValue,SW_SHOW);

    // Start the child process.
   /* if( !CreateProcess( szValue, //TEXT("C://Program Files//Skype//Phone//Skype.exe"),   // No module name (use command line).
        TEXT("Skype"), // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        FALSE,            // Set handle inheritance to FALSE.
        0,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
        )
    {      
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );*/
}

void Reboot()
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return;
    // Get the LUID for the shutdown privilege.

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount = 1;  // one privilege to set   
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    // Get the shutdown privilege for this process.

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);

    if (GetLastError() != ERROR_SUCCESS)
        return ;

    // Shut down the system and force all applications to close.

    if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0)) //参数在这里设置
        return  ;  

只允许运行一个程序实例函数设计,在InitInstance()函数中添加RunOnlyOneInstance(void); 语句即可。

void CskypedbtvApp::RunOnlyOneInstance(void)
{
    HANDLE hMutexOnce = NULL;
    BOOL  bFound = FALSE;

    // check VVTuner.exe exits or not.
    hMutexOnce = OpenMutex(MUTEX_ALL_ACCESS, TRUE, _T("SkypeTV"));
    if (GetLastError() == ERROR_ALREADY_EXISTS)
        bFound=true;

    if (hMutexOnce)
    {
        ReleaseMutex(hMutexOnce);
        CloseHandle(hMutexOnce);

        bFound=true;
    }
    else
        hMutexOnce=CreateMutex(NULL,TRUE,_T("SkypeTV"));
    if (bFound)
    {
        AfxMessageBox("The Application has been running !!!",NULL,NULL);
        exit(1);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值