C#非托管DLL在C ++中导出/导入 [英] C# unmanaged DLL Export / Import in C++

查看:74
本文介绍了C#非托管DLL在C ++中导出/导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0下来投票最爱





我在C#中有以下代码:(我使用R.Giesecke Dllexport模板创建一个非托管的使用C#的Dll ......它创建了一个带有(托管)C#的.lib和.dll文件。

0 down vote favorite


I have following Code in C#: (I used R.Giesecke Dllexport Template to create a unmanaged Dll with C#... it creates me a ".lib" and ".dll" file with (managed) C# )

//UnmanagedExports.cs
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace AddDll
{
   class MyAddDll
   {
      [DllExport("Add", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static double Add(double a, double b)
      {
         return a + b;
      }
   }
}



C#Dll代码运行良好且没有错误,并为我创建了一个AddDll.lib和AddDll.dll文件。



我想用C ++导入这个Dll:


The C# Dll Code runs well without errors and creats me a AddDll.lib and AddDll.dll file.

I would like to import this Dll in C++:

//CallAdd.h

#pragma once

#define DllImport __declspec(dllimport) 

namespace AddDll
{
    class MyAddDll
    {
    public:
        static DllImport double Add(double a, double b);
    };
}




// CallAdd.cpp

#include "stdafx.h"
#include"CallAdd.h"
#include <iostream>



using namespace std;

int main()
{
    double a = 4.5;
    double b = 5.5;

    cout << "a + b = " << AddDll::MyAddDll::Add(a, b) << endl;

    system("pause");

    return 0;
}



我有以下错误:




I've got following errors:

Warning 1 Platform is AnyCpu, creating binaries for each CPU platform in a separate subfolder...<br />
<br />
    Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static double __cdecl AddDll::MyAddDll::Add(double,double)" (__imp_?Add@MyAddDll@AddDll@@SANNN@Z) referenced in function _main<br />
<br />
    Error 3 error LNK1120: 1 unresolved externals





在Visual Studio中,我在C ++控制台应用程序属性下执行了以下操作:



- 在共同属性中 - >框架和参考。添加AddDll项目参考。

- 在配置属性中 - >链接器 - >输入。附加依赖项AddDll.lib

- 在配置属性中 - >链接器 - >一般。其他库目录,添加

目录,其中AddDll.lib和AddDll.dll位于



任何人都可以帮我消除错误吗? br />


提前Thx !!



In Visual Studio I did following under my C++ Console Application Properties:

- In Common Properties ‐> Framework and References. Add AddDll Project reference.
- In Configuration Properties ‐> Linker ‐> Input. Additional Dependencies AddDll.lib
- In Configuration Properties ‐> Linker ‐> General. Additional Library Directories, add
directory where AddDll.lib and AddDll.dll are located

Can anyone help me to eliminate the errors?

Thx in advance!!

推荐答案

你需要为同一个CPU构建C#-dll你的C ++项目。 :-O
you need to build the C#-dll for the same CPU as your C++ project. :-O


你有没有检查过这个



从非托管C ++ EXE中使用托管C#DLL - 无法启动CLR主机 [ ^ ]



-praveen
have you checked this

Consume Managed C# DLL from Unmanaged C++ EXE - Cannot start the CLR host[^]

-praveen


您也可以将C#代码编译为普通(.NET)代码,并将C ++代码编译为C ++ / CLI。这可以在同一文件中使用.NET和非托管代码。如果您需要在.NET和非托管C ++代码之间进行分离,您还可以在C ++ / CLI中编写一个包装器DLL来连接2。
You could also just compile your C# code as normal (.NET) code, and compile your C++ code as C++/CLI. That can use both .NET and unmanaged code in the same files. If you need separation between .NET and the unmanaged C++ code, you could also write a wrapper DLL in C++/CLI to interface between the 2.


这篇关于C#非托管DLL在C ++中导出/导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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