您的位置:首页 > 其它

项目中一些常用函数

2008-01-20 13:19 405 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: