MATLAB's Combuilder Series -Matlab is a COM component

zhaozj2021-02-16  112

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: MATLAB's Combuilder Series -Matlab Make COM Component Send Station: BBS Shuimu Tsinghua Station (Wed May 21 10:24:09 2003) COM Builder is MATLAB6. 5. There is also the use of MathWorks. It is recommended to use it for mixed programming. I have a full-scale exploration of him. I feel cool. Let's uncover its mysterious veil, ^ _ ^. This series is divided into the following: 1. Matlab Make a COM component 2.vb, C # .NET implementation call 3.VC implementation call 4. Package 5. Excellent difference commentary 2,3, you can choose to see Matlab to do COM Component COM is a referusion of Component Object Module, which is a universal object interface. Any language can be called to call it as long as you follow this interface criteria. MATLAB6.5 new Combuilder is to make the program under MATLAB into COM components for other languages. Let's first prepare two test files, and copy a picture to the C drive, name is called 1.jpg (these you can change, I am in order to easily): The first called im_test.m as follows: function im_test% This file does not input and output i = IMREAD ('c: /1.jpg');%% is not included with the MCC computed with the IMSHOW program, so I want to try COMBUILDER if IMSHOW (i); % Can support these functions second call split2rgb.m, that is, the program sent to me before, because it has problems after being compiled by MCC, so I will continue to test it here, and it also takes There are multiple inputs and output parameters, so I just tested to test COMTOOL in MATLAB Workspace, open MATLAB COM Builder, click File-New Project, create a new project, fill in Comtest Name, Class Name Fill in a SGLTEST (and will automatically generate the CLASSES), COMPLIE CODE IN selection C or C does not matter, draw the checkbox of the Use hand in Complier Options, click OK. Then click Project - Add Files to add IM_TEST.M and SPLIT2RGB.M to the project, then click Build-Com Object, generate a COMTEST_1_0.DLL under the Comtest / Distrib / folder (it is doing a good COM component ), MATLAB has automatically registered this DLL in the registry, and we need to be prepared, open a DOS window, enter the / bin / win32 directory (MATLABROOT) For the path to Matlab installation on your machine, hit the regsvr32 mwcomutil.dll, that is, the MWComutil.dll is registered (this DLL is a DLL of any COM components under MATLAB). The following we are calling when other compilers It can be used.

^ _ ^, Is it easy to do a COM component is simple, Matlab can add members, events, methods, etc., I am actually adding two ways to class SGLTest, how to add members and Methods You can see the help of Matlab's COM Builder. Appendix: Source Code %% // Test File Function [M_NHEight, M_nWidth, Morigr, Morigg, Morigb] = Split2RGB (FileName) %% // Reads a JPG file, MorigData = Imread (filename);% MDestData = ImResize (MorigData, 0.5); Imwrite (MorigData, 'c: /2.jpg'); %% // Save its R, G, B component morigr = morigdata (:,:, 1); Morigg with three variables = MorigData (:,:, 3);,:::, 3); %% // Get the height of the image, width [m_nheight, m_nwidth] = size (mORIGR); Figure (1); SET GCF, 'MenuBar', 'None') IMSHOW (Morigdata); Title ([ORGINAL Image: ', FileName],' Color ',' B ',' FONTSIZE ', 14); XLabel ([' Height: ', Num2Str (m_nheight), 'width:', Num2Str (m_nwidth)], 'color', 'b', 'fontsize', 12); %% // Write param file parafName = [filename (1: length (filename) - 4), '. Param']; FID = fopen (parafName, 'W'); fwrite (FID, M_NHEIGHT, 'UINT32'); FWRITE (FID, M_NWIDTH, 'UINT32'); Fclose (FID); %% / / Write R component file RFNAME = [filename (1: length (filename) -4), '_ r.ROT']; FID = fopen (RFNAME, 'W'); FWRITE (FID, MORIGR, 'UINT8'); fclose (FID); %% // Write G component file GFNAME = [filename (1: length (filename) -4), '_ g.ROT']; FID = FOPEN (GFNAME, 'W'); FWRITE (FID, MORIGG) , 'uint8'); fclose (FID); %% // Write B component file BFNAME = [ Filename (1: Length (filename) -4), '_ b.rot']; FID = fopen (bfname, 'w'); fwrite (FID, MORIGB, 'UINT8'); Fclose (FID); - Come ON , Baby! ※ Source: · BBS Shuimu Tsinghua Station http://smth.orgs [from: 202.113.39.83]

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: MATLAB's Combuilder Series-VB, C # .NET Call Station: BBS Shuimu Tsinghua Station (Wed May 21 10:24:38 2003) This section tells VB How to implement C # .NET How to call the previous part we generated COMTEST_1_0.DLL (COM components made under MATLAB), remember to register MWComutil.dll (how to register to see a part) 1. VB implement call or Create a new VB engineering, point Project-Reference, find Comtenste 1.0 Type Library in the pop-up window, select the front check box, click OK, then add this COM component to the project, at this time You can use the object browser to see a SGLTEST class under Comtest. There are two ways IM_TEST, SPLIT2RGB in this class, and a MWFLAGS member (this member is automatically generated), and the test code under VB is as follows: Test the im_test method Code: DIM St AS SGLTEST SETS ST = New SGLTEST CALL St.IM_TEST Test Split2RGB Method: Dim St AS SGLTest Set St = New SGLTest Dim H AS Variant, W AS Variant, R AS Variant, G AS Variant, B As Variant FILENAME AS VARIANT FileName = "C: //1.jpg" Call St.Split2RGB (5, H, W, R, G, B, FileName) Various input / output parameters under MATLAB is all Variant type in COM components. The variable, the test is successful, the result is the same as the touch of Matlab, cool 2.C # .NET implementation call open or create a C # project (I use the vs.net editor), select the resolution on the right The reference to the program resource manager, the mouse is right, select COM in the pop-up window, then find Comtensters_1_0.dll, click Select, then OK, this COM component is also added to the project On the same, we can choose the object browser to see the COMTEST and the SGLTEST class below, the C # test item is as follows: Test Try the code of the im_test method: Comtest.sgltestClass st = new comtest.sgltestClass (); st.im_test (); test split2RGB method code: Comtenste.sgltestClass St = new Comtest.sgltestClass (); Object H = null, w = null , r = null, g = null, b = null; object filename = "c: //1.jpg"; st.Split2RGB (5, Ref h, refl, ref r, refg, ref b, filename); It can be seen that the input parameter is the REF Object type, and the output parameter is Object type, and the test is also successful, and the result of the running under Matlab is cool.

