#include "stdafx.h"
#include
#include
/*
注册表添加新键
keyName: 添加键的名,比如为"XXXX",在网址填写"XXXX://"就可以启动path指定的exe路径."XXXX:///"会作为argv[1]传给程序(末尾加了一个/)
path: exe路径
*/
void WriteRegedit(const char* keyName, const wchar_t* path)
{
long lRet;
HKEY hKey;
HKEY hKey1;
std::wstring wstrModule = L"\"";
wstrModule += path;
wstrModule += L"\"";
wstrModule += L" \"%1\"";
std::string fullKey = keyName;
fullKey += "\\shell\\open\\command";
lRet = RegOpenKeyExA(HKEY_CLASSES_ROOT, fullKey.c_str(), 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS)//已存在,改值
{
lRet = RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)wstrModule.c_str(), wstrModule.size() * 2);
RegCloseKey(hKey);
if (lRet == ERROR_SUCCESS)
{
std::cout << "已修改" << std::endl;
}
else std::cout << "修改失败" << std::endl;
RegOpenKeyExA(HKEY_CLASSES_ROOT, keyName, 0, KEY_ALL_ACCESS, &hKey);
RegSetValueEx(hKey, L"URL Protocol", 0, REG_SZ, (BYTE*)path, wcslen(path) * 2);
RegCloseKey(hKey);
}
else //不存在,创建
{
//keyName
DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
lRet = RegCreateKeyExA(HKEY_CLASSES_ROOT, keyName, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS)
{
std::cout << "设置失败" << std::endl;
return;
}
wchar_t* strValue = L"注册新协议,使用URL链接打开本地程序";
RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE*)strValue, wcslen(strValue) * 2);
RegSetValueEx(hKey, L"URL Protocol", 0, REG_SZ, (BYTE*)path, wcslen(path) * 2);
//DefaultIcon
lRet = RegCreateKeyEx(hKey, L"DefaultIcon", 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hKey1, &dwDisposition);
if (lRet != ERROR_SUCCESS)
{
std::cout << "设置失败" << std::endl;
RegCloseKey(hKey);
return;
}
std::wstring temp = path;
temp += L",1";
lRet = RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE*)temp.c_str(), temp.size() * 2);
if (lRet != ERROR_SUCCESS)
{
std::cout << "设置失败" << std::endl;
}
RegCloseKey(hKey1);
//shell\\open\\command
lRet = RegCreateKeyEx(hKey, L"shell\\open\\command", 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hKey1, &dwDisposition);
if (lRet != ERROR_SUCCESS)
{
std::cout << "设置失败" << std::endl;
RegCloseKey(hKey);
return;
}
lRet = RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE*)wstrModule.c_str(), wstrModule.size() * 2);
if (lRet != ERROR_SUCCESS)
{
std::cout << "设置失败" << std::endl;
}
RegCloseKey(hKey1);
RegCloseKey(hKey);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//禁止同名程序运行
HANDLE hMutex = CreateMutex(NULL, FALSE, L"OnlyOne");
if (hMutex)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMutex);
HWND hWndPrev = GetWindow(GetDesktopWindow(), GW_CHILD);
while (IsWindow(hWndPrev))
{
if (GetProp(hWndPrev, L"001"))
{
if (IsIconic(hWndPrev))
ShowWindow(hWndPrev, SW_RESTORE); //恢复窗口尺寸
SetForegroundWindow(hWndPrev); //窗口前置
return 0;
}
hWndPrev = ::GetWindow(hWndPrev, GW_HWNDNEXT);
}
return 0;
};
}
else return 0;
if (argc > 1)
{
HWND hWnd = FindWindow(NULL, argv[0]);
SetProp(hWnd, L"001", (HANDLE)1);
std::wcout << argv[1] << std::endl;
std::cin.get();
}
else
{
WriteRegedit("webtest", argv[0]);
#ifdef _DEBUG
std::cin.get();
#endif
}
CloseHandle(hMutex);
return 0;
}