SHGetKnownFolderPath:模糊符号' IServiceProvider'?

时间:2015-01-11 19:45:45

标签: c++-cli known-folders

我正在尝试在/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\8.0\include\um\ocidl.h    6482    1   Project2
Error   2   error C2872: 'IServiceProvider' : ambiguous symbol  C:\Program Files\Windows Kits\8.0\Include\um\shobjidl.h 9181    1   Project2

我尝试添加#pragma comment (lib, "Shell32.lib")并在项目设置中关联Shell32.lib并且没有任何更改。

当我删除#include <ShlObj.h>但是SHGetKnownFolderPath函数变得未定义时,错误消失。我该如何解决这个问题?

注意:我在Windows 7上

修改:我的项目标题文件为:

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");
    }
}

2 个答案:

答案 0 :(得分:3)

IServerProvider确实不明确,它在servprov.h Windows SDK头文件中作为COM接口类型存在,而在System命名空间中作为.NET接口类型存在。

重新解决问题的最简单方法是将 using namespace 指令放在错误的位置:

  #include "stdafx.h"
  using namespace System;
  #include <ShlObj.h>
巴姆,18个错误。如果你订购它们没问题:

  #include "stdafx.h"
  #include <ShlObj.h>
  using namespace System;

小心使用使用指令,他们非常擅长制造歧义。

答案 1 :(得分:0)

IServerProvider含糊不清,因为它是servprov.h中的COM接口类型(与windows.h一起获得),并且是System命名空间中的.NET接口类型。

如果在windows.h中不需要所有API,则可以#define WIN32_LEAN_AND_MEAN获取不包含IServiceProvider定义并避免歧义的子集。

#define之前进行#include,然后在#undef之后这样做是个好主意,像这样:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
相关问题