Re: Calling a matlab DLL from C#?
- From: "Taher Hassan" <taherfabbas@xxxxxxxxx>
- Date: Wed, 7 Jan 2009 15:15:04 +0000 (UTC)
Hi:
It would be nice if you post a sample code.
Regards
T
Henrik <henrik.gundersen@xxxxxxxxx> wrote in message <1180425043.212140.76660@xxxxxxxxxxxxxxxxxxxxxxxxxxx>...
On May 24, 1:49 pm, "John Reilly" <jrhokie1@xxxxxxxxxxxxxxxxx> wrote:
Hi Henrik,
did you ever get this to work?
i have extensive C# experience and have done much with P/Invoke.
without trying an example, your code doesn't seem right.
1. your Run() function never calls mclInitializeApplication, which
you *must* call before you initialize your DLL.
2. don't use [in] with strings being passed to your DLL! you've got
the sense wrong. you would use [out].
3. your P/Invoke statements are probably wrong. look at the
generated C/C++ header file for the function prototype. If you send
it to me, I'm sure I can give you the right P/Invoke. taking a WAG:
on the strings you pass to your function, I'll bet you should be
passing pointers to ANSI strings. So you need to add
MarshalAs(UnmanagedType.LPStr)
to each of the outgoing string params. for your RETURN string value,
you need to allocate the buffer yourself, and pass the pointer to the
DLL. this means you need to know how big it is going to be (256):
IntPtr pbuf = Marshal.AllocHGlobal(256);
// call your DLL, passing pbuf
// convert pbuf to string:
string sOut = Marshal.PtrToStringAnsi(pbuf);
Marshal.FreeHGlobal(pbuf);
that should do it.
good luck. if you want to email me your .h file, i'll give you a
more definitive answer.
john.
Henrik wrote:
On May 21, 10:43 am, Henrik <henrik.gunder...@xxxxxxxxx>wrote:
aHello.under
Need some help with the calling of a matlab compiled DLL (C++
deploytool). The matlab function takes three strings and return
below.fourth. I know I have to do something like the pasted code
I amsome
unsure of the correct usage here, since I couldn't find anyexamples
using strings. Is the return and input correct, or is there
'int
nargin' and REF and OUT keywords?
I appreciate any help and hints.
-Henrik Gundersen
[DllImport("mclmcrrt76.dll")]options,
private static extern bool mclInitializeApplication(string
Int32 count);
[DllImport("mclmcrrt76.dll")]
private static extern void mclTerminateApplication();
[DllImport("testing.dll", EntryPoint = "#1")]
static extern string mlfTesting([In]string s1, [In]string s2,
[In]string s3);
[DllImport("testing.dll")]
private static extern void testingInitialize();
[DllImport("testing.dll")]
private static extern void testingTerminate();
Some more information. I tried doing the below, which results in a
System.AccessViolationException .
namespace Matlab32
{
class MatlabConnection{
[DllImport("mclmcrrt76.dll")]
private static extern bool mclInitializeApplication(string
options, Int32 count);
[DllImport("mclmcrrt76.dll")]
private static extern void mclTerminateApplication();
/*wrappers*/
public static void Initialize(){
mclInitializeApplication("NULL",0);
}
public static void Terminate(){
mclTerminateApplication();
}
}
class Mini
{
[DllImport("matlab/mini/mini.dll")]
private static extern void miniInitialize();
[DllImport("matlab/mini/mini.dll")]
private static extern void miniTerminate();
[DllImport("matlab/mini/mini.dll", EntryPoint = "#1")]
private static extern void mlfMini([In]Int32 nargout,
StringBuilder buf, [In]string s1, [In]string s2, [In]string s3);
public static string Run(string s1, string s2, string s3){
miniInitialize();
StringBuilder miniBuilder = new StringBuilder(256);
mlfMini(1,miniBuilder,s1,s2,s3);
miniTerminate();
return miniBuilder.ToString();
}
}
}
I made it work quite nicely. I sat down and read more P/Invoke and C#
documentation (only know Java, C, and C++ to some degree) which helped
me get it right. If I should leave any tips for future users of P/
Invoke and MATLAB it would be regarding your point 1. You can only
call mclInitializeApplication and mclTerminateApplication once. I
originally had this inside my Run(), but it failed horribly. Also the
miniInitialize and miniTerminate were giving me troubles when used
more than once, so I moved them out to mclInit (some place that runs
once per application and at application exit). Not the best solution,
but it works. I would love to have it verified or shown to in
documentation that any Init-function in a DLL can only be run once per
application. Now, I made it work based on trial and error. Not
optimal.
If there is any interest, I will paste some working code for this.
-Henrik
.
- Prev by Date: Re: need help
- Next by Date: Re: ball following robot
- Previous by thread: Re: how to use initmesh ?
- Next by thread: Matlab and fftw-3.0.1 give different 2D fft result
- Index(es):
Relevant Pages
|
Loading