Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Writing a C++ COM Server for a Matlab COM client

Subject: Writing a C++ COM Server for a Matlab COM client

From: Chris

Date: 9 Jun, 2009 17:56:02

Message: 1 of 4

Hi everyone,

My question is actually more related to C++ in VisualStudio and COM but since it is also heavily related to Matlab I have no other option than posting here.

I would like to write a custom in-process COM server for MATLAB and use MATLAB as a COM client.

I have succesfully implemented a server dll which generates the following keys under HKEY_CLASSES_ROOT when registered
with !regsvr32 in matlab.

HKEY_CLASSES_ROOT\CLSID\myclsid
HKEY_CLASSES_ROOT\CLSID\myclsid\InProcSvr32
HKEY_CLASSES_ROOT\CLSID\myclsid\ProgID = myprogid
HKEY_CLASSES_ROOT\CLSID\myprogid
HKEY_CLASSES_ROOT\CLSID\myprogid\CLSID = myclsid

The library provides a TestClassFactory (inherits IClassFactory) which creates instances of TestClass (inherits IDispatch).

Calling actxserver( myprogid ) seems to return a handle to an instance of my TestClass implementation (proper functions of my dll are called).

The problem I have now is that calling
handle = actxserver( myprogid );
handle.invoke; % Returns nothing
does not really work.

All I can retrieve is
handle =
   COM.Test_Application_1
which corresponds to the progid of my dll (-> "Test.Application.1")


I'm aware that it is extremly difficult to answer this kind of this question without having a look at my source. But if anyone could provide me with some more insight on how Matlab interacts with a simple custom COM server dll and what is necessary on the COM server side such that the handle.invoke command above actually shows the methods of the server class I could try to identify the problem myself. All I need is a small hint or a good code example to guide me in the right direction.

At the moment I'm lost as it's unclear to me if additional registry keys are necessary or what information/function/properties actually need to be implemented in a COM dll such that matlab can retrieve all the methods and properties of the my COM interface.

The Matlab documentation only explains how to interface specific COM servers such as Excel or how to use Matlab as a COM server.

If anyone has VisualStudio example which actually works this would even be better.

Thanks heaps!

Chris

Subject: Writing a C++ COM Server for a Matlab COM client

From: Yair Altman

Date: 9 Jun, 2009 22:23:01

Message: 2 of 4

[snip]
> handle = actxserver( myprogid );
> handle.invoke; % Returns nothing
> does not really work.
>
> All I can retrieve is
> handle =
> COM.Test_Application_1
> which corresponds to the progid of my dll (-> "Test.Application.1")

You're nearly there:

Try handle.methods or handle.methodsview or my UIInspect utility on the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/17935

Alternately, type "handle." followed by the <tab> key to see all publicly-exposed members of your COM server.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Writing a C++ COM Server for a Matlab COM client

From: Chris

Date: 12 Jun, 2009 09:52:01

Message: 3 of 4

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h0mnc5$ldl$1@fred.mathworks.com>...
> [snip]
> > handle = actxserver( myprogid );
> > handle.invoke; % Returns nothing
> > does not really work.
> >
> > All I can retrieve is
> > handle =
> > COM.Test_Application_1
> > which corresponds to the progid of my dll (-> "Test.Application.1")
>
> You're nearly there:
>
> Try handle.methods or handle.methodsview or my UIInspect utility on the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/17935
>
> Alternately, type "handle." followed by the <tab> key to see all publicly-exposed members of your COM server.
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Hi,

Thanks for the response!

I finally managed to setup my COM communication. Now I have an interface ICOMVideoSink which provides the following methods
/** Create and initialize an image, then add it to the videosink */
STDMETHOD(createImage)( ULONG width,
                                        ULONG height,
                                        ULONG channels,
                                        CHAR* cdepth,
                                        ULONG* /*return value*/ index);
    
/** Destroy an existing image */
STDMETHOD(destroyImage)( ULONG index,
                                          ULONG* /*return value*/ remaining_size );

/** Set the image data of an image of the videosink */
STDMETHOD(setImage)( CHAR* pImageData,
                                    ULONG index,
                                    LONG* pErrorId );

Calling destroyImage() works flawlessly but:

>> image_idx = videosink_handle.createImage( 640, 480, 1, 'IPL_DEPTH_8U' )
??? No method 'createImage' with matching signature found for class 'Interface.MIPServerDll_1.0_Type_Library.ICOMVideoSink'.

fails completely. The problem seems to be that matlab character arrays and the CHAR* data type are incompatible. My guess is that I need to "somehow" convert the matlab character array 'IPL_DEPTH_8U' into a CHAR* data type.

Is there any matlab command which could do that for me (I haven't found anything so far)? The general question is really how do I need to perpare my matlab data within matlab to send matlab arrays to a COM server.

Thanks!

Chris

Subject: Writing a C++ COM Server for a Matlab COM client

From: Yair Altman

Date: 12 Jun, 2009 13:38:02

Message: 4 of 4

> STDMETHOD(createImage)( ULONG width,
> ULONG height,
> ULONG channels,
> CHAR* cdepth,
> ULONG* /*return value*/ index);
>
> >> image_idx = videosink_handle.createImage( 640, 480, 1, 'IPL_DEPTH_8U' )
> ??? No method 'createImage' with matching signature found for class 'Interface.MIPServerDll_1.0_Type_Library.ICOMVideoSink'.
>
> fails completely. The problem seems to be that matlab character arrays and the CHAR* data type are incompatible. My guess is that I need to "somehow" convert the matlab character array 'IPL_DEPTH_8U' into a CHAR* data type.


No - the problem is not in the 4th arg (CHAR *cdepth) but in the missing 5th arg - (ULONG* /*return value*/ index) !

Like I said above - use methodsview or UIINSPECT to know exactly which and how many arguments each exposed function expects.

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us