Re: Calling a matlab DLL from C#?
- From: "John Reilly" <jrhokie1@xxxxxxxxxxxxxxxxx>
- Date: Thu, 24 May 2007 07:49:25 -0400
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:
wrote:
On May 21, 10:43 am, Henrik <henrik.gunder...@xxxxxxxxx>
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?options,
I appreciate any help and hints.
-Henrik Gundersen
[DllImport("mclmcrrt76.dll")]
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();
}
}
}
- Follow-Ups:
- Re: Calling a matlab DLL from C#?
- From: Henrik
- Re: Calling a matlab DLL from C#?
- References:
- Calling a matlab DLL from C#?
- From: Henrik
- Re: Calling a matlab DLL from C#?
- From: Henrik
- Calling a matlab DLL from C#?
- Prev by Date: Re: double quadrature with functional inner limits
- Next by Date: Re: how to stop a running function
- Previous by thread: Re: Calling a matlab DLL from C#?
- Next by thread: Re: Calling a matlab DLL from C#?
- Index(es):
Relevant Pages
|
Loading