473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# communicate with unmanaged C DLL from C#

17
Hi everyone,

I'd like to implement some external (in C-written) DLL-functions to my C# software.
I'm having some problems with the declaration though, since I'd like to pass an array (which will be numerically changed from within the dll-function).
The IDE keeps telling me I'm about to change some memory. The exact message is:

"AccessViolationException was unhandled
Attempted to read or write protected memory. This is often an indication that other memory is corrupt."


Below is a piece of code as far as I've got it now:

Expand|Select|Wrap|Line Numbers
  1. At the c# side:
  2.  
  3.     class DSPToolBox
  4.     {
  5.  
  6.         // Externe DSP dll
  7.  
  8.         unsafe public static extern double S_MakeMOVRMS(short* arrIn, double* arrR, long Size,
  9.             short Order, double* arrA, double* arrB, double CaliOffset);
  10.  
  11. }
  12.  
  13. class someother class
  14. {
  15. long ret;
  16.  
  17. double[] A = ACoeff.A;
  18. double[] B = ACoeff.B;
  19. double[] R = ACoeff.Register;
  20. short* Temp = StartPtr;
  21.  
  22. fixed (double* pACoeffA = A) 
  23. fixed (double* pACoeffB = B)
  24. fixed (double* pACoeffR = R)
  25.  
  26. ret = DSPToolBox.S_MakeMOVRMS(Temp, pACoeffR,
  27.                     (long)DiskBuffer.Length, Order, pACoeffA, pACoeffB, 0);
  28.  
  29. }
  30. //--------------------------------------------------------------------------------------------------------
  31. At the dll (c) side:
  32.  
  33. double _stdcall __declspec(dllexport) S_MakeMOVRMS
  34. ( short* arrIn, double* arrR, long Size, short Order, double* arrA, double* arrB,
  35.   double CaliOffset)
  36. {
  37.  arrR[0] = 5*2;
  38.  
  39. blabla....
  40.  
  41.  return 0;
  42. }
  43.  
  44.  
Calling this dll was not a problem at all in VB6, I simply used byref to pass the arrays (with element 0), and it worked, so the dll is ok. Passing the pointers doesn't seem that simple in c# as in VB though (how ironic that may be).
Does anybody maby know how one could handle this?

Thanks.
-Michiel
Apr 23 '07 #1
0 791

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bill | last post by:
Hello, I'm successfully on my way to finishing up a C# Windows Service now, but now I'm wondering how I can communicate with this service. Services examples seem to be everywhere on the internet...
2
by: C# | last post by:
How can I communicate with the parallel port in C#?
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
6
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
1
by: Nuno | last post by:
Hi I have some legacy code written in C++ and I am trying to add a GUI (plus some other functionalities) using C#. Thus the problem that I am facing is to have C# and C++ live together. I am...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
5
by: akash | last post by:
I'm having problems calling an unmanaged class from a managed wrapper. I suspect I'm missing something obvious, as I'm unfamiliar with C++ and classes are very simple. My unmanaged class is as...
1
by: DaTurk | last post by:
Sorry, some how I clicked post before I finished. Hi, I'm writing an application that has a native c++ layer, and a CLi/C++ layer above it. Above the CLi layer will be C# but that's not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.