|
-
July 29th, 2009, 09:05 PM
#1
Passing multi-dimensional array from C# to Win32 C++ dll?
hello
I'm trying to pass multi-dimensional arrays from C# to un-managed WIN32 C++ dll - second parameter of each of the two functions exported below are meant to be OUTPUT parameter (memory allocated in C# however)
Code:
MATLABGENERICDLL_API void GetCurves(char* strParam, double * myCurve)
{
for(int i=0; i<10; i++)
{
if(i>0) {
myCurve[i] = 1.01 * Curve[i-1];
} else {
myCurve[i] = 0.5;
}
}
return;
}
MATLABGENERICDLL_API void GetMatrix(char* strParam, double ** myMatrix)
{
try {
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
myMatrix[i][j] = 0.25;
}
}
} catch( ...)
{
throw;
}
return;
}
GetCurves executed successfully, but not GetMatrix.
Code:
class Program
{
[DllImport(@"C:\MatlabGenericDll.dll")]
private static extern string ToUpper2(string Src);
[DllImport(@"C:\MatlabGenericDll.dll")]
private static extern void GetCurves(string strParam, [In, Out] double[] myCurves);
[DllImport(@"C:\MatlabGenericDll.dll")]
private static extern void GetMatrix(string strParam, [In, Out] double[,] myMatrix);
static void Main(string[] args)
{
string strTest = "abc";
string strResult = null;
double[] myCurve = null;
double[,] myMatrix = null;
try
{
strResult = ToUpper2(strTest);
myCurve= new double[100];
GetYieldCurves("AAA", myCurve); << For some reason, executed succssfully (Although I checked &myCurve - address seems to be different in C#/C++ layer so I really don't understand why it worked)
myMatrix= new double[10, 10];
GetMatrix("BBB", myMatrix); << System.AccessViolationException, complained saying memory probably corrupted.
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return;
}
}
I'm not sure if I am doing right thing ... allocating memory from C# layer and if C# exe and C++ dll share same address space at all.
My end goal is: I actually also need to pass, from c# exe to unmanaged C++ dll, a two-dimensional array of undetermined size of different column types (string, doubles, int, DateTime)
Thanks!
http://msdn.microsoft.com/en-us/libr...53(VS.71).aspx
Last edited by THY02K; July 30th, 2009 at 01:01 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|