简体   繁体   English

如何正确使用INetSharingManager?

[英]How to properly use INetSharingManager?

I'm a newbie C++ programmer, and using C++ Builder XE3, and I have a good Delphi background, and I'm trying to use INetSharingManager, but it gives me an error message: 我是一名新手C ++程序员,并且使用C ++ Builder XE3,并且具有良好的Delphi背景,并且尝试使用INetSharingManager,但是它给我一条错误消息:

E2352 Cannot create instance of abstract class 'INetSharingManager'

this is the code that I use: 这是我使用的代码:

INetSharingManager* NSManager = new INetSharingManager();

My questions are : 我的问题是

  • How to properly use INetSharingManager in C++? 如何在C ++中正确使用INetSharingManager?
  • and how to use INetSharingManager in Delphi, (if it's possible) ? 以及如何在Delphi中使用INetSharingManager (如果可能)

and thanks in advance. 并预先感谢。

This is an interface, and you cannot instantiate it like that. 这是一个接口,您不能像这样实例化它。 Remember that interfaces do not have implementation, so there's clearly no chance at all to instantiate them. 请记住,接口没有实现,因此显然根本没有机会实例化它们。 You have to instantiate something else that implements the interface. 您必须实例化实现该接口的其他内容。

The C++ sample code here shows how to create one of these guys. 这里的C ++示例代码显示了如何创建这些家伙之一。 Here's the key excerpt: 这是关键摘录:

CoInitialize (NULL);

// init security to enum RAS connections
CoInitializeSecurity (NULL, -1, NULL, NULL, 
                      RPC_C_AUTHN_LEVEL_PKT, 
                      RPC_C_IMP_LEVEL_IMPERSONATE,
                      NULL, EOAC_NONE, NULL);

INetSharingManager * pNSM = NULL;    
HRESULT hr = ::CoCreateInstance (__uuidof(NetSharingManager),
                                 NULL,
                                 CLSCTX_ALL,
                                 __uuidof(INetSharingManager),
                                 (void**)&pNSM);

This code comes from the official Microsoft documentation for this library. 此代码来自该库的官方Microsoft文档。 You should read that documentation thoroughly. 您应该彻底阅读该文档。

You ask how to use this interface from Delphi. 您问如何从Delphi使用此接口。 Well, you do exactly the same as you do in C++. 好吧,您所做的与在C ++中所做的完全相同。 Call CoCreateInstance to obtain an interface reference. 调用CoCreateInstance以获取接口引用。 Now that you can see how to do it from C++, it's simple enough to translate it to Delphi. 现在您可以看到如何使用C ++进行操作,将其转换为Delphi非常简单。

As the answer is fairly outdated I recommend following code: 由于答案已经过时,因此我建议使用以下代码:

HRESULT hr = E_FAIL; 
CoInitialize(nullptr);
INetSharingManager* pNetSharingManager = nullptr;
hr = CoCreateInstance(CLSID_NetSharingManager, 
nullptr,
CLSCTX_ALL, 
IID_INetSharingManager,
reinterpret_cast<LPVOID*>(&pNetSharingManager));

Tested and working code sample. 经过测试的工作代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM