Click here to Skip to main content
16,022,752 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am calling a C++ dll in C# code. The called function takes a 2 dimesnsional array but gives the follwoing error when the values are to be printed from the C++ code. The code snippets are attached below.
-------------------------------------------------------------------
"An unhandled exception of type 'System.AccessViolationException' occurred in Utility.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

-------------------------------------------------------------------
The DLL import function is as follows:-
[DllImport("ClusteringDLL.dll", EntryPoint = "?getdata@CClusteringDLL@@QAAXQAPAM0H@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]

public static extern void getdata([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1, SizeConst=4)] float[,] dx, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1, SizeConst=4)] float[,] dy, int it);

-----------------------------------------------------------------

The C++ function is as follows:-
CLUSTERINGDLL_API void __cdecl CClusteringDLL::getdata(float *dxm[], float *dym[], int s) //function for getting the data from user
//CLUSTERINGDLL_API void __ CClusteringDLL::getdata(float (&dxm)[Size][2]) //function for getting the data from user
//CLUSTERINGDLL_API void __thiscall CClusteringDLL::getdata(int dxm) //function for getting the data from user
{
std::cout<<"Enter the number of clusters";
std::cin>>numClust;
std::cout<<std::endl<<"Enter the number of points";
std::cin>>numPoints;
if (numPoints==numClust)
{
std::cout<<std::endl<<"Reenter the number of points as the number of points=number of clusters";
std::cin>>numPoints;
}
for(int i=0;i<=s;i++)
{
std::cout<<std::endl<<"X Co-ordinate:"<<dxm[i][0];

}


}
---------------------------------------------------------------------
The code throws the error when one tried to print the X co-ordinate as shown on the last line of the code.
-------------------------------------------------------------------

And the calling function in main is as fiollows:-
Dllc.getdata(dx, dy, it);

I have tried different calling conventions as well as different ways of writing the signature of the function. But the same error comes up. Any qucik help will be highly appreciated.

Regards
Amit
Posted

1 solution

First ensure that it is working when called from C++.
My doubts are on the line
for(int i=0;i<=s;i++)

I guess it should be
for(int i=0;i<s;i++)

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900