从C调用C#DLL;链接问题 [英] Calling a C# DLL from C; linking problems

查看:52
本文介绍了从C调用C#DLL;链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2012,并尝试从我编写的C DLL中调用C#项目中的函数.外部应用程序(Thunderbird)调用了C DLL.

I'm using VS2012 and trying to call a function in a C# project from a C DLL I've written. The C DLL is called by an external app (Thunderbird).

我找到了不受管理的出口页,然后按照说明进行操作.在此过程中,我的C#项目必须设置为.net4和x86,以使所有内容都能进行编译和链接.但是我有一个无法解决的链接器错误,由于这对我来说是未知的领域,可能是我在做一些愚蠢的事情.

I've found the Unmanaged Exports page and followed the instructions. In the process, my C# project had to be set to .net4 and x86 to get everything to compile and link as far as it does. But I have a linker error I can't resolve, and since this is uncharted territory for me it may be I'm just doing something stupid.

我的C#中有

 using RGiesecke.DllExport;

 namespace StreamAttacher
 {
     class Test
     {
         [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
         public static int TestExport(int left, int right)
         {
             return left + right;
         }
     }
 }

在我的C语言中

 #include<stdio.h>

 int add(int left, int right); //in c#

 __declspec(dllexport)char* strTest2()
 {
    int i = add(2,3);
    char *p = "C:\\Work\\Mozilla\\Test dir\\testfile.zip";
    return p;
 }

C项目在其外部依赖项中引用了C#项目.

The C project references the C# project in its external dependencies.

在建筑物上,出现链接器错误错误LNK2019:函数_strTest2中引用的未解析的外部符号_add"

On building, I get the linker error "error LNK2019: unresolved external symbol _add referenced in function _strTest2"

如果我使用DllExp查看C#DLL,则看不到任何导出内容.我是不是该?我原本希望看到添加"功能,但是它不存在,而且当我自己构建C#DLL时也不会产生任何错误.

If I look at the C# DLL using DllExp then I see nothing exported. Should I? I was expecting to see the "add" function but it's not there, and nor are any errors generated when I build the C# DLL on its own.

此外,如果事情在链中进一步发展的话,那么C语言中"add"的原型就是我所需要的吗?

Also, is that proto for 'add' in the C code all I need if things are working right further back along the chain?

推荐答案

我最近尝试出于相同的目的(使用RGiesecke的库和其他方法)导出C#函数,无法使其正常工作,并且发现它非常繁琐.如果您可以选择尝试其他方法,我建议您考虑编写一个C ++/CLI库作为中介.对我来说,这是一个更可互操作的解决方案.

I recently tried exporting C# functions for the same purpose (using RGiesecke's library and other methods), couldn't get it to work and found it to be very hairy. If you have the option to try something else, I would suggest looking into writing a C++/CLI library as an intermediate instead. To me, it is a much more interopable solution.

这是一个不错的教程: http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-小于10分钟

Here is a decent tutorial: http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes

这篇关于从C调用C#DLL;链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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