下面是我的C++ DLL未處理的異常:System.AccessViolationException:嘗試讀取或寫入
// DLL.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
//#include <stdexcept>
#include<iostream>
using namespace std;
typedef void (*FunctionPtr)(int);
void (*FunctionPtr1)(int);
extern "C" __declspec(dllexport)void Caller();
extern "C" __declspec(dllexport)void RegisterFunction(FunctionPtr func_ptr);
extern void Caller()
{
int i = 10;
FunctionPtr1(i);
}
extern void RegisterFunction(FunctionPtr func_ptr1)
{
FunctionPtr1 = func_ptr1;
}
這個DLL會得到refernce從C#這裏函數名和參數傳遞給C#功能.. 是我的C#代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace test
{
class Program
{
[DllImport("C:/Users/10602857/Documents/Visual Studio 2010/Projects/DLL/Debug/DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Caller();
[DllImport("C:/Users/10602857/Documents/Visual Studio 2010/Projects/DLL/Debug/DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern fPointer RegisterFunction(fPointer aa);
static void Main(string[] args)
{
Console.WriteLine("DLL Linking");
fPointer abc = new fPointer(ping);
RegisterFunction(abc); //send address of function to DLL
Caller(); //call from DLL
}
public delegate void fPointer(int s); // point to every functions that it has void as return value and with no input parameter
public static void ping(int a)
{
Console.WriteLine("ping executed " + a);
}
public static void add1()
{
Console.WriteLine("add executed");
}
}
}
C#代碼是能得到我在C++ DLL paseed如下值
int i = 10;
FunctionPtr1(i);
m到處的sedired輸出,但progrram在年底得到了與墜毀爲什麼我收到此以下execption
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at test.Program.Caller()
?
什麼包裹C++使用C++/CLI的dll?根據我的經驗,這很容易! –
我是新來這..我可以實現它嗎? –