未定义标识符 AssocSetPerceivedType
时间: 2023-09-16 13:16:00 浏览: 40
如果在使用上述示例代码时出现了 `AssocSetPerceivedType` 未定义标识符的错误,可能是因为该函数是Windows 8及更高版本的新函数,而你的编译器可能不支持。在这种情况下,你可以尝试使用旧的函数 `AssocCreate` 来替代 `AssocSetPerceivedType`,如下所示:
```c++
#include <Windows.h>
int main()
{
LPCWSTR extension = L".txt"; // 文件扩展名
LPCWSTR appName = L"C:\\Windows\\notepad.exe"; // 应用程序路径
// 设置默认程序
HKEY hKey;
HRESULT result = AssocCreate(extension, L"text", &hKey);
if (SUCCEEDED(result))
{
result = RegSetValueEx(hKey, L"", 0, REG_SZ, (BYTE*)appName, (lstrlen(appName) + 1) * sizeof(wchar_t));
if (SUCCEEDED(result))
{
result = SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
if (SUCCEEDED(result))
{
// 设置成功
}
}
RegCloseKey(hKey);
}
return 0;
}
```
这个示例代码调用了 `AssocCreate`、`RegSetValueEx` 和 `SHChangeNotify` 函数来分别设置文件类型、默认程序和刷新Shell通知。你可以修改 `extension` 和 `appName` 变量来设置不同的文件类型和默认程序。同样需要以管理员权限运行该程序才能修改默认程序。