基于该贴主的方法,我尝试修改了以下代码,3.9启动没问题,但是4.0版本启动失效,只会将已经最小化的窗口还原
代码如下:
static bool getVxInstallPath(WCHAR* pathStr, double version)
{
HKEY hKey;
LPCWSTR keyPath;
LONG openResult;
if (version >= 4.0) {
keyPath = _T("SOFTWARE\\Tencent\\Weixin");
}
else {
keyPath = _T("SOFTWARE\\Tencent\\WeChat");
}
openResult = RegOpenKeyEx(HKEY_CURRENT_USER, keyPath, 0, KEY_ALL_ACCESS, &hKey);
if (openResult == ERROR_SUCCESS) {
DWORD valueSize = 4095;
LONG queryResult = RegQueryValueEx(hKey, _T("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;
}
static 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);
}
void CMultiWeiXin4Dlg::OnBnClickedButtonWx3()
{
WCHAR WeChatPath[4096] = { 0 };
size_t bufSize = 4096;
if (getVxInstallPath(WeChatPath, 3.9)) {
if (!wcscat_s(WeChatPath, bufSize, _T("\\WeChat.exe"))) {
enableMultiWeChat();
ShellExecute(NULL, NULL, WeChatPath, NULL, NULL, SW_NORMAL);
return;
}
}
MessageBox(_T("启动失败"));
}
void CMultiWeiXin4Dlg::OnBnClickedButtonWx4()
{
WCHAR WeChatPath[4096] = { 0 };
size_t bufSize = 4096;
if (getVxInstallPath(WeChatPath, 4.0)) {
if (!wcscat_s(WeChatPath, bufSize, _T("\\Weixin.exe"))) {
enableMultiWeChat();
ShellExecute(NULL, NULL, WeChatPath, NULL, NULL, SW_NORMAL);
return;
}
}
MessageBox(_T("启动失败"));
}