C++的DLL函数
extern "c" _declspec(dllexport) int _stdcall ceshi(int a,int b)
{
int c;
c = a + b;
return c;
}c#中调用
[DllImport("PlayClient.dll", EntryPoint = "ceshi", CallingConvention = CallingConvention.StdCall)]
private static extern int ceshi(int a1,int b1);调用函数
int c;
c = cechi(1,2);
MessageBox.Show(c.tostring());运行错误
Debug Assertion Failed;
file:d:\microsoft cisual studio 8........\atlsimpstr.h
line:107
expression:nrefs != 0
for information on how your program can cause an assertion failure,see the visual c++ documentation on asserts
extern "c" _declspec(dllexport) int _stdcall ceshi(int a,int b)
{
int c;
c = a + b;
return c;
}c#中调用
[DllImport("PlayClient.dll", EntryPoint = "ceshi", CallingConvention = CallingConvention.StdCall)]
private static extern int ceshi(int a1,int b1);调用函数
int c;
c = cechi(1,2);
MessageBox.Show(c.tostring());运行错误
Debug Assertion Failed;
file:d:\microsoft cisual studio 8........\atlsimpstr.h
line:107
expression:nrefs != 0
for information on how your program can cause an assertion failure,see the visual c++ documentation on asserts
把CallingConvention 去掉试试或者改成别的
修改运行后提示如上
就是在返回C值时出现了问题
a和b的值我也已经传到DLL的函数中了
也进行了运算
就是返回时报错
{
int c;
c = a + b;
return c;
}
测试代码using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace InvokeDLL
{ class Program
{
[DllImport("Win32DLL.dll", EntryPoint = "calculate", CallingConvention = CallingConvention.StdCall)]
private static extern int calculate(int a1, int b1); static void Main(string[] args)
{
int c;
c = calculate(1, 2);
Console.WriteLine(c);
Console.ReadKey();
}
}
}
找到问题了
把dll字符集改成使用Unicode字符集 就可以了
开始我用的事多字节字符集