SHGetKnownFolderPath:不明确的符号 'IServiceProvider'?
SHGetKnownFolderPath: ambiguous symbol 'IServiceProvider'?
我正在尝试在 /AppData/local
中为我的应用程序创建一个文件夹,以便我可以在其中保存一些 ini 文件,我尝试使用以下方法获取目标路径:
#include <ShlObj.h>
if (SHGetKnownFolderPath (FOLDERID_LocalAppData, 0, NULL, &tempPath) == S_OK)
{
....
}
它不起作用并给我这些错误:
Error 1 error C2872: 'IServiceProvider' : ambiguous symbol c:\program files\windows kits.0\include\um\ocidl.h 6482 1 Project2
Error 2 error C2872: 'IServiceProvider' : ambiguous symbol C:\Program Files\Windows Kits.0\Include\um\shobjidl.h 9181 1 Project2
我尝试在项目设置中添加 #pragma comment (lib, "Shell32.lib")
并链接 Shell32.lib
,但没有任何改变。
当我删除 #include <ShlObj.h>
但随后 SHGetKnownFolderPath
函数变得未定义时,错误消失了。我该如何解决这个问题?
注:我在Windows7
编辑: 我的项目头文件是:
MyForm.h
#pragma once
#define CRTDBG_MAP_ALLOC
#include "gamepad.h"
#include "configure.h"
#include <stdlib.h>
#include <crtdbg.h>
#include <Dbt.h>
namespace Project2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
this->gamepad = gcnew Gamepad();
this->SETTINGS = gcnew Settings();
}
....
};
}
gamepad.h
#pragma once
#include <Windows.h>
#include <WinUser.h>
#include <tchar.h>
#define _USE_MATH_DEFINES
#include <math.h>
extern "C"
{
#include <hidsdi.h>
}
#include "InputHandler.h"
#include "keycodes.h"
using namespace System;
public ref class Gamepad
{
....
}
configure.h
#pragma once
#include "keycodes.h"
#include <Windows.h>
#include <Shlwapi.h>
#include <ShlObj.h>
#include <msclr\marshal.h>
using namespace System;
using namespace System::Diagnostics;
using namespace System::IO;
using namespace msclr::interop;
public ref class Settings
{
public:
Settings(void)
{
PWSTR tempPath;
if (SUCCEEDED (SHGetKnownFolderPath (FOLDERID_LocalAppData, 0, NULL, &tempPath)))
Debug::WriteLine (gcnew String (tempPath));
else Debug::WriteLine ("Failed");
}
}
IServerProvider
确实有歧义,它既作为 COM 接口类型存在于 servprov.h Windows SDK 头文件中 又作为 . System 命名空间中的 NET 接口类型。
重现问题的最简单方法是将 using namespace 指令放在错误的位置:
#include "stdafx.h"
using namespace System;
#include <ShlObj.h>
砰,18 个错误。如果您订购正确,没问题:
#include "stdafx.h"
#include <ShlObj.h>
using namespace System;
小心那些 using 指令,它们非常擅长制造歧义。
IServerProvider
是不明确的,因为它是 servprov.h 中的 COM 接口类型(通过 windows.h 获得)并且是 System 命名空间中的 .NET 接口类型。
如果您不需要 windows.h 中的所有 API,您可以 #define WIN32_LEAN_AND_MEAN
获取排除 IServiceProvider
定义并避免歧义的子集。
在 #include
之前执行 #define
,之后执行 #undef
可能是个好主意,如下所示:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
我正在尝试在 /AppData/local
中为我的应用程序创建一个文件夹,以便我可以在其中保存一些 ini 文件,我尝试使用以下方法获取目标路径:
#include <ShlObj.h>
if (SHGetKnownFolderPath (FOLDERID_LocalAppData, 0, NULL, &tempPath) == S_OK)
{
....
}
它不起作用并给我这些错误:
Error 1 error C2872: 'IServiceProvider' : ambiguous symbol c:\program files\windows kits.0\include\um\ocidl.h 6482 1 Project2
Error 2 error C2872: 'IServiceProvider' : ambiguous symbol C:\Program Files\Windows Kits.0\Include\um\shobjidl.h 9181 1 Project2
我尝试在项目设置中添加 #pragma comment (lib, "Shell32.lib")
并链接 Shell32.lib
,但没有任何改变。
当我删除 #include <ShlObj.h>
但随后 SHGetKnownFolderPath
函数变得未定义时,错误消失了。我该如何解决这个问题?
注:我在Windows7
编辑: 我的项目头文件是:
MyForm.h
#pragma once
#define CRTDBG_MAP_ALLOC
#include "gamepad.h"
#include "configure.h"
#include <stdlib.h>
#include <crtdbg.h>
#include <Dbt.h>
namespace Project2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Diagnostics;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
this->gamepad = gcnew Gamepad();
this->SETTINGS = gcnew Settings();
}
....
};
}
gamepad.h
#pragma once
#include <Windows.h>
#include <WinUser.h>
#include <tchar.h>
#define _USE_MATH_DEFINES
#include <math.h>
extern "C"
{
#include <hidsdi.h>
}
#include "InputHandler.h"
#include "keycodes.h"
using namespace System;
public ref class Gamepad
{
....
}
configure.h
#pragma once
#include "keycodes.h"
#include <Windows.h>
#include <Shlwapi.h>
#include <ShlObj.h>
#include <msclr\marshal.h>
using namespace System;
using namespace System::Diagnostics;
using namespace System::IO;
using namespace msclr::interop;
public ref class Settings
{
public:
Settings(void)
{
PWSTR tempPath;
if (SUCCEEDED (SHGetKnownFolderPath (FOLDERID_LocalAppData, 0, NULL, &tempPath)))
Debug::WriteLine (gcnew String (tempPath));
else Debug::WriteLine ("Failed");
}
}
IServerProvider
确实有歧义,它既作为 COM 接口类型存在于 servprov.h Windows SDK 头文件中 又作为 . System 命名空间中的 NET 接口类型。
重现问题的最简单方法是将 using namespace 指令放在错误的位置:
#include "stdafx.h"
using namespace System;
#include <ShlObj.h>
砰,18 个错误。如果您订购正确,没问题:
#include "stdafx.h"
#include <ShlObj.h>
using namespace System;
小心那些 using 指令,它们非常擅长制造歧义。
IServerProvider
是不明确的,因为它是 servprov.h 中的 COM 接口类型(通过 windows.h 获得)并且是 System 命名空间中的 .NET 接口类型。
如果您不需要 windows.h 中的所有 API,您可以 #define WIN32_LEAN_AND_MEAN
获取排除 IServiceProvider
定义并避免歧义的子集。
在 #include
之前执行 #define
,之后执行 #undef
可能是个好主意,如下所示:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN