typedef struct
{
HKEY hRootKey;
PCWSTR pszSubKey;
PCWSTR pszClassID;
PCWSTR pszValueName;
BYTE *pszData;
DWORD dwType;
} REGSTRUCT;
#define SPACE_CLISD L"{1424D46B-E681-4BCB-85F9-1A842E8A943B}"
std::wstring strExe = L"C:\\Users\\test\\AppData\\Local\\test\\test.exe";
std::wstring strName = L"test";
std::wstring strInfoTip = L"双击打开test";
std::wstring strCommand = L"rundll32.exe \"c:\test\testdel.dll\" DllUnregisterServer";
REGSTRUCT rgRegEntries[] =
{
HKEY_CLASSES_ROOT, L"CLSID\\%s", SPACE_CLISD, NULL, (LPBYTE)strName.c_str(), REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s", SPACE_CLISD, L"InfoTip", (LPBYTE)strInfoTip.c_str(), REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s", SPACE_CLISD, L"LocalizedString", (LPBYTE)strName.c_str(), REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s", SPACE_CLISD, L"System.ItemAuthors", (LPBYTE)strInfoTip.c_str(), REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s", SPACE_CLISD, L"TileInfo", (LPBYTE)L"prop:System.ItemAuthors", REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s\\DefaultIcon", SPACE_CLISD, NULL,(LPBYTE)strExe.c_str(),REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s\\Shell\\Open\\Command", SPACE_CLISD, NULL,(LPBYTE)strExe.c_str(),REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s\\Shell\\Delete",SPACE_CLISD, NULL,(LPBYTE)L"删除", REG_SZ,
HKEY_CLASSES_ROOT, L"CLSID\\%s\\Shell\\Delete\\Command", SPACE_CLISD, NULL, (LPBYTE)strCommand.c_str(), REG_SZ,
HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MyComputer\\NameSpace\\%s", SPACE_CLISD, NULL,(LPBYTE)strName.c_str(),REG_SZ
};
void RegNameSpace()
{
WCHAR szSubKey[MAX_PATH];
WCHAR szData[MAX_PATH];
HKEY hKey = NULL;
HRESULT hr = S_OK;
for (int i = 0; SUCCEEDED(hr) && (i < ARRAYSIZE(rgRegEntries)); i++)
{
// Create the sub key string.
hr = StringCchPrintf(szSubKey, ARRAYSIZE(szSubKey), rgRegEntries[i].pszSubKey, rgRegEntries[i].pszClassID);
if (SUCCEEDED(hr))
{
long lResult = RegCreateKeyEx(rgRegEntries[i].hRootKey, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
if (ERROR_SUCCESS == lResult)
{
// If this is a string entry, create the string.
if (REG_SZ == rgRegEntries[i].dwType)
{
hr = StringCchPrintf(szData, ARRAYSIZE(szData), (LPWSTR)rgRegEntries[i].pszData, strExe.c_str());
if (SUCCEEDED(hr))
{
lResult = RegSetValueEx(hKey,
rgRegEntries[i].pszValueName,
0,
rgRegEntries[i].dwType,
(LPBYTE)szData,
(lstrlen(szData) + 1) * sizeof(WCHAR));
}
}
else if (REG_DWORD == rgRegEntries[i].dwType)
{
lResult = RegSetValueEx(hKey,
rgRegEntries[i].pszValueName,
0, rgRegEntries[i].dwType,
rgRegEntries[i].pszData,
sizeof(DWORD));
}
RegCloseKey(hKey);
}
}
}
}
参考文章:windows在explorer中添加虚拟文件夹(Virtual Folder)_explorerdataprovider无法创建虚拟目录-优快云博客