Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在非托管cpp dll中实现C#接口_C#_C++_Plugins_Interface_Add On - Fatal编程技术网

在非托管cpp dll中实现C#接口

在非托管cpp dll中实现C#接口,c#,c++,plugins,interface,add-on,C#,C++,Plugins,Interface,Add On,我有一个具有附加功能的C#应用程序。实现特定接口的附加组件/插件可以在运行时链接/加载到主C#应用程序 现在我的问题是:如何开发一个非托管cpp dll来实现C#接口并在运行时加载到C#应用程序 谢谢, Cabbi < P>使用通用语言支持< /C> >,可以编写混合了完整的本机C++能力的托管DLL。 您可以通过.Net中的PInvoke加载非托管dll,但我不建议这样做。严格来说,您不能拥有实现C#(托管)接口的非托管dll 但是,您可以创建一个,这可能是您想要的。这是一种非常常见的方法,提

我有一个具有附加功能的C#应用程序。实现特定接口的附加组件/插件可以在运行时链接/加载到主C#应用程序

现在我的问题是:如何开发一个非托管cpp dll来实现C#接口并在运行时加载到C#应用程序

谢谢,
Cabbi

< P>使用<代码>通用语言支持< /C> >,可以编写混合了完整的本机C++能力的托管DLL。
您可以通过.Net中的PInvoke加载非托管dll,但我不建议这样做。

严格来说,您不能拥有实现C#(托管)接口的非托管dll

但是,您可以创建一个,这可能是您想要的。这是一种非常常见的方法,提供一个带有.NET包装器的本地C++(非托管)实现,使它成为一个看起来像C(或其他.NET语言)用户的程序集。
此外,您可以使用从网络程序集使用纯本机DLL。

您可以使用p/inkove实现接口,例如:

public interface IDirectory
{
    bool IsDirectoryEmplty(string path);
}

public class Directory : IDirectory
{
    [DllImport("Shlwapi.dll", EntryPoint = "PathIsDirectoryEmpty")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsDirectoryEmplty([MarshalAs(UnmanagedType.LPStr)]string directory);

    bool IInterface.IsDirectoryEmplty(string path)
    {
        return IsDirectoryEmplty(path);
    }
}

与仅仅调用C#方法相比,您对性能有什么概念吗?有很多开销吗?我想,这样的实现(外部c/c++)只能用于大型计算…我不知道混合模式,很好!我将调查“PInvoke”开销。为什么不推荐“PInvoke”?@Cabbi C++/CLI比PInvoke更容易编写。即使在大多数情况下,C++/CLI也比PInvoke快一点。