简体   繁体   English

使用DLLImport导入类

[英]Using DLLImport to import a class

I have an class in dll: For example: 我在dll中有一个类:例如:

namespace foo {
   public class baa {
      /* ... */
  }
}

how can I imports the baa class from dll? 如何从dll导入baa类? it is possible? 有可能的?

[DllImport(DllName)]
public extern ?? foo() ??

Thanks in advance. 提前致谢。

That's not going to work. 那不行。 Unmanaged DLLs export a C interface, not a C++ one. 非托管DLL导出C接口,而不是C++接口。 And for managed DLLs (C# or C++/CLI) you simply don't need DllImport. 对于托管DLL(C#或C ++ / CLI),您根本不需要DllImport。

Only functions that are imported into a static class I'm afraid. 只有导入静态类的函数我才害怕。

DllImport is used only when you want to invoke unmanaged functions from an unmanaged library (like one written in C++). DllImport仅在您想要从非托管库调用非托管函数时使用(例如用C ++编写的函数)。

When you have a managed .NET assembly you simply add it as reference to your project and use it. 当您拥有托管的.NET程序集时,只需将其添加为项目的引用并使用它。

So assuming you have a .NET class library containing the following class: 假设您有一个包含以下类的.NET类库:

namespace foo {
   public class baa {
      /* ... */
  }
}

and then you have some other project that needs to use this assembly you go to the References node in the Solution Explorer and Add Reference to the given assembly. 然后你有一些其他需要使用这个程序集的项目,你可以转到解决方案资源管理器中的References节点和给定程序集的Add Reference。 Then you bring the namespace into scope: 然后将命名空间放入范围:

using foo;

and instantiate the class: 并实例化该类:

baa b = new baa();
... use the b instance here

That's a standard C++ export mechanism that only works with C++. 这是一个标准的C ++导出机制,只适用于C ++。 You can't import it from C# directly. 您无法直接从C#导入它。 There are workarounds, like exporting a managed type from a MC++ assembly, use a separate managed wrapper, using COM and a type library or something like that, but you can't use the same import/export mechanism C++ applications use. 有一些解决方法,比如从MC ++程序集导出托管类型,使用单独的托管包装,使用COM和类型库或类似的东西,但不能使用C ++应用程序使用的相同导入/导出机制。

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

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