无法使用C ++ / CLI中的自定义.dll(C#) [英] Unable to use Custom .dll (C#) from C++/CLI

查看:224
本文介绍了无法使用C ++ / CLI中的自定义.dll(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我试图在C ++项目中使用我的C#库

p>

我做了什么


  1. dll文件与我的C#项目。命名为:ClassLibrary1.dll

  2. 创建了一个名为的C ++控制台应用程序:CppClr(Common Language RunTime支持更改为:/ clr)

  3. 复制ClassLibrary1.dll


  4. 以下是我的ClassLibrary1(C#)代码:

     使用系统; 
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;


    命名空间ClassLibrary1
    {
    public class Class1
    {
    public static void output()
    {

    String mystr;


    mystr =Helloo World; //创建一个新的字符串

    Console.WriteLine(mystr);
    Console.WriteLine(From C ++ Managed Code!);
    Console.ReadLine();
    }
    }
    }

    以下是我的C ++ / CLI代码:

      #includeCppClr.h
    #using< ClassLibrary1.dll>

    int main()
    {
    ClassLibrary1 :: Class1 :: output();
    }

    问题:我的C ++ / CLI代码:


    未知模块中发生类型为System.IO.FileNotFoundException的未处理异常。加载文件或程序集ClassLibrary1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其一个依赖项。系统找不到指定的档案。



    解决方案

    可执行文件不能在运行时找到/加载C#DLL。



    .NET运行库有几个不同的地方加载程序集。最简单的是从运行编译应用程序的同一目录。在你的C ++项目的根目录中有 ClassLibrary1.dll 是不够的,它需要在运行项目的同一个目录中。



    作为一个快速和脏的测试,你应该能够通过将 ClassLibrary1.dll 复制到C ++项目的输出目录来验证这一点。如果你在Debug配置中运行,这可能会像 C:\Projects\CppClr\Debug\ 或者你的C ++项目。验证这是包含编译的 CppClr.exe 文件的目录。一旦你复制这里的DLL和运行你的项目,运行时应该能够找到DLL和所有应该是好的。



    然而,这可能是一种痛苦如果你经常更新你的C#项目,因为你会不断需要将它复制到C ++输出文件夹每次有变化。



    在Visual Studio中,打开你的C ++项目。从解决方案资源管理器中,右键点击参考,然后选择添加参考... 。接下来,点击浏览... 底部,并浏览到磁盘上 ClassLibrary1.dll 的位置。点击添加,然后点击确定



    现在,无论何时构建C ++项目, code> ClassLibrary1.dll 程序集到项目的输出文件夹中。您还应该能够从C ++文件的顶部删除 #using< ClassLibrary1.dll> 指令,因为添加DLL作为项目引用应该执行相同的函数。


    Thanks In advance for you time and Help.

    Goal: I am trying to use my C# library within a C++ project

    What I did:

    1. Created a simple .dll file with my C# project. named: ClassLibrary1.dll
    2. Created a C++ console application named: CppClr (Common Language RunTime Support Changed to: /clr)
    3. Copied ClassLibrary1.dll to the root off CppClr project

    Following is my ClassLibrary1(C#) code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace ClassLibrary1
    {
        public class Class1
        {
            public static void output()
            {
    
                String mystr; 
    
    
                mystr = "Helloo World"; // create a new string
    
                Console.WriteLine(mystr);
                Console.WriteLine("From C++ Managed Code!");
                Console.ReadLine();
            }
        }
    }
    

    The Following is my C++/CLI code:

    #include "CppClr.h"
    #using <ClassLibrary1.dll>
    
    int main()
    {
        ClassLibrary1::Class1::output();
    }
    

    Problem: The error I get when I run my C++/CLI code :

    An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.Additional information: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    解决方案

    Your problem is simply an issue of your C++ executable not being able find/load the C# DLL at runtime.

    There are a few different places that the .NET runtime looks to load assemblies. The simplest of these is from the same directory that your compiled application is running from. It is not enough to have ClassLibrary1.dll in the root of your C++ project - it needs to be in same directory that your project is running from.

    As a quick and dirty test, you should be able to verify this by copying ClassLibrary1.dll into the C++ project's output directory. If you're running in the Debug configuration, this will probably be something like C:\Projects\CppClr\Debug\ or wherever your C++ project is. Verify that this is the directory that contains the compiled CppClr.exe file. Once you copy the DLL here and run your project, the runtime should be able to find the DLL and all should be good.

    However, this could be kind of a pain if you're frequently updating your C# project, since you'll continually need to copy it to the C++ output folder every time something changes. The solution to this is to add a reference in the C++ project to your C# library.

    In Visual Studio, open up your C++ project. From the solution explorer, right-click on references and select Add reference... Next, click on the Browse... button at the bottom of the dialog and browse to the location of ClassLibrary1.dll on your disk. Click on Add and then OK.

    Now, whenever you build the C++ project, it will copy the ClassLibrary1.dll assembly into the project's output folder. You should also be able to remove the #using <ClassLibrary1.dll> directive from the top of your C++ file, since adding the DLL as a project reference should perform the same function.

    这篇关于无法使用C ++ / CLI中的自定义.dll(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