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; } < P> C代码能够获得C++ +DLL中低于的值 int i = 10; FunctionPtr1(i);
我得到了sedired输出,但是progrram最后由于以下执行而崩溃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; } < P> C代码能够获得C++ +DLL中低于的值 int i = 10; FunctionPtr1(i);,c#,c++,dll,dllimport,dllexport,C#,C++,Dll,Dllimport,Dllexport,我得到了sedired输出,但是progrram最后由于以下执行而崩溃 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() 为什么我会得到这个???很可能是你的代表 fPoint
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()
为什么我会得到这个???很可能是你的代表
fPointer abc = new fPointer(ping);
在垃圾被使用之前,先把它收集起来。
您需要将对委托的引用存储为类的一个字段,以确保它在类的生存期内继续存在
尝试在
main
外部定义fPointer abc
,然后在main
内部将新fPointer(ping)
分配给它,看看这是否有帮助。很可能是您的代表
fPointer abc = new fPointer(ping);
在垃圾被使用之前,先把它收集起来。
您需要将对委托的引用存储为类的一个字段,以确保它在类的生存期内继续存在
尝试在
main
外部定义fPointer abc
,然后在main
内部将新fPointer(ping)
分配给它,看看这是否有帮助。好的,我为您编写了测试代码。
概念很简单
#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;
}
#include "stdafx.h"
#include "MyDllCLR.h"
void MyDllCLR::MyFunc(int i)
{
MyDllCLR::Class::fun(i);
}
MyDllCLR.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;
}
#include "stdafx.h"
#include "MyDllCLR.h"
void MyDllCLR::MyFunc(int i)
{
MyDllCLR::Class::fun(i);
}
您的C#代码使用本机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();
}
}
}
Program.cs需要本机DLL和CLR DLL
当然,这不是实现目标的唯一途径 好的,我为您编写了测试代码。 概念很简单
#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;
}
#include "stdafx.h"
#include "MyDllCLR.h"
void MyDllCLR::MyFunc(int i)
{
MyDllCLR::Class::fun(i);
}
MyDllCLR.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;
}
#include "stdafx.h"
#include "MyDllCLR.h"
void MyDllCLR::MyFunc(int i)
{
MyDllCLR::Class::fun(i);
}
您的C#代码使用本机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();
}
}
}
Program.cs需要本机DLL和CLR DLL
当然,这不是实现目标的唯一途径 我需要做的就是。。
只需将我的代表声明为
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate();
我所需要做的就是。。
只需将我的代表声明为
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate();
用C++/CLI包C++ C++ DLL如何?根据我的经验,这要容易得多!我对这个新的…我如何实现它?用C++ +CLI包C++ C++ DLL怎么样?根据我的经验,这要容易得多!我是新手。我如何实现它?嗯,你确定呼叫约定是正确的吗?你试过stdcall而不是cdecl吗?虽然我假定C++代码中的C表示CDDEL,但它是在黑暗中刺伤。是否需要复制到FunctionPtr1?这是否等同于更改类型?您还可以尝试将
[Marshallas(UnmanagedType.FunctionPtr)]
属性添加到RegisterFunction dllimport语句中的参数。看这里:嗯,你确定通话惯例是正确的吗?你试过stdcall而不是cdecl吗?虽然我假定C++代码中的C表示CDDEL,但它是在黑暗中刺伤。是否需要复制到FunctionPtr1?这是否等同于更改类型?您还可以尝试将[Marshallas(UnmanagedType.FunctionPtr)]
属性添加到RegisterFunction dllimport语句中的参数。看这里:-你的概念很好。。。但我尝试将我的委托声明为[UnmanagedFunctionPointer(CallingConvention.Cdecl)]公共委托void MyDelegate();现在工作正常:)-你的概念很好。。。但我尝试将我的委托声明为[UnmanagedFunctionPointer(CallingConvention.Cdecl)]公共委托void MyDelegate();现在工作正常:)