- Come on, baby! ※ Modification: · xxhn at May 21 10:27:53 Modified this article · [From: 202.113.39.83] ※ Source: · BBS Shuimu Tsinghua Station http://smth.org · [From: 202.113 .39.83]

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: MATLAB's Combuilder Series -VC Upset Send Station: BBS Shuimu Tsinghua Station (WED May 21 10:25:17 2003) This part is in the VC The first part we generated COMTEST_1_0.DLL, also remember to register MWComutil.dll (how to see the first part), VB and .NET implementation to call the COM component is very simple, and the VC implements this step I can explore it. For a few days (mainly VC is not good) 1. Before doing some preparations, start with Ole Viewer tool - MICROSOFT Visual Studio6.0 - Microsoft Visual Studio6.0 Tools - Ole Viewer (this The tool can also open in the VC's Tools - Ole / Com Object View, in the Ole Viewer Tool, select Type Libraries on the right, start him, find Comtenste 1.0 Type Library, select it, right click, select View , I will pop up a window, and the Save button on the toolbar saves him as Comtest_1_0.h, Comtest_1_0.c (can also be stored as a COMTEST_1_0.IDL interface file), we can implement COMTEST_1_0 through these two files .dll call 2.VC Upset new or open a VC project, note that there is no setup for the compiler (and the DLL generated by MCC We set a lot, I only set precomplied headers, choose automatic Use of precomplied headers, write stdafx.h), join comtest_1_0.h and comtest_1_0.c, and copy a comtest_1_0.dll to the project directory, because Comtenster_1_0.dll is used to use MWComutil.dll, so will / extern / incrude / under MWcomutil.h and MWCOMTYPES.H also join the project (copy the two files into the engineering path, if library path is set, can not be added) This time you can see it through ClassView. _Iid, imwutil, isgltest class, isgltest is under the SGLTEST class VC we built under Matlab The code is as follows // These are introduced into the DLL and header file #import "mwcomutil.dll" #Import "COMTEST_1_0.DLL" #include "mwcomutil.h" #include "comtest_1_0.h" #include "comutil.h" // This file is used to handle the code of the transformation test IM_TEST method to the Variant type: if (Failed (NULL)) // Initialization Call COM {AFXMessageBox ("Unable to Initialize COM");} isgltest * ST = NULL; // create a com component, CLSID_sgltest and IID_Isgltest can find HRESULT hr = CoCreateInstance (CLSID_sgltest, NULL, CLSCTX_ALL, IID_Isgltest, (void **) & st) from comtest_1_0.h and comtest_1_0.c years; if (SUCCEEDED (hr )) {St-> im_test ();

