首页 > 解决方案 > 使用“wininet”和“windows.h”时如何解决“IServiceProvider”不明确?

问题描述

我正在尝试InternetCheckConnection使用wininet.

这是我的CheckerClass:处理检查过程。

#pragma once
#include <Windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
#include <String>

public ref class CheckerClass
{
public:
    static std::string hasInternet() {
        bool bConnect = InternetCheckConnection(L"https://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0);
        if (bConnect){
            return "Has Internet!";
        }else{
            return "No Internet!";
        }
    }
};

但我收到以下错误,我无法解决。

Error (active)  E1986   an ordinary pointer to a C++/CLI ref class or interface class is not allowed
Error (active)  E0266   "IServiceProvider" is ambiguous
Error   C3699   '*': cannot use this indirection on type 'IServiceProvider' 

经过搜索,我发现这是因为使用using namespace System,但我在上面的课程中没有。

但是,我在使用上述类的Main类中有以下内容。

#pragma once
#include<string>
#include "CheckerClass.h"
namespace CppCLRWinformsProjekt {

    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 std::string;

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:

        CheckerClass checkerClass;
        
        Form1(void)
        {
            InitializeComponent();
            string result = checkerClass.hasInternet();
            this->label_output->Text = gcnew System::String(result.c_str());
        }

    .....

任何人都可以解释发生了什么,我该如何解决上述问题?

标签: c++visual-studiovisual-studio-2019

解决方案


这是因为using namespace System;与中的名称冲突Windows.h

Windows.hservprov.h间接引入。并且servprov.h有这样的定义:typedef interface IServiceProvider IServiceProvider; 其中,IServiceProvider与System命名空间中的冲突IServiceProvider,造成不确定性。

解决方案是使用完全限定名称而不是 System 命名空间,例如System::IServiceProvider.


推荐阅读