RegisterApplicationRestart

来源:互联网 发布:windows打开隐藏文件 编辑:程序博客网 时间:2024/05/16 08:09
#include <stdlib.h>
#include <string>


BOOL SetRestartApplication(const TCHAR* commandLine)
{
typedef HRESULT (WINAPI* PFN_RegisterApplicationRestart)(LPWSTR,DWORD);
PFN_RegisterApplicationRestart pfnRegisterApplicationRestart = NULL;


HMODULE hInst = ::GetModuleHandle("kernel32.dll");
if(hInst == NULL) return FALSE;


pfnRegisterApplicationRestart = (PFN_RegisterApplicationRestart)GetProcAddress(hInst,"RegisterApplicationRestart");
if(NULL == pfnRegisterApplicationRestart) return FALSE;


DWORD dwFlags=0,dwRet = S_OK;
#if defined(_UNICODE) ||defined( UNICODE)
dwRet = pfnRegisterApplicationRestart(commandLine,dwFlags);
#else
wchar_t* cmdLine = NULL;
const char * oldLocale = setlocale(LC_CTYPE,NULL);
const char * newLocale = ::setlocale(LC_CTYPE,".936"); //equals ".936","chs",""
int len = mbstowcs(NULL,commandLine,0);
if(len >0)
{
cmdLine = new wchar_t[len+1];
mbstowcs(cmdLine,commandLine,len+1);
dwRet = pfnRegisterApplicationRestart(cmdLine,dwFlags);
delete[] cmdLine;
cmdLine = NULL;
}
else
{
dwRet = S_FALSE;
}
#endif


FreeLibrary(hInst);


if(oldLocale)
{
const char * locale = setlocale(LC_CTYPE,oldLocale);
}
return (dwRet == S_OK);
}
0 0
原创粉丝点击