Deng Kefeng DKF03@mails.tsinghua.edu.cn Matlab's Combuilder Series -Matlab Make COM Components
Com Builder is MATLAB6.5, and it is also recommended for MathWorks to mix programming. I have a full-time exploration of him. I feel cool. Let's uncover its mystery, ^ _ ^. This series is divided into the following: 1. Matlab Make COM component 2.vb, C # .NET implementation call 3.VC implementation call 4. Package 5. Excellent difference commentary 2. If you have any questions, please choose one problem Send mailbox: dkf03@mails.tsinghua.edu.cn, you can also add SMTH, ID: XXHN later supplement: 6. Variant variable in the Var in the VARIANT variable (by Capriccio) 7.vb, .NET The mutual conversion of Variant variables in the COM 8. MATLAB and VC mix programming (CM components with IDL or DLL) 9.combuilder and VC mixed output (by lpctstr)
I. Matlab Making COM Components COM is the abbreviation of Component Object Module, it is a common object interface, and any language can be invoked by using this interface criteria. MATLAB6.5 new Combuilder is to make the program under MATLAB into COM components for other languages. We first prepare two test files, and copy a picture to the C drive, name it is 1.jpg (these you can change, I am in order to easily) The first called im_test.m as follows: function im_test% The file does not have input and output I = IMREAD ('c: /1.jpg');% is not used by MCC with an IMSHOW program, so I want to try COMBUILDER IMSHOW (i);% can Support these functions second called split2rgb.m, which is the program sent to me before ZOSCO, because it has a problem after computing into a DLL with MCC, so I will continue to test it here, and it also has more An input 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 Comtenste in Component Name, and fill in Class Name A SGLTEST (and will automatically generate CLASSES in COMTEST Remove), and COMPLIE CODE IN selection C or C does not matter, draw on the checkbox of Use Handle Graphics Library 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
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'); Deng Corfeng DKF03@mails.tsinghua.edu.cn fclose (FID); %% // Write G component file GFNAME = [filename (1: length (filename) -4), '_ g.rot']; FID = FOPEN GFNAME, 'W'); FWRITE (FID, M Origg, '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);
II.VB, C # .NET implementation calls this part of the VB, C # .NET how to implement call the previous part we generated COMTEST_1_0.DLL (COM components made under MATLAB), remember to be registered with MWcomutil.dll first (how to Registration See section) 1. VB Realization Call open or create a VB engineering, point Project-reference, find Comtenste 1.0 Type Library in the pop-up window, select the front check box, click OK, at this time Add this COM component to the project, then you can use the object browser to see a SGLTest class under Comtest, this class has two methods im_test, split2RGB, and a MWFLAGS member (this member is automatically generated) The test code under VB is as follows: Test IM_TEST method: DIM St AS SGLTEST 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) Visible under MATLAB The input and output parameters of the function are all Variant type variables in the COM component. The test is successful. The result is the same as a touch running under Matlab, cool 2.C # .NET implementation call to open or create a C # project (I use The VS.NET editor), select the reference to the resource manager in the right side, the right mouse button, select the addition reference, select COM in the pop-up window, then find COMTEST_1_0.DLL, click Select, then determine At this time, this COM component is also added to the project. Similarly, we can select the object browser to see COMTEST and the following SGLTest classes, the C # test items are as follows: Test the code of the im_test method: COMTEST.SGLTESTCLASS ST = New Comtenster. SGLTestClass (); st.im_test (); Deng Kefeng dkf03@mails.tsinghua.edu.cn Test split2RGB method: comtes T.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, REF W, REF R, REF G, REF B, FILENAME); Visible input parameters are REF OBJECT type, and output parameters are Object type, the test is also successful, and the results running under MATLAB Just touch, cool.
Three .vc Upset This section is in this part of the VC implementation calling the first part we generated COMTEST_1_0.DLL, and also remember to register MWComutil.dll (how to register first), VB and .NET implementation to COM components Very simple, and VC implementation, I have been explored for a few days (mainly VC is not good) 1. Do some preparations first, start with the Ole Viewer tool - MiCrosoft Visual Studio6.0-- Microsoft Visual Studio6.0 Tools - Ole Viewer (This tool can also be opened by taking Tools - Ole / COM Object Viewer under VC, in the Ole Viewer Tool, select Type Libraries on the right, expand him, find Comtenste 1.0 Type library, select it, right-click, select the view, then pop up a window, point the Save button on the toolbar, save him as Comtenster_1_0.h, Comtest_1_0.c (can also save COMTEST_1_0.IDL interface file) We can implement the COMTEST_1_0.DLL call 2.vc by these two files to be invoked to create or open a VC project, pay attention, not to make any settings for the compiler (and the DLL generated by MCC We set a big Heap, I only set Precompiled Headers, select Automatic Use of Precompiled Headers, write stdafx.h), join Comtensters_1_0.h and comtest_1_0.c, and copy a comte_1_0.dll to the project directory, due to Comtenster_1_0.dll It is also necessary to use MWComutil.dll, so add the MWComutil.h of
} 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) IF (Failed (Coinitialize (NULL)) {AFXMessageBox ("Unable to Initialize COM");} isgltest * ST = NULL; HRESULT HR = CocreateInstance (CLSID_SGLTEST, NULL, CLSCTX_ALL, IID_ID, (void **) & st); Variant M, N, R, G, B , filename; VariantInit (& m); VariantInit (& n); VariantInit (& r); VariantInit (& g); VariantInit (& b); VariantInit (& filename); filename.vt = VT_BSTR; filename.bstrVal = _com_util :: ConvertStringToBSTR ( "C : //1.jpg "); if (succeededed (hr)) {st-> split2RGB (5, M, N, R, G, B, FileName); st-> release (); afxMessageBox (" succeed ") } else {AFXMessageBox ("unsucceed");}, the result is the same as the results under MATLAB, remember that our imshow is used in the IMSHOW. It used to use the MCC generated program, it is wrong. Delicated. See MSDN and its Visual C essence area for COM components and their types of COM components under VC. Deng Kefeng dkf03@mails.tsinghua.edu.cn II. Package: ^ _ ^, Combuilder series can end in Workspace under Matlab, point File-Open Project to build our previous Comtest.cbl project file Open, then point component - package management, you will implement the package. At this time, you can see it in the COMTEST / DISTRIB folder. The generated chast.exe is packaged after packaging, double-click it will decompress some files, then click to extract it _INSTALL.BAT can be installed (it will be done according to his steps)
V. Five. Excellent and shortage commentary This kind of Combuilder can be died in this day, especially when the VC is cut, remember that there is no substantive breakthrough in the essence of Combuilder, I don't think so, its breakthrough It can be great 1. Come, common, any compiler, only the compiler, you can achieve call, want C Builder, Delphi, etc., I want to do it according to the call COM component, you can success 2. Support for IMSHOW and other functions, support for graphics libraries is also strong than before (these also need to test together) 3. Implement the method, not to set up a lot of stack of stacks like previous mixed 4. You can write your own class in matlab and write members, methods, and events for your own class. It is also convenient for management project (this is a bit like VC, VB management project) is too cool, and I don't know. How to write a shortcomings, ^ 0 ^, I think that the shortcomings need to be used together to find out what it is showing a shortcoming, I feel that it is all Variant type, not convenient for later addition:
Six. Variant variables in COM (by Capriccio, thank you) in the Capriccio, thank you, first describe the function of COM components in the example: function y = hehe (x) y with MATLAB M file = x * 2; save this file as Hehe.m to establish an engineering Comparatest in Combuilder, join class paartest, then join file hehe.m, and then build becomes 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" Deng Kefeng DKF03@mails.tsinghua.edu.cn Int Main (int Argc, char * argv []) {double a [4] = {1, 2, 3, 4}; int i = 4; IF (Failed) // Initialization Call COM {Printf ("Unable to Initialize COM");} Variant X, Y; Variantinit (& X); Variantinit (& Y); x.vt = vt_r8 | Vt_Array; // Declaration X Type of Vt_Array, // Array Elements in Double SafeaRaybound Rgsabound [1]; RgsAbound [0] .CGELEments = 4; // Element RgsAbound [0] .llbound = 0; // The lower bound of matrix index is 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 Matlab Combuilder Dunco -feng DKF03@mails.tsinghua.edu.cn, 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. Seven.vb, .NET array and the mutual conversion of Variant variables in COM are also very simple. The following example briefly says: Add a function in the SGLTest in front of us called Y = Paratest ( x), x, y is the input output, under the matrix 1.vb: DIM St AS SGLTEST SET = New SGLTEST DIM X (2) AS VARIANT, Y AS VARIANT X (0) = 3 # x (1) = 5 # x (2) = 6 # Call St.Paratest (1, Y, X) Y (1, Y, X) Y (1, 0), Y (1, 1), Y (1, 2) is the output of the array 2..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 outputs
Eight. Matlab and VC Mixed Programming (Calling COM components with IDL or DLL) This will then provide a method of calling COM components by IDL or DLL (excluding * .h and * .c files) in fact than pass Generate .H and .C files to call to simply :), use the example of the essence 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 Document 2. Open the ClassWizard, click the Add Class-from A Type library class, select the original COMTEST_1_0.TLB (should be in the debug / directory), in fact, this can also directly select Comte_1_0.dll generated with MATLAB's Combuilder ( This saves the first step), and there will be some dialogs that generate Class, which can be done, so that COMTEST_1_0.H and COMTEST_1_0.cpp are generated, and there is more than one ISGLTEST class :) 3. Test The code is as follows: #include "comtest_1_0.h" test code: isgltest st; Afxoleinit (); Dunco - dkf03@mails.tsinghua.edu.cn if (st.created. " SGLTEST "))) {st.im_test (); AFXMessageBox (" Haha, succeeded "); st.releasedispatch ();} else afxMessageBox (" unsucceeded "); the specific meaning of the above code is to see MSDN, I just agreed: )