简体   繁体   English

C#-无法访问C ++ / CLI .dll中包含的方法

[英]C# - Unable to Access Methods Included in C++/CLI .dll

I have two C++/CLI DLLs that I am trying to use with C#. 我有两个要与C#一起使用的C ++ / CLI DLL。 I am using VS10 and have made sure that the DLLs are appropriately included in the "References" folder. 我正在使用VS10,并确保DLL已正确包含在“参考”文件夹中。

So, the first DLL works great. 因此,第一个DLL效果很好。 It contains classes, so the C# application can access the functions within the classes by 1) creating an instance of a class found in the DLL, then 2) calling the function "normally." 它包含类,因此C#应用程序可以通过以下步骤访问这些类中的函数:1)创建DLL中找到的类的实例,然后2)“正常”调用该函数。 (eg instanceOfClass.method1(); ). (例如instanceOfClass.method1(); )。

The second DLL does not work. 第二个DLL不起作用。 In the second DLL, all functions are at a global scope. 在第二个DLL中,所有功能都位于全局范围内。 No namespace scopes or classes. 没有名称空间范围或类。 I am running into issues here, because when I try to access one of these functions in the C# application, I get an error: The name 'fxn' does not exist in the current context. 我在这里遇到了问题,因为当我尝试在C#应用程序中访问这些功能之一时,出现错误: The name 'fxn' does not exist in the current context. I have tried all of the following possible ways of addressing the function: 我尝试了以下所有可能的方法来解决该功能:

namespace myDLL {           // Tried with and without namespace declaration
    class Program {
        static void Main(string[] args) {
            fxnInitialize();
            myDLL.fxnInitialize();
            myDLL->fxnInitialize();    
        }
    }
}

All of the other questions I have found regarding this issue have been with C++ DLLs with classes. 我发现的与此问题有关的所有其他问题都是带有类的C ++ DLL。 My issue is: Is it possible to reference methods in DLLs that don't belong to a class? 我的问题是: 是否可以在属于类的DLL中引用方法?

  • If it is possible, what am I doing wrong? 如果可能,我在做什么错? I suspect I might be limiting the namespace (or some scope) in my C# application that is not allowing it see the contents of the DLL 我怀疑我可能限制了我的C#应用​​程序中的命名空间(或某些范围),而该命名空间不允许它查看DLL的内容

  • If it is not possible, why can't C# not use "class-less" DLLs? 如果不可能,为什么C#不能使用“无类” DLL? Is there a way to get around this, or another way to avoid using classes in the library (for easiest accessibility)? 是否有解决此问题的方法,或避免在库中使用类的另一种方法(以实现最简单的可访问性)?

If it is not possible, why can't C# not use "class-less" DLLs? 如果不可能,为什么C#不能使用“无类” DLL?

Because the CLR does not support it, methods must always be a member of a class. 因为CLR不支持它,所以方法必须始终是类的成员。 That's an impedance mismatch with C++'s support for free functions. 这与C ++对自由功能的支持不匹配。 The C++/CLI compiler solves it by moving them into a class anyway. C ++ / CLI编译器通过将它们移入类来解决该问题。 It's name is <Module> and it is not accessible outside of the assembly. 它的名称是<Module> ,在程序集外部无法访问。 Like an internal class in C#. 就像C#中的内部类一样。 In effect making the functions inaccessible as well. 实际上使这些功能也无法访问。

Solving it is simple, add them to a ref class. 解决起来很简单,将它们添加到ref类中。 You can declare them static . 您可以将它们声明为static

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

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