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

解决方案 »

  1.   

    这么简单的函数,不应该出什么错呀。
    把CallingConvention 去掉试试或者改成别的
      

  2.   

    + $exception {"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"} System.Exception {System.AccessViolationException}
    修改运行后提示如上
      

  3.   

    我调试了
    就是在返回C值时出现了问题
    a和b的值我也已经传到DLL的函数中了
    也进行了运算
    就是返回时报错
      

  4.   

    我测试了下,在你的基础上加入了头文件extern "C" int _declspec(dllexport) calculate(int a,int b);#include "Calculation.h"int calculate(int a,int b)
    {
    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();
            }
        }
    }
      

  5.   

    谢谢啊 
    找到问题了
    把dll字符集改成使用Unicode字符集 就可以了 
    开始我用的事多字节字符集