473,626 Members | 3,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing array to C DLL

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(noi se, N);
}

}
}
}

Apr 4 '06 #1
17 3671
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.co m

<mr*********@gm ail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
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(noi se, N);
}

}
}
}

Apr 4 '06 #2
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?

Apr 4 '06 #3
>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*********@gma il.com wrote:
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(noi se, N);
}

}
}
}

Apr 4 '06 #4
ok, have removed unsafe, currently calling it as:

[DllImport("FFT. dll")]
public static extern void FFT([Out]float[] x, int n);

but with the same problem. i have worked out that the error only occurs
when the C DLL tries to use the SSE commands such as:

_mm_load_ps
_mm_store_ps

etc. can you think why this error is occuring? i can call the DLL just
fine from a C program, so it is definitely functioning.

Apr 4 '06 #5
Note that you can't pass the C# float array to SSE instructions directly.
There is no guarantee that the managed array is 16 byte aligned, so you'll
have to marshal the array to a 16 byte aligned array before passing to SSE.
I'm not entirely clear why you want to use this from managed code anyway,
you won't realize any performance gain by marshaling from managed to
unmanaged, you better use unmanaged or managed code only for this.

Willy.

<mr*********@gm ail.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
| ok, have removed unsafe, currently calling it as:
|
| [DllImport("FFT. dll")]
| public static extern void FFT([Out]float[] x, int n);
|
| but with the same problem. i have worked out that the error only occurs
| when the C DLL tries to use the SSE commands such as:
|
| _mm_load_ps
| _mm_store_ps
|
| etc. can you think why this error is occuring? i can call the DLL just
| fine from a C program, so it is definitely functioning.
|
Apr 4 '06 #6
Willy,
I'm not entirely clear why you want to use this from managed code anyway,
you won't realize any performance gain by marshaling from managed to
unmanaged, you better use unmanaged or managed code only for this. For my own understanding, you are only referring to marshallings the 16 byte aligned arrays here? For the non-aligned
array, my understanding (from [1]) is that that marshallings overhead is minimal when marshallings a float/double array.
The runtime just pins the pointer to the array and passes it on to the unmanaged function.

I'm curious if using _mm_loadu_ps and _mm_storeu_ps instead of _mm_load_ps and _mm_store_ps would solve the problem.

Thanks,
Marcus
[1] http://msdn2.microsoft.com/en-us/library/75dwhxf7.aspx
Willy Denoyette [MVP] wrote: Note that you can't pass the C# float array to SSE instructions directly.
There is no guarantee that the managed array is 16 byte aligned, so you'll
have to marshal the array to a 16 byte aligned array before passing to SSE.
I'm not entirely clear why you want to use this from managed code anyway,
you won't realize any performance gain by marshaling from managed to
unmanaged, you better use unmanaged or managed code only for this.

Willy.

<mr*********@gm ail.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
| ok, have removed unsafe, currently calling it as:
|
| [DllImport("FFT. dll")]
| public static extern void FFT([Out]float[] x, int n);
|
| but with the same problem. i have worked out that the error only occurs
| when the C DLL tries to use the SSE commands such as:
|
| _mm_load_ps
| _mm_store_ps
|
| etc. can you think why this error is occuring? i can call the DLL just
| fine from a C program, so it is definitely functioning.
|

Apr 5 '06 #7
i am not using the float array directly, in the C DLL i am assigning
aligned space using _aligned_malloc for 3 arrays and the float[] input
is being copied to one of these arrays. all the SSE work is then
performed on the 3 aligned arrays, and this is what causes the error
for some reason.
I'm not entirely clear why you want to use this from managed code anyway,
you won't realize any performance gain by marshaling from managed to
unmanaged, you better use unmanaged or managed code only for this.


i was also under the impression that the performance overhead would be
minimal for marshalling the float[]. if i could get the thing to run i
would give you some numbers :)

i am doing this using managed code because i am trying to create a
simple app for recording hydrophone data, and the only demanding work
it does is a bunch of FFTs. i have written an FFT algorithm in managed
code but the performance hit is quite large, a 16k FFT in C# takes
something on the order of 5-8ms whereas the C DLL using SIMD commands
rarely takes more than 1ms. i thought it would be easy to call it from
C# thus speeding up the app considerably (each iteration performs 12
FFTs roughly) and saving me the hassle of writing the rest of the app
in C++.

