将数组传递给C DLL

编程入门 行业动态 更新时间:2024-10-26 19:30:15
本文介绍了将数组传递给C DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿 我在从C#调用C DLL时遇到一些问题。我使用一个简单的 函数,它接受一个浮点数组和一个整数作为输入,但是 我似乎无法让它工作。当我尝试编译时,我得到 以下错误: 尝试读取或写入受保护的内存 C函数不应该操纵输入arra,只读它 它。有人可以帮忙吗?代码如下: 命名空间DLLTest { class HellDLL { [DllImport(" FFT.DLL")] public static extern unsafe void FFT(float [] x,int n); } class program { static void Main(string [] args) { int i,N = 4096; float [] noise = new float [N]; for(i = 0; i< N; i ++) { noise [i] =(float)i; } 不安全 { HellDLL.FFT(噪音,N); } } } }

hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only reading it. can anyone help? code is below: namespace DLLTest { class HellDLL { [DllImport("FFT.DLL")] public static extern unsafe void FFT(float[] x, int n); } class Program { static void Main(string[] args) { int i, N = 4096; float[] noise = new float[N]; for (i = 0; i < N; i++) { noise[i] = (float)i; } unsafe { HellDLL.FFT(noise, N); } } } }

推荐答案

你能提供功能的声明吗?在C?在不知道第一个问题的情况下说出什么是错误是不可能的。 - - Nicholas Paldino [.NET / C#MVP ] - mv*@spam.guard.caspershouse < mr ********* @ gmail>在消息中写道 news:11 ********************** @ i40g2000cwc.googlegr oups ... Can you provide the declaration of the function in C? It''s impossible to say what is wrong without knowing that first. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse <mr*********@gmail> wrote in message news:11**********************@i40g2000cwc.googlegr oups... 嘿 我在从C#调用C DLL时遇到一些问题。我正在使用一个简单的函数,它接受一个浮点数组和一个整数作为输入,但是我似乎无法让它工作。当我尝试编译时我得到了以下错误: 尝试读取或写入受保护的内存 C函数不应该操作输入arra,只有读它它。有人可以帮忙吗?代码如下: 命名空间DLLTest {HellDLL类 {DllImport(" FFT.DLL")] 公开static extern unsafe void FFT(float [] x,int n); } class program { static void Main(string [] args) { int i,N = 4096; float [] noise = new float [N]; for(i = 0; i< N; i ++) { noise [i] =(float)i; } 不安全 {HellDLL.FFT(noise,N); } } } } hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only reading it. can anyone help? code is below: namespace DLLTest { class HellDLL { [DllImport("FFT.DLL")] public static extern unsafe void FFT(float[] x, int n); } class Program { static void Main(string[] args) { int i, N = 4096; float[] noise = new float[N]; for (i = 0; i < N; i++) { noise[i] = (float)i; } unsafe { HellDLL.FFT(noise, N); } } } }

嘿 这里是C函数声明: DLLIMPORT void FFT(float x [],int n); 函数是一个高性能的FFT算法,它mallocs一堆 数组,将输入数组x复制到其中一个,加载m使用SSE内在函数然后将结果复制回x。 这是一个合适的方法吗?由于一些奇怪的原因, 程序第一次运行它,但从那以后它给了我一个 损坏的内存错误。 有人可以帮忙吗? hey here is the C function declaration: DLLIMPORT void FFT(float x[], int n); the function is a high performance FFT algorithm, it mallocs a bunch of arrays, copies the input array x into one of them, does a load of maths using SSE intrinsics, then copies the result back into x. is this a suitable way to go about this? for some strange reason the program worked the first time i ran it, but since then it gives me a corrupt memory error. can anyone help?

> public static extern unsafe void FFT(float [] x,int n); 您可能想尝试 public static extern unsafe void FFT([In,Out] float [] x,int n); ^^^^^^ 认为你可能需要Out属性,因为本机方法正在更新数组。 另外,真的需要将它标记为不安全吗? /> Marcus mr ******* **@gmail 写道: >public static extern unsafe void FFT(float[] x, int n); You might want to try public static extern unsafe void FFT([In,Out]float[] x, int n); ^^^^^^ In think you might need the Out attribute since the native method is updating the array. Also, do really need to mark it as unsafe? Marcus mr*********@gmail wrote: 嘿 我在从C#调用C DLL时遇到一些问题。我正在使用一个简单的函数,它接受一个浮点数组和一个整数作为输入,但是我似乎无法让它工作。当我尝试编译时我得到了以下错误: 尝试读取或写入受保护的内存 C函数不应该操作输入arra,只有读它它。有人可以帮忙吗?代码如下: 命名空间DLLTest {HellDLL类 {DllImport(" FFT.DLL")] 公开static extern unsafe void FFT(float [] x,int n); } class program { static void Main(string [] args) { int i,N = 4096; float [] noise = new float [N]; for(i = 0; i< N; i ++) { noise [i] =(float)i; } 不安全 {HellDLL.FFT(noise,N); } } } } hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only reading it. can anyone help? code is below: namespace DLLTest { class HellDLL { [DllImport("FFT.DLL")] public static extern unsafe void FFT(float[] x, int n); } class Program { static void Main(string[] args) { int i, N = 4096; float[] noise = new float[N]; for (i = 0; i < N; i++) { noise[i] = (float)i; } unsafe { HellDLL.FFT(noise, N); } } } }

更多推荐

将数组传递给C DLL

本文发布于:2023-07-29 19:33:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   DLL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!