AFXMessageBox ("succeed"); st-> release ();} else {afxMessageBox ("unsucceed");} If your VC project is console project, the above AFXMessageBox can change to PrintF or Cout, test the code of the split2RGB method. (Type conversion I refer to Visual C Essence area also conversion successfully) {AFXMessageBox ("Unable to Initialize COM");} isgltest * st = null; hResult hr = cocreateInstance (CLSID_SGLTEST, NULL, CLSCTX_ALL, IID_ID_IDTEST, (VOID **) & ST); Variant M, N, R, G, B, FileName; Variantinit; Variantinit; Variantinit (& R); Variantinit; Variantinit (& B) ); Variantinit; filename.vt = vt_bs; filename.bstrval = _com_util :: ConvertStringTOBSTR ("c: //1.jpg"); if (succeeded (hr)) {st-> split2RGB (5, & M, & n, & r, & g, & b, filename); ST-> Release (); AFXMESSAGEBOX ("succeed");} else {AFXMessageBox ("unsucceed));} Like, running results are the same as the results under MATLAB, remember Inside our imshow, IMSHOW, I used to use it in the MCC generated program, it is wrong, cool. For changes in COM components and their types under VC, please refer to MSDN and its Visual C Essence - Come on, Baby! ※ Modify: • XxHn on May 21 10:36:09 Modified this article · [From: 202.113.39.83 ] ※ Modification: • XXHN at May 21 20:23:21 Modified this article · [From: 202.113.39.83] ※ Modified: • xxhn on May 23 19:12:04 Modified this article · [From: 202.13.39.16] ※ Source: · BBS Shuimu Tsinghua Station http://smth.org · [From: 202.113.39.83]

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: MATLAB's Combuilder Series - Package and Its Advantages and Disadvantages Note Send Station: BBS Shuimu Tsinghua Station (WED May 21 10:25:42 2003) ^ _ ^, The Combuilder series can end one. Package: In the Workspace under matlab, click File-Open Project to open our previous Comtest.cbl project file, then click Component - Package Component to package, at this time Looking into the COMTEST / DISTRIB folder, the generated chast.exe is the packaged decompression program, double-click it to extract some files, then click the unfained _Stall.bat to implement the installation (according to his steps ) II. Excellent and shortage commentary these days in this day, I will die, especially when the VC is successful, I remember that Combuilder has no substantive breakthrough in the essence, I don't think so, it The breakthrough can be big. It is COM. General, any compiler can be called, want C Builder, Delphi, etc., I want to do it according to calling COM components, can also succeed 2. Support IMSHOW and other functions, support for graphics libraries is also strong than before (these also need to test together) 3. Implement the method, no big pile like previously mixed Dongdong 4. You can write your own class under Matlab and write members, methods and events for your class. The management project is also convenient (this is a bit like VC, VB management project) is too cool, and suddenly I don't know how to write a shortcomings, ^ 0 ^, I think that it is necessary to find out if I have said something here, I feel that it is all Variant type, not very convenient - Come on, Baby! ※ Modify : · XXHN at May 21 10:29:04 Modified this article · [from: 202.113.39.83] ※ Source: · BBS Shuimu Tsinghua Station http://smth.org · [From: 202.113.39.83]

Sender: Capriccio (Jun), Cyre: Mathtools Title: Arch of Var and Variant Variables in COM in the Var Reinterpretation Station: BBS Shuimu Tsinghua Station (Thu May 29 23:22:28 2003), transfer An example of an example, first describing the function of the COM component in an example with MATLAB's M file: function y = hehe (x) y = x * 2; save this file as Hehe.m to establish an engineering Comparatest with Combuilder, join the class PARATEST, then join the file hehe.m, then Build Become COM components. Below is the code in the VC: #include "stdafx.h" #include "stdio.h" #import "mwcomutil.dll" #Import "COMTEST2_1_0.DLL" #include "mwcomutil.h" #include "chast2_1_0.h" # INCLUDE "Comutil.h" int Main (int Argc, char * argv []) {double a [4] = {1, 2, 3, 4}; int i = 4; if (Failed (Coinitialize (NULL)))) // Initialization Call COM {Printf ("Unable to Initialize COM"); The element in the array is Double SafeaRaybound Rgsabound [1]; Rgsabound [0] .CELEMENTS = 4; // Element RgSabound [0] .llbound = 0; // Matrix index lower bound X.PARRAY = SafeArrayCreate Vt_r8, 1, rgsabound; // This function is created // a SafeArray, and passed the pointer of this SafeArray to X.Parray.

x.parray-> pvData = a; Iparatest * st = NULL; // create a com component, CLSID_paratest and IID_Iparatest can find HRESULT hr = CoCreateInstance (CLSID_paratest, NULL, CLSCTX_ALL, IID_Iparatest from //comparatest2_1_0.h and comparatest2_1_0.c in (void **) & st); if (succeeded (HR)) {st-> hehe (1, & y, x); Memcpy (a, y.parray-> pvdata, 4 * sizeof (double)); // When the matrix Y returned in the MATLAB file, the COM component // stores the value of the obtained y. The memory area pointed to Y.PARRAY-> PVDATA. Printf ("% f / N% f / N% f / N% f / n", a [0], a [1], a [2], a [3]); Printf ("succeed"); ST -> Release ();} else {printf ("unsucceed");} Return 0;} The above method solves the passage between the actor matrix parameters in COM components and VCs, should also be used to use other types of matrices, such as Characters and integer matrices. However, if the COM component is obtained by compiling M files by the Matlab Combuilder, and the matrix returned by the M file is a plurality of plurality, it will not. Because it seems that the matrix returned at this time is not saved in Y. Parray as described above. As for where to save where it is, it has been found out, anyway, I have no one, I don't have a little clue. - ※ Source: · BBS Shuimu Tsinghua Station SMTH.org · [From: 166.111.33.68]

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: Re: Matlab and VC mixed programming problems (calls with IDL or DLL) Sending station: BBS Shuimu Tsinghua Station (Tue Aug 19 15:08 : 50 2003), transfer here again to provide a method of calling COM components by IDL or DLL (excluding * .h and * .c files) In fact, this method is called by generating .h and .c files. Simple :), also use the Essence area 1. Generate an IDL file through the OLE / COM Object, add this IDL file to the project, point menu build-complie comtest_1_0.tlb, generate TLB files 2. Open ClassWizard, click Add right Class-from a type library class, select the original COMTEST_1_0.TLB (should be in the debug / directory), in fact, it can also directly select the Comtensters_1_0.dll generated with Matlab's Combuilder (this saves the first step). There will be some dialogs that generate CLASS, which can be done, this will generate Comtensters_1_0.h and comtest_1_0.cpp, more than an isgltest class:) 3. The test code is as follows: Simply add: Headfinder Add: #include "comtest_1_0.h" test code: isgltest st; afxoleinit (); if (st.created))) {st.im_test (); afxMESSAGEBOX ("Haha, succeeded"); ST .ReleaseDispatch (); else afxMessageBox ("unsucceeded"); the specific meaning of the above code See MSDN, I just fully explored :) [In the masterpiece of Promise0608 (Promise0608) mentioned:]: When I am using COM components, Use Microsoft Visual Studio tool Ole Viewer to open the generated component. Looking: To the DLL file generated by Matlab COM Builder, an error occurred as * .h and * .c files! : I use vc.net, I don't know why! : Please help me! : Thank you! - Come ON, Baby! ※ Modification: · xxhn in AUG 19 15:12:28 Modified this article · [From: 218.76.251.27] ※ Source: · BBS Shuimu Tsinghua Station Smth.org · [From: 218.76.251.27]

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: Re: VARIANT variable in VC and the mutual conversion station of Variant variables in COM: BBS Shuimu Tsinghua Station (Fri May 30 12:58:02 2003) , Transfer me to add, VB, .NET's array and COM's mutual conversion VB and .NET variables of VB and .NET variables are also very simple. The example is simply: add one in front of our front sgltest The function called y = paatest (x), x, y is the input output, all of which is matrix 1.vb: Dim St AS SGLTESTSET ST = New SGLTestDim x (2) As Variant, Y As Variantx (0) = 3 # x ( 1) = 5 # x (2) = 6 # Call St.ParateSt (1, Y, X) Y (1, Y, X) Y (1, 1), Y (1, 2) is the array 2 of the output. Net under Net: Comtest.sgltestClass St = New Comtest.sgltestClass (); Object Y = NULL; Object [] x; x = new object [3]; x [0] = 2.0; x [1] = 3.0; x [2 ] = 4.0; pt.para_test (1, ref y, x); y [0, 0], y [0, 1], y [0, 2] is the array of output [in Capriccio "masterpiece To:]: Use an example to explain,: First use MATLAB M file to describe the functionality of COM components :: function y = hehe (x): y = x * 2;: Save the file as Hehe.m : Use Combuilder to establish an engineering comparatest, join the class paartest, then join the file hehe.m, and then build is a COM component. : Below is the code in the VC: #include "stdafx.h": #include "stdio.h": #import "mwcomutil.dll": ................. .. - come on, baby! The mutual conversion station of Variant variables in COM: BBS Shuimu Tsinghua Station (Fri May 30 14:49:09 2003), transferring the parameters of the transfer is the problem of multiple matrices, depressed, suddenly thinking after a long time, no It is necessary to figure out this problem (although MATLAB does provide the transfer method of multiple parameters) only need to pass the plurality of real and false parts as two real numbers.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>

Sender: xxhn (Little Hunan), the letter area: Mathtools Title: Re: About Com Builder! Sending station: BBS Shuimu Tsinghua Station (Fri May 30 19:35:07 2003), transfer [in SiCiliner (Fisher) masterpiece:】 标: Re: About Com Builder! : Sending station: BBS Shuimu Tsinghua Station (Fri May 30 15:28:10 2003), transfer:: No one help? : [In the masterpiece of SiCiliner (Fisher):]:: Tests for 6.5 believed in COM Builder, found that if there is no Simulink model, that is, M files:: inside the SIM command, use Combuilder Create a COM component does not have problems, but if you contain the sim command:: If you ask you, have any good solution, or Combuilder does not support Simulink ~~~~~~~ I think this is this the reason, as is off from help matlabcombuilder in to the Limitations and RestrictionsIn general, limitations and restrictions on the use of MATLAB COM Builder are the same as those for the MATLAB Compiler. See the Limitations and Restrictions section of the MATLAB Compiler User's Guide for Details. Note That Alth The Compiler Supports Some Usage of the Compiler Suppirl COMMAND, MATLAB COMMARDER DOES NOT Support this Command At All. The MATLAB Compiler does not support Simulink, so Combuilder does not support:: Thank you! :: -::: ※ Source: · BBS Shuimu Tsinghua Station Smth.org · [From: 166.111.73.158] - Come on, Baby! ※ Source: · BBS Shuimu Tsinghua Station Smth.org · [From: 202.113. 40.70] Nine. Output of complex types in Combuilder and VC mixed (by LPCTSTR)

Today, I am drumping, and I solved this problem on the basis of my predecessors;)

The method of calling COM is slightly different from the previous one, and personal feelings is simpler, regular, and is a point;)

