[C++] 纯文本查看 复制代码
// WeChatLauncher.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include <aclapi.h>
#include <shlwapi.h>
#include <shellapi.h>
#include "WeChatLauncher.h"
#pragma comment(lib,"Shlwapi.lib")
bool getVxInstallPath(WCHAR* pathStr)
{
HKEY hKey;
LPCWSTR keyPath = L"SOFTWARE\\Tencent\\WeChat";
LONG openResult = RegOpenKeyEx(HKEY_CURRENT_USER, keyPath, 0, KEY_ALL_ACCESS, &hKey);
if (openResult == ERROR_SUCCESS) {
DWORD valueSize = 4095;
LONG queryResult = RegQueryValueEx(hKey, L"InstallPath", nullptr, nullptr, (LPBYTE)pathStr, &valueSize);
if (queryResult == ERROR_SUCCESS) {
RegCloseKey(hKey);
return true;
}
else {
::MessageBox(NULL, L"无法读取注册表值", L"提示", MB_OK);
}
}
else {
::MessageBox(NULL, L"无法打开注册表键", L"提示", MB_OK);
}
RegCloseKey(hKey);
return false;
}
void enableMultiWeChat()
{
HANDLE hMutex = CreateMutexW(NULL, FALSE, L"_WeChat_App_Instance_Identity_Mutex_Name");
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
PSID pEveryoneSID = NULL;
char szBuffer[4096] = { 0 };
PACL pAcl = (PACL)szBuffer;
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID);
InitializeAcl(pAcl, sizeof(szBuffer), ACL_REVISION);
AddAccessDeniedAce(pAcl, ACL_REVISION, MUTEX_ALL_ACCESS, pEveryoneSID);
SetSecurityInfo(hMutex, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pAcl, NULL);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
WCHAR WeChatPath[4096] = { 0 };
size_t bufSize = 4096;
if (getVxInstallPath(WeChatPath))
{
if (!wcscat_s(WeChatPath, bufSize, L"\\WeChat.exe"))
{
enableMultiWeChat();
ShellExecute(NULL, NULL, WeChatPath, NULL, NULL, SW_NORMAL);
return TRUE;
}
}
::MessageBox(NULL, L"执行失败!", L"错误提示", MB_ICONERROR|MB_OK);
return FALSE;
}