C#程序中调用C++动态库

GK_2014 2015-10-13 12:51:16
我用C++写了个动态库,该动态库导出一个加法函数,然后用C#的代码调用该动态库,运行时提示“找不到指定模块”?了解的前辈们帮我检查下是哪里不对?

下面是C++写的动态库

#include "stdafx.h"
extern "C" _declspec(dllexport) int sum(int a, int b)
{
return a+b;
}

下午是导出的函数:

然后将动态库复制到C#代码的工程目录
C#的测试代码如下:

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

namespace ImportCplusDll
{
class Program
{
[DllImport("c++_dll.dll", EntryPoint = "sum", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern int sum(int a, int b);

static void Main(string[] args)
{
int result = sum(3,5);
Console.WriteLine(result);
Console.ReadKey();
}
}
}

动态库调用失败,原因出在哪呢?
...全文
72 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
GK_2014 2015-10-13
  • 打赏
  • 举报
回复
引用 1 楼 findcaiyzh 的回复:
找不到指定模块?好像是找不到c++的dll。
你要把c++_dll.dll copy到 C#程序exe的同一个目录下。我这里是bin\Debug目录下

另外,在我的测试工程中,调用约定要使用
CallingConvention = CallingConvention.Cdecl


引用 1 楼 findcaiyzh 的回复:
找不到指定模块?好像是找不到c++的dll。
你要把c++_dll.dll copy到 C#程序exe的同一个目录下。我这里是bin\Debug目录下

另外,在我的测试工程中,调用约定要使用
CallingConvention = CallingConvention.Cdecl


点个赞!是你说的这两个问题。
宝_爸 2015-10-13
  • 打赏
  • 举报
回复
找不到指定模块?好像是找不到c++的dll。
你要把c++_dll.dll copy到 C#程序exe的同一个目录下。我这里是bin\Debug目录下

另外,在我的测试工程中,调用约定要使用
CallingConvention = CallingConvention.Cdecl

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