简体   繁体   English

从 C# 应用程序调用 c++ DLL

[英]Call c++ DLL from C# application

I have C# as my front end application and I want to call c++ dll from my c# but I am getting error.我有 C# 作为我的前端应用程序,我想从我的 Z240AA2CEC4B29C56F3BEE520ADC 中调用 c++ dll 我得到了 EZEE 错误。 I am posting my code here please help me out how to resolve this:我在这里发布我的代码,请帮助我解决这个问题:

Program.cs程序.cs

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCSharp
{
     class Program
     {
          [DllImport("C:\\Users\\xyz\\source\\repos\\Project1\\Debug\\TestCpp.dll", CallingConvention = CallingConvention.Cdecl)]
          public static extern void DisplayHelloFromDLL(StringBuilder name, int appId);

          static void Main(string[] args)
          {
               try
               {
                   StringBuilder str = new StringBuilder("name");                
                   DisplayHelloFromDLL(str, str.Length);
                   str.Clear();
               }
               catch(DllNotFoundException exception)
               {
                    Console.WriteLine(exception.Message);
               }
               catch(Exception exception)
               {
                   Console.WriteLine("General exception " + exception.Message);
               }
               finally
               {
                   Console.WriteLine("Try again");
               }
          }
     }
 }

and cpp code like below:和cpp代码如下:

Header: source.h Header:source.h

#include <string>
using namespace std;

extern "C"
{
    namespace Test
    {
        class test
        {
        public:
            test();
            __declspec(dllexport) void DisplayHelloFromDLL(char * name, int appId);
        }
    }
}

c++ class: source.cpp c++ class:source.cpp

#include <stdio.h>
#include "source.h"

Test::test::test()
{
    printf("This is default constructor");
}
void Test::test::DisplayHelloFromDLL(char * name, int appId)
{
    printf("Hello from DLL !\n");
    printf("Name is %s\n", name);
    printf("Length is %d \n", appId);
}

Code is building successfully but when I run this I got Unable to find an entry point named 'DisplayHelloFromDLL' in DLL.代码构建成功,但是当我运行它时,我无法在 DLL 中找到名为“DisplayHelloFromDLL”的入口点

Same CPP code when I am writing without using namespace and class, it is working fine.当我在不使用命名空间和 class 的情况下编写相同的 CPP 代码时,它工作正常。 ie IE

Header: source.h Header:source.h

extern "C"
{
    __declspec(dllexport) void DisplayHelloFromDLL(char * name, int appId);
}

c++ class: source.cpp c++ class:source.cpp

#include "source.h"

void DisplayHelloFromDLL(char * name, int appId)
{
    printf("Hello from DLL !\n");
    printf("Name is %s\n", name);
    printf("Length is %d \n", appId);
}

So how do I use DLL which has namespaces and claases in my c# application.那么如何在我的 c# 应用程序中使用具有命名空间和分类的 DLL。

Thanks for the answers.感谢您的回答。 I resolved the issue by making one extra class(Wrapper class) that contains the managed code.我通过制作一个包含托管代码的额外类(包装类)解决了这个问题。 This wrapper class is called by the c# classes in the same way as I mentioned in the question.这个包装器 class 由 c# 类调用,方法与我在问题中提到的相同。 This wrapper class than call the c++ class and return the result to the UI.这个包装器 class 比调用 c++ class 并将结果返回给 UI。

The easiest way is to create a "proxy": set of clear-C functions, these will call your c++ functions.最简单的方法是创建一个“代理”:一组 clear-C 函数,这些函数将调用您的 c++ 函数。 I think calling c++ function is not good idea: name decoration is changed from version to version of compiler.我认为调用 c++ function 不是一个好主意:名称装饰从编译器版本更改为版本。

Do you have this project hosted somewhere?你有这个项目托管在某个地方吗? On the first view I would say that you need to build the c++ project first (only the c++.:!) and then run the C# project.在第一个视图中,我会说您需要先构建 c++ 项目(仅 c++.:!),然后运行 C# 项目。 Perhaps you would like to have a look here: Testprojects Especially the "MessageBox" stuff shows how to use C++ with C#.也许你想看看这里: Testprojects特别是“MessageBox”的东西展示了如何使用 C++ 和 C#。 There are some Testprojects with UWP as well.还有一些带有 UWP 的测试项目。

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

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