2

我正在尝试为我的应用程序创建一个文件夹,/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();
        }
        ....
    };
}

游戏手柄.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
{
    ....
}

配置.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");
    }
}
4

2 回答 2

4

IServerProvider确实是模棱两可的,它既作为 servprov.h Windows SDK 头文件中的 COM 接口类型存在,又作为System命名空间中的 .NET 接口类型存在。

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

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

Bam,18 个错误。如果您正确订购它们,没问题:

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

小心那些使用指令的人,他们真的很擅长制造歧义。

于 2015-01-12T01:08:24.037 回答
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
于 2019-06-27T14:05:12.680 回答