C#程序中调用C++动态库解决办法
C#程序中调用C++动态库
我用C++写了个动态库,该动态库导出一个加法函数,然后用C#的代码调用该动态库,运行时提示“找不到指定模块”?了解的前辈们帮我检查下是哪里不对?
下面是C++写的动态库
下午是导出的函数:

然后将动态库复制到C#代码的工程目录
C#的测试代码如下:
动态库调用失败,原因出在哪呢?
------解决思路----------------------
找不到指定模块?好像是找不到c++的dll。
你要把c++_dll.dll copy到 C#程序exe的同一个目录下。我这里是bin\Debug目录下
另外,在我的测试工程中,调用约定要使用
CallingConvention = CallingConvention.Cdecl
我用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();
}
}
}
动态库调用失败,原因出在哪呢?
------解决思路----------------------
找不到指定模块?好像是找不到c++的dll。
你要把c++_dll.dll copy到 C#程序exe的同一个目录下。我这里是bin\Debug目录下
另外,在我的测试工程中,调用约定要使用
CallingConvention = CallingConvention.Cdecl