christine21

Hello, I've cretaed a Matlab function and managed to call it out in C++. But, when i convert my C++ to C#, there's an 'AccessViolationException Unhandled' error.
Besides, i have successfully import the mclmcr.dll and my function dll in C# by using [DllImport(@"mclmcr70.dll")] method.

Below is my C++ codes:
[mxArray *imageName = NULL;
imageName = mxCreateString("demo1.jpg");

// Call the function
mlfVaanalysis(imageName, outputName);
mxDestroyArray(imageName); imageName=NULL;]

After converted to C#:
[IntPtr imgName= VamDLL.mxCreateString("demo1.jpg");
VamDLL.mlfVaanalysis(imgName,outputName);
VamDLL.mxDestroyArray(imgName);]

where the VamDLL is the C# class i use [DllImport(@"mclmcr70.dll")] to call the following method
[DllImport(@"mclmcrrt70.dll")]
public static extern void mxDestroyArray([In] IntPtr mxArray);
[DllImport(@"mclmcrrt70.dll")]
public static extern IntPtr mxCreateString(string str);
[DllImport(@"vam.dll")]
public static extern bool mlfVaanalysis([In] IntPtr imgName, [In] IntPtr outputName);

the violation error occured at the line IntPtr imgName= VamDLL.mxCreateString("demo1.jpg");

Anybody know what's the correct way to call mxCreateString in C#
How should i call the function
thanks for your help~!



Re: Visual C# Express Edition How to solve AccessViolationException when call matlab function usind C#?

nobugz

mxCreateString takes an old-fashioned 8-bit 'C' string; you are passing it a 16-bit Unicode string. Change your P/Invoke declaration to:

[DllImport(@"mclmcrrt70.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr mxCreateString(string str);






Re: Visual C# Express Edition How to solve AccessViolationException when call matlab function usind C#?

christine21

hello, i've tried the method you mentioned. But, i still having the same error.

I'm wondering is it due to the mxArray*
In the C++, i using
[
mxArray *imageName = NULL;
imageName = mxCreateString("demo1.jpg");
]

Do i have to declare mxArray in C#
Due to there's only IntPtr in C#, is mxArray* equivalent to IntPtr

I've been stucked at this point for 2 weeks. can somebody guide me
Help~!

thanks.





Re: Visual C# Express Edition How to solve AccessViolationException when call matlab function usind C#?

nobugz

Hey Figo, misplaced post





Re: Visual C# Express Edition How to solve AccessViolationException when call matlab function usind C#?

Figo Fei - MSFT

oh, sorry I've deleted :P