Apr 5 '06 #8
Marcus,

Yes, it should, but you will loose another couple of cycles as these
intrinsics are a lot slower than the aligned ones.

Willy.

"Marcus Cuda" <ma****@newsgro up.nospam> wrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
| Willy,
|
| > I'm not entirely clear why you want to use this from managed code
anyway,
| > you won't realize any performance gain by marshaling from managed to
| > unmanaged, you better use unmanaged or managed code only for this.
| For my own understanding, you are only referring to marshallings the 16
byte aligned arrays here? For the non-aligned
| array, my understanding (from [1]) is that that marshallings overhead is
minimal when marshallings a float/double array.
| The runtime just pins the pointer to the array and passes it on to the
unmanaged function.
|
| I'm curious if using _mm_loadu_ps and _mm_storeu_ps instead of _mm_load_ps
and _mm_store_ps would solve the problem.
|
| Thanks,
| Marcus
| [1] http://msdn2.microsoft.com/en-us/library/75dwhxf7.aspx
|
|
| Willy Denoyette [MVP] wrote:
| > Note that you can't pass the C# float array to SSE instructions
directly.
| > There is no guarantee that the managed array is 16 byte aligned, so
you'll
| > have to marshal the array to a 16 byte aligned array before passing to
SSE.
| > I'm not entirely clear why you want to use this from managed code
anyway,
| > you won't realize any performance gain by marshaling from managed to
| > unmanaged, you better use unmanaged or managed code only for this.
| >
| > Willy.
| >
| > <mr*********@gm ail.com> wrote in message
| > news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
| > | ok, have removed unsafe, currently calling it as:
| > |
| > | [DllImport("FFT. dll")]
| > | public static extern void FFT([Out]float[] x, int n);
| > |
| > | but with the same problem. i have worked out that the error only
occurs
| > | when the C DLL tries to use the SSE commands such as:
| > |
| > | _mm_load_ps
| > | _mm_store_ps
| > |
| > | etc. can you think why this error is occuring? i can call the DLL just
| > | fine from a C program, so it is definitely functioning.
| > |
| >
| >
Apr 5 '06 #9
Sorry, but you got a AV exception, that means you are reading/writing
from/to a read/write protected piece of memory. You also said that the C DLL
is working correctly when called from C, that would mean that: or, the AV is
due to the managed/unmanaged interop, or due to the piece of code that
passes the float[] elements from unmanaged code to managed code. I would
suggest you run this in the (unmanaged) debugger.
I'm also not clear on what you are doing exactly in your FFT alorithm and
what version of the framework you are using, but, as CLR sits in top of the
exact same C runtime when doing float maths (using SIMD when available), the
performance figures should be comparable.
Willy.
<mr*********@gm ail.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
|i am not using the float array directly, in the C DLL i am assigning
| aligned space using _aligned_malloc for 3 arrays and the float[] input
| is being copied to one of these arrays. all the SSE work is then
| performed on the 3 aligned arrays, and this is what causes the error
| for some reason.
|
| > I'm not entirely clear why you want to use this from managed code
anyway,
| > you won't realize any performance gain by marshaling from managed to
| > unmanaged, you better use unmanaged or managed code only for this.
|
| i was also under the impression that the performance overhead would be
| minimal for marshalling the float[]. if i could get the thing to run i
| would give you some numbers :)
|
| i am doing this using managed code because i am trying to create a
| simple app for recording hydrophone data, and the only demanding work
| it does is a bunch of FFTs. i have written an FFT algorithm in managed
| code but the performance hit is quite large, a 16k FFT in C# takes
| something on the order of 5-8ms whereas the C DLL using SIMD commands
| rarely takes more than 1ms. i thought it would be easy to call it from
| C# thus speeding up the app considerably (each iteration performs 12
| FFTs roughly) and saving me the hassle of writing the rest of the app
| in C++.
|
Apr 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3955
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
15
4665
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is that I get a segmentation error when doing so. I'm using the Dev-c++ compiler. Any ideas? ...
58
10115
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
9
4789
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
8
4107
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
10
3154
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
6
12654
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
11
8115
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
8
3493
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.