c # — необработанное исключение: System.AccessViolationException: попытка чтения или записи

Ниже моя c ++ DLL

// 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 получит ссылку на имя функции из 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 parameterpublic static void ping(int a)
{
Console.WriteLine("ping executed " + a);
}

public static void add1()
{
Console.WriteLine("add executed");
}

}
}

C # код может получить значение, которое я paseed в C ++ DLL, как показано ниже

int i = 10;
FunctionPtr1(i);

M получает устаревший вывод, но в конце программа потерпела крах с последующим выполнением

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()

почему я получаю это ??

1

Решение

Хорошо, я написал тестовый код для вас.
Концепция проста.

  1. Вы написали DLL с использованием C ++ или C.

  2. Библиотека CLR (Managed dll) оборачивает вашу dll.

  3. Ваш код C # может использовать вашу Native DLL через библиотеку CLR.

Ваша родная DLL

MyDll.cpp

#include "stdafx.h"
#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;
}

Ваша библиотека CLR, оболочка для Native Dll

MyDllCLR.h

#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;

typedef void (*FunctionPtr2)(int);
extern "C" __declspec(dllimport)void Caller();
extern "C" __declspec(dllimport)void RegisterFunction(FunctionPtr2 func_ptr);

namespace MyDllCLR {

void MyFunc(int i);

public ref class Class
{
public:
delegate void FunctionDelegate(int i);
static FunctionDelegate^ fun;

static void Caller1()
{
Caller();
}

static void RegisterFunction1(FunctionDelegate^ f)
{
fun = f; // Wrapper MyFunc call this delegate

// this occurs runtime error and I don't know why.
// So I wrote Warpper MyFunc() method. I usually do like this.
//IntPtr p = Marshal::GetFunctionPointerForDelegate(fun);
//RegisterFunction((FunctionPtr2)(void*)p);

// Register Function Wrapper instead of user delegate.
RegisterFunction(MyFunc);
}
};
}

MyDllCLR.cpp

#include "stdafx.h"#include "MyDllCLR.h"
void MyDllCLR::MyFunc(int i)
{
MyDllCLR::Class::fun(i);
}

Ваш код C # с использованием Native DLL

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestMyDllCLR
{
class Program
{
static void MyFunc(int i)
{
Console.WriteLine("Come on! {0}", i);
}

static void Main(string[] args)
{
MyDllCLR.Class.RegisterFunction1(MyFunc);
MyDllCLR.Class.Caller1();
}
}
}

Вам нужны Native DLL и CLR DLL как для Program.cs

И, конечно же, это не единственный путь к вашей цели. 🙂

1

Другие решения

Вполне может быть, что ваш делегат

fPointer abc = new fPointer(ping);

собирает мусор перед использованием.
Вы должны сохранить ссылку на делегат как поле класса, чтобы обеспечить его сохранение в течение всего срока жизни класса.

Попробуйте определить fPointer abc вне main затем назначьте new fPointer(ping) к нему внутри main и посмотрим, поможет ли это.

0

Все, что мне нужно сделать, это ..
просто объявите моего делегата как …

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate();
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector