C#程序中调用C++动态库提示找不到指定模块

.Net技术 码拜 9年前 (2015-10-13) 3362次浏览
本人用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();
        }
    }
}

动态库调用失败,原因出在哪呢?

解决方案:40分
找不到指定模块?好像是找不到c++的dll。你要把c++_dll.dll copy到 C#程序exe的同一个目录下。本人这里是bin\Debug目录下

另外,在本人的测试工程中,调用约定要使用

CallingConvention = CallingConvention.Cdecl


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#程序中调用C++动态库提示找不到指定模块
喜欢 (0)
[1034331897@qq.com]
分享 (0)