如何将C静态库暴露给.Net? [英] How to Expose C Static Library to .Net?

查看:216
本文介绍了如何将C静态库暴露给.Net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将C ++函数暴露给C和.Net的步骤是什么?我想在C ++,C和.Net中为32位和64位版本使用相同的函数名。



我发布这个问题和答案,因为我

解决方案

这些步骤是:


  1. 将C ++函数公开为C静态库(.lib)。


  2. 使用 #pragma 语句将lib的函数投影到DLL中。


有关示例,请参阅此github项目。



步骤1.创建一个静态库项目。创建一个将每个函数作为C函数公开的C ++文件。

  externC{
void vfunc ){return; }
int ifunc(int i){return i; }
}

步骤2.创建一个DLL项目。将静态库指定为其他依赖关系。创建一个C文件并放置 #pragma 语句将每个静态库函数投影到.DLL中。你需要定义两个#pragmas,一个用于32位,另一个用于64位。区别在于32位在函数名称前需要一个前导下划线。没有必要的代码,只需 #pragma

  #ifdef _M_X64 
#define别名(func)注释(链接器,/ export:func=func)
#else
# = _func)
#endif

#pragma别名(vfunc)
#pragma别名(ifunc)

步骤3.创建.Net项目。使用 DllImport 属性公开每个函数。

  b {
[DllImport(c-dll.dll)]
extern static void vfunc();
[DllImport(c-dll.dll)]
extern static void ifunc();
static void Main(string [] args)
{
vfunc();
int i = ifunc(1);
}
}

使用这些技术编写代码非常简单,需要对解决方案和项目文件进行相当多的编辑。


  1. 设置解决方案的正确构建顺序。该DLL将取决于静态库。


  2. 手动编辑DLL项目文件以将32位和64位DLL构建到单独的目录中


  3. 手动编辑.Net项目文件以创建x86 / x64和Debug / Release 4总计)。请勿使用 AnyCPU DllImport 不能使用 AnyCPU 。仅使用x86 / x64。


  4. 手动编辑.Net项目文件输出路径为与上述相同的Win32 / x64目录。这将允许 DllImport 找到DLL,因为DLL / EXE共享同一目录。你也可以把DLL放到一个可发现的目录中,或者在DllImport中放置一个路径 - 但是当你使用两个DLL(32/64)时会出现问题。



What are the steps to expose C++ functions to C and .Net? I want to use the same function names in C++, C and .Net for 32-bit and 64-bit builds.

I'm posting this question and answer because I haven't found these techniques documented anywhere.

解决方案

The steps are:

  1. Expose C++ functions as a C static library (.lib).

  2. Use #pragma statements to project the lib's functions into a DLL.

  3. Use .Net's DllImport attribute to expose the DLL's functions.

For an example, see this github project.

Step 1. Create a static library project. Create a C++ file that exposes each function as a C function.

extern "C" {
  void vfunc(void) { return; }
  int ifunc(int i) { return i; }
}

Step 2. Create a DLL project. Specify the static library as an "Additional Dependency". Create a C file and put #pragma statements to project each static lib function into the .DLL. You'll need to define two #pragmas, one for 32-bit and another for 64-bit. The difference being that 32-bit requires a leading underscore before the function name. No code necessary, just #pragmas.

#ifdef _M_X64
#define Alias(func) comment(linker, "/export:" func "=" func)
#else
#define Alias(func) comment(linker, "/export:" func "=_" func)
#endif

#pragma Alias("vfunc")
#pragma Alias("ifunc")

Step 3. Create a .Net project. Use DllImport attribute to expose each function.

class Program
{
    [DllImport("c-dll.dll")]
    extern static void vfunc();
    [DllImport("c-dll.dll")]
    extern static void ifunc();
    static void Main(string[] args)
    {
        vfunc();
        int i = ifunc(1);
    }
}

While coding is straightforward using these techniques, you'll need to do quite a bit of editing of solution and project files.

  1. Set the solution's correct build order. The DLL will depend on the static library. The .Net projects will depend on the DLL.

  2. Manually edit the DLL project file to build the 32-bit and 64-bit DLLs into separate directories (e.g. Win32\Debug, x64\Debug).

  3. Manually edit the .Net project files to create sections for combinations of x86/x64 and Debug/Release (4 total). DO NOT USE AnyCPU. DllImport WILL NOT WORK WITH AnyCPU. USE ONLY x86/x64.

  4. Manually edit the .Net project file output path to be the same Win32/x64 directories as above. This will allow DllImport to find the DLL since DLL/EXE share the same directory. Optionally you can put the DLL into a discoverable directory or put a path in DllImport -- but that's problematic when you are using two DLLs (32/64).

这篇关于如何将C静态库暴露给.Net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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