Step 1 build M file, build into com

M file is very simple

Function CPLX = getCPLX ()

CPLX = 1 2i

Convenient to see the code, write the name of I have established COM project: Amycom Category: MyTST

Step 2 Export the necessary header files

1. Open Ole Viewer (VC6.0 menu: Tools -> OLE / COM OBJECT VIEWER

2.ole Viewer menu: file -> View Typelib

3. Open the DLL generated by Combuilder: Amycom_1_0.dll

4.File -> Save as selectively generated .h file

5. Copy the generated header file to the VC engineering path

6. Repeat the above steps to generate the header file of mwcomutil.dll

Simple test source code under Step 3 VC is as follows:

#include

#include "stdio.h" #include "mwcomutil.h"

#include "amycom_1_0.h"

void main ()

{

Coinitialize (NULL); // Initialization COM

// Create a COM instance

CLSID CLSID_MYTST;

CLSIDFROMPROGID (L "amycom.mytst.1_0", & clsid_mytst);

// The progID here can be seen in the Combiulder's Component-> Component Info.

Deng Kefeng dkf03@mails.tsinghua.edu.cn

/ / Or search registration

IID IID_IMYTST = __UUIDOF (iMyTST);

IMytSt * PMYTST = NULL;

CoCreateInstance (CLSID_MYTST, NULL, CLSCTX_ALL, IID_IMYTST, (Void **) & PMYTST

);

Variant R [5];

For (int i = 0; i <5; i )

{

Variantinit (& R [I]);

}

PMYTST-> getCPLX (1, ​​& r [0]); // Call function interface

IMWcomplex * ptemp = (iMWcomplex *) R [0] .pdispval; // complex type interface

Ptemp-> get_real (& R [1]);

PTEMP-> GET_IMAG (& R [2]);

Double DR = r [1] .dblval; // Get real part

Double img = r [2] .dblval; // Get the virtual part

Printf ("REAL:% F ​​/ NIMG:% F / N", DR, IMG); //

GetChar ();

Couninitialize ();

Return;

}

Personally feel that the complicated data type of Matlab has made COM group packages in MWcomutil.dll, when

You can see a lot of interfaces when you open this file with Ole Viewer, including the IWMCOMPLEX diameter interface, CO

The passage of the M parameter is implemented through the Variant type, and the detailed usage can be checked in MSDN.

转载请注明原文地址:https://www.9cbs.com/read-10283.html

New Post(0)