我正在尝试创建一个C#程序,它可以将数据传递到C / C ++ DLL,存储数据并完成计算。然后将结果返回给C#程序。不使用C#进行计算的原因是我计划用CUDA加速它。
我目前在C#中声明一个字节数组,并将其作为参数传递给DLL以发送数据。
然后我在C#中声明一个大小相同的字节数组,并将其作为参数传递以接收数据。然后DLL应该将结果写入该内存。
当我运行程序时,正确的数字会打印到屏幕上。 (因为我正在向阵列写入1,2,3 ......两次,加上这些是2,4,6 ......)然后打印出“追逐鼠标的猫”。当我按下一个键时,显示“鼠标追逐猫”,程序崩溃后退出显示vshost32.exe停止工作错误。 我能够从this question解决此错误。
现在,当我尝试直接从Windows资源管理器运行EXE时,我第一步打印出数字36,但是我在计算函数调用上得到以下错误:
“Unhanded exception:System.AccessViolationException试图读取 或写保护的内存。这通常表明其他 记忆已腐败“
我知道DLL正确加载,因为init函数返回了正确的数字。我不确定为什么它只会在我作为EXE而不是在调试器中运行时崩溃。可能导致这种情况的原因是什么?
我应该采用不同的方式处理DLL和C#程序之间的内存交换吗?我的目标是避免使用Marshal Copy复制它,因为我需要这是高性能。最终目标是将GPU复制到GPU或从GPU进行计算的DLL,因此我认为没有理由在转到GPU之前在DLL中复制第二次。
C ++ DLL Code(编写为C代码)
#include "stdafx.h"
#include "stdlib.h"
const int CONSTANT = 3;
char** items;
int itemCount = 0;
int xcnt = -1;
int ycnt = -1;
extern "C" int __declspec(dllexport) __stdcall init(int itemcount_local, int xcnt_local, int ycnt_local){
itemCount = itemcount_local;
xcnt = xcnt_local;
ycnt = ycnt_local;
items = (char**)malloc(itemCount);
if (items == NULL){
return -1;
}
for (int i = 0; i < itemCount; i++){
items[i] = (char*)malloc(CONSTANT*xcnt*ycnt);
if (items[i] == NULL){
return -1;
}
for (int d = 0; d < CONSTANT*xcnt*ycnt; d++){
items[i][d] = 0;
}
}
return CONSTANT*xcnt*ycnt;
}
//frees the memory created in init
extern "C" void __declspec(dllexport) __stdcall cleanup(){
for (int i = 0; i < itemCount; i++){
free(items[i]);
}
free(items);
return;
}
extern "C" void __declspec(dllexport) __stdcall loadItem(int index, char* data){
memcpy(items[index], data, CONSTANT*xcnt*ycnt);
return;
}
//fills the buffer with the computed result. Buffer needs to be the size of CONSTANT*xcnt*ycnt
extern "C" void __declspec(dllexport) __stdcall compute(char* buffer){
for (int x = 0; x < CONSTANT*xcnt*ycnt; x++){
int sum = 0;
for (int i = 0; i < itemCount; i++){
sum += items[i][x];
}
if (sum > 255){ //saturate at 255
buffer[x] = 255;
}
else {
buffer[x] = sum;
}
}
return;
}
C#代码
class Program
{
[DllImport("DllTest1.dll", CallingConvention=CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern int init(int imagecount_local, int xres_local, int yres_local);
[DllImport("DllTest1.dll", CallingConvention=CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern void cleanup();
[DllImport("DllTest1.dll", CallingConvention=CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern void loadItem(int index, byte[] data);
[DllImport("DllTest1.dll", CallingConvention=CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern void compute(byte[] buffer);
static void Main(string[] args)
{
int x = 4, y = 3;
int total = x * y * 3;
Console.WriteLine(init(5, x, y));
byte[] a = new byte[total];
compute(a);
for (int i = 0; i < total; i++)
{
Console.WriteLine(a[i]);
}
byte[] b = new byte[total];
for (int i = 0; i < total; i++)
{
b[i] = (byte)i;
}
loadItem(0, b);
loadItem(1, b);
compute(a);
for (int i = 0; i < total; i++)
{
Console.WriteLine(a[i]);
}
//cleanup();
Console.WriteLine("The cat chased the mouse");
Console.ReadKey();
Console.WriteLine("The mouse chased the cat");
return;
}
}
答案 0 :(得分:1)
我能够解决问题。这条线
items = (char**)malloc(itemCount);
应该是
items = (char**)malloc(itemCount*sizeof(char*));