c++ - 使用“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());
}
.....
任何人都可以解释发生了什么,我该如何解决上述问题?
解决方案
这是因为using namespace System;
与中的名称冲突Windows.h
,
Windows.h
servprov.h
间接引入。并且servprov.h
有这样的定义:typedef interface IServiceProvider IServiceProvider;
其中,IServiceProvider
与System命名空间中的冲突IServiceProvider
,造成不确定性。
解决方案是使用完全限定名称而不是 System 命名空间,例如System::IServiceProvider
.
推荐阅读
- atom-editor - 升级到 MacOs Mojave 后 Xdebug 和 Atom 不起作用
- ios - 无法在 Mac 上运行适用于 iOS 的 Uno 项目 - 无法加载“SQLitePCLRaw.core”
- python - Python 3 unittest 补丁未返回所需值
- camera - Jetson Xavier 入门级 CSI-2 摄像头模块
- sql-server - SQL Server:每晚从文件中批量插入数据
- entity-framework - Entity Framework 6 DB First 方法,没有 EDMX。可能吗?
- php - 带有要转换为纯 HTML 的简码的 Wordpress 发布内容
- azure-active-directory - 如何设置检测另一个目录中的 AD 更改的 Azure 触发器?
- java - 事件线程和主线程的代码结构
- f# - F# / FAKE - 如何在调用 MSBuild 时指定 `--platform:x64`