By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

C# communicate with unmanaged C DLL from C#

P: 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
Share this question for a faster answer!
Share on Google+

Post your reply

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