Re: Calling a matlab DLL from C#?



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:
Hello.
Need some help with the calling of a matlab compiled DLL (C++
under
deploytool). The matlab function takes three strings and return
a
fourth. I know I have to do something like the pasted code
below.
I am
unsure of the correct usage here, since I couldn't find any
examples
using strings. Is the return and input correct, or is there
some
'int
nargin' and REF and OUT keywords?

I appreciate any help and hints.
-Henrik Gundersen

[DllImport("mclmcrrt76.dll")]
private static extern bool mclInitializeApplication(string
options,
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


.



Relevant Pages

  • Re: Calling a matlab DLL from C#?
    ... your P/Invoke statements are probably wrong. ... to each of the outgoing string params. ... private static extern bool mclInitializeApplication(string ... private static extern void mclTerminateApplication; ...
    (comp.soft-sys.matlab)
  • Re: Calling a matlab DLL from C#?
    ... Need some help with the calling of a matlab compiled DLL (C++ under ... private static extern bool mclInitializeApplication(string options, ... private static extern void mclTerminateApplication; ... public static string Run{ ...
    (comp.soft-sys.matlab)
  • Re: C# - Problem to receive data from a C++ Dll
    ... private static extern void GetSNString s); ... > // Call to the dll ... > Perhaps the problem is that I'm using a program destinated to Pocket PC? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Calling a matlab DLL from C#?
    ... don't use with strings being passed to your DLL! ... to each of the outgoing string params. ... private static extern bool mclInitializeApplication(string ... private static extern void mclTerminateApplication; ...
    (comp.soft-sys.matlab)
  • Re: C# - Problem to receive data from a C++ Dll
    ... Declaration of the dll in the C # program: ... private static extern void GetSN; ... However, I can download the dll on the device which is on Pocket PC 2003, I ...
    (microsoft.public.dotnet.languages.csharp)

Loading