how can MATLAB handle .NET methods that have a REF parameter?

I have a .NET dll which provides the following capability:
TMCTL cTmctl = new TMCTL();
int ret = 0;
ret = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id );
I need to use this dll file in Matlab, and so far I can succesfully reference the dll and create the necessary object:
YK_TMCTL = NET.addAssembly('C:\Windows\SysWOW64\TmctlAPINet64.dll');
Oscilloscope = TmctlAPINet.TMCTL;
However, I have difficulties in callig the Initialize function, this code always retunrs 1, wihch means the function could not establish a connection:
int32DeviceID = int32(0);
int32InstrumentAnswer = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,'10.60.142.50,anonymous',int32DeviceID);
Oscilloscope.TM_CTL_ETHER is just a constant, it's not giving me any trouble. But I believe the int32DeviceID is the problem and I do not know how to succesfully pass this 'reference' variable.
Note that I have looked at this answer and I have subsequently tried:
[int32InstrumentAnswer,int32DeviceID] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,'10.60.142.50,anonymous')
Unfortunatelly this fails with the following error: No method 'Initialize' with matching signature found for class 'TmctlAPINet.TMCTL'.
Hence, the question in the title.
Regards,
Cristian

1 Comment

what does
methodsview(Oscilloscope)
%or
methods(Oscilloscope, '-full')
says the full signature of Initialize is (there may be several, in which case, show them all).
Oh, and what is the actual documented .Net signature?

Sign in to comment.

 Accepted Answer

If we go by your .Net code, the correct matlab version should be:
id = int32(0); %you probably don't even need the int32. matlab should do the conversion automatically
[ret, id] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER, '11.22.33.44,anonymous,', id);
ref parameters are both inputs and outputs in matlab. See Call .Net methods With ref Keyword
If that doesn't work then answer my comment to your question.

3 Comments

Cristian's comment mistakenly posted as an answer moved here:
Hi, thank you for your help!
I was not aware of the Call .NET methods with ref Keyword link that you sent me.
Following your suggestion, I tried the following:
[int32InstrumentAnswer,int32DeviceID] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,"10.60.142.50,anonymous",int32DeviceID)
It works, in the sense that it does not throw an error, but it does not work, in the sense that the returned values are [1, -1], which basically means a connection error.
Running methods(Oscilloscope, '-full') yields (for this method):
[int32 scalar RetVal, int32 scalar id] Initialize(TmctlAPINet.TMCTL this, int32 scalar wire, System.String comm, int32 scalar id)
I am a bit puzzled about the "TmctlAPINet.TMCTL this" argument, as it would mean I need to pass 4 arguments to this Initialize function right? But I do not really know what it is.
Anyway, one thing I can try is intall Sharpdevelop and see if the C# equivalent of the code is really working, so as not to chase the presumably innocent Matlab.
Regards,
Cristian
Cristian's comment mistakenly posted as an answer moved here:
I found the problem, eventually. The correct formulation of the instrumction is as you suggested:
[int32InstrumentAnswer,int32DeviceID] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,"10.60.142.50,anonymous,",int32DeviceID)
However, the key is the "," character at the end of the second argument, which I did not have before. Adding this small chang makes it work, I noticed this when actually trying to make it work in C# (I never got around to finishing that, as when I noticed this difference I went back to Matlab and it worked!)
The string argument consists of an IP address, username and pasword, and they must be separated by commas. According to the documentation, , if the username is "anonymous" then there is no further need for a password, however, it seems the comma still needs to be there.
Best regards,
Cristian
In C# and a lot of other languages, the compiler automatically adds an extra input to class member functions, usually as the first input. This argument is always the class instance on which the function is called. So, for example, the method:
//.Net code
//class definition
public class TMCTL{
public int Initialize(int wire, string comm, ref int id){
//... implementation
}
}
is automatically converted, at compile-time to
public int Initialise(TMCTL this, int wire, string comm, ref int id)
because the function actually needs to know which object it operates on. This extra argument is all invisible to you, and the compiler also adds it in function calls:
ret = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id );
is compiled as
ret = Initialize(cTmctl, TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id);
In matlab however, the developpers decided to leave that job to you. So whenever you implement a class method in matlab, you must make sure that the object is an explicit input argument.
%matlab code
classdef TMCTL
methods (Access = public)
function [result, id] = Initialise(this, wire, comm, id)
%...
end
end
end
Matlab still does the job of transforming function calls, but you can also do it yourself
%both of these work in matlab and are more or less equivalent (there's a slight difference of scope that is not relevant)
[ret, id] = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", id);
[ret, id] = Initialize(cTmctl, TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", id);
if the username is "anonymous" then there is no further need for a password, however, it seems the comma still needs to be there.
Yes, that makes sense. The code probably split the string at commas and expect 3 strings after the split.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!