将数组传递给C DLL [英] passing array to 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.com
>
< mr ********* @ gmail.com>在消息中写道
news:11 ********************** @ i40g2000cwc.googlegr oups.com ...
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.com
<mr*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
嘿
我在从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.com 写道:
>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.com 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!