C ++ Builder Upset Matlab's COM Summary

zhaozj2021-02-16  69

C Builder Upset Matlab's COM Summary

Hu Jinsong

(Zhejiang University Postdoctoral Mobile Station, Postdoctoral Workstation in Ningbo Bonded Area, Ningbo Bonded Area, 315800)

Component Object Models (COM) are binary standards for Windows objects. Provide unified, object-oriented, expandable communication protocols for Windows. This means that describes the executable code of an object (code of .dll or .exe file) can be performed by other objects. Even if both objects are written in different languages, they can communicate with COM standards. MathWorks launched a tool COM Builder compiled as a COM component in Matlab 6.5, using this tool to easily create a desired COM component for other support for COM, implementation MATLAB and other programming languages ​​integrate.

The following will be described in a specific example of C Builder5 and Matlab6.5 to achieve a specific implementation of hybrid programming through COM.

1 Matlab compile environment settings

MATLAB COM Builder requires an external compiler when compiling to generate COM components. Because not all compilers can generate a COM component compatible with Microsoft, COM Builder only supports the following compilers:

Borland C Builder 3

Borland C Builder 4

Borland C Builder 5

Microsoft Visual Studio 5.0

Microsoft Visual Studio 6.0

Microsoft Visual Studio.net

After matlab installed COM Builder (Generally, when you choose Matlab, you can set the external compiler used by the MATLAB COM Builder. Can be implemented by MBUILD-SETUP.

In Matlab's Command Window Enter the Mbuild-Setup, and select the appropriate compiler according to the prompt. The author uses Microsoft Visual Studio 6.0, the process is as follows:

>> MBUILD -SETUP

Please choose your compiler for building Standalone Matlab Applications:

Would You Like Mbuild To Locate Installed Compilers [Y] / n?

SELECT A Compiler:

[1] LCC C Version 2.4 in d: / matlab6p5 / sys / lcc

[2] Microsoft Visual C / C Version 7.0 in C: / Program Files / Microsoft Visual Studio .NET

[3] Microsoft Visual C / C Version 6.0 in D: / Microsoft Visual Studio

[0] none

Compiler: 3

Please Verify your Choices:

Compiler: Microsoft Visual C / C 6.0

Location: D: / Microsoft Visual Studio

Are Thase Correct? ([Y] / N):

Specific tips will vary depending on the case.

If our work path is in E: / matSt, enter the command CD ('E: / matSt') in Command WINDOW; McCSAVEPATH; Set Matlab provides the search path provided by the compiler, which can only be entered once.

2 MATLAB test files used by using Matlab COM Builder to make the MATLAB test files used for Plotdata.m, as follows:

Function [myout] = Plotdata (data)

Myout = data;

Plot (DATA);

Set (GCF, 'MenuBar', 'None');

This function is quite simple, there is an input variable DATA and an output variable myout, the function is displayed, and the input data assigns the input data to the output data, although it is simple, but already contains the interface, typical.

Enter COMMAND WINDOW in Matlab, start the COM Builder graphical user interface as shown in Figure 1:

Figure 1 COM Builder main window

Then use File-> New Project ... to establish new projects, the following project settings window appear, as shown in Figure 2. Enter the name of the COM component you want to generate in Component Name, for example, we entered the name Testout, and the name of the final COM component is this name plus version information. Pressing the COM component to add and delete the class (Class) by add >> and remove, which can be easily added to this COM component. Here, we add a class Testout to this component. Project Version is set to easily manage the COM components you generated by users, through this version number, users can easily distinguish between the same name of the same name for different periods. Project Directory is a directory stored throughout the project. Recommended in Compile Code in, because C code performance is better, C code readability, because the last use is a DLL, so it is not necessary to see the intermediate code, so select a better performance C code. If you want to use the MATLAB graphics library, you will choose to select Use Handle Graphics Library in Compiler Options. Checking Builddebug Version generates a debug version of COM component. If there is an error while debugging, there will be corresponding error prompt information, which can be easily positioned with the error, not the debug version COM will not give any error prompt information when an error occurs. Checking show verbose Output can output detailed process information when compiling to generate COM components. After the engineering creation, you can still change this window by menu Project-> Settings .... A COM component can contain many classes (Class), the addition method is added in Project Setting.

Figure 2 Project Settings Window

Adding a method to a class is very simple: first use the classes you want to operate, then use the menu project-> add file ... or press NewD file to add a ready-made (pre-written) file. Note * .m files cannot be a script file, can only be a function file. Add a PlotData.m file so that a simple method is added to the class. After all functions are added, you can compile using the menu build-> com object or press the New BUILD. After compiling, you will generate two folders under the project folder: one is SRC, stored in some intermediate files; one is distrib, it is what we want. After compiling, MATLAB will automatically make generated Component, we can use the menu Component-> Component Info ... to remove the details of TESTOUT_1_0.DLL in the system registry to see the information about the generated Component. 3 COM components in 3 C Builder

Be sure to register the components before using the component, and the registration work MATLAB has been completed for us. Click on C Builder Project-> Import Type Library ..., the interface shown in Figure 3 appears, find Testout 1.0 Type Library [Version 1.0] in the above window, then click the Install ... button,

Figure 3 Installing COM components

TESTOUT control

Figure 4 Testout control

The Testout control is installed in the AXTIVEX control page, as shown in Figure 4. Create a project file Project1, placed a Testout control and a button control on the Form, as shown in Figure 5. Double-click the button, then add the following code:

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

Double SRC [100]; // Incoming data array;

Double Outp [100]; // Output data array;

SafeArray InsafeArray; // Used for incoming security arrays;

TvariantInparam Data; // Incoming the variant of COM;

Tvariant myout; // Out of variants of COM;

FOR (int i = 0; i <100; i ) // incoming data assignment;

{

SRC [I] = COS (i);

}

// Set the input security array;

InsafeArray.cdims = 1; // Array is one-dimensional;

InsafeArray.ffeatures = fadf_auto; // Allocated on the pile of this application;

InsafeArray.cbelements = sizeof (double); // The size of the array element is Double size;

Insafearray.rgsabound [0] .llbound = 0; // The boundary of the array is 0;

INSAFEARRAY.CLOCKS = 1;

InsafeArray.rgsabound [0]. C. C. C, //Rroups with 100 elements;

Insafearray.pvdata = src; // array references SRC pointer;

Data.setsafeArray (& InsafeArray); // Incoming the variant setting of the COM setup security array;

Data.vt = vt_r8 | vt_Array; // variant is the array type, array is Double type;

Testout1-> Plotdata (1, & myout, data); // Call the method of COM, the first one is the number of output, the second component output, the third is the data variant input COM;

Memcpy (outp, myout.parray-> pvdata, 100 * sizeof (double)); // copy the output variant to

/ / Output data array;

}

Figure 5 Establishment Project Project1

The SRC array in the program is to save data from the COM control. The OUTP array is the data that saves the data from the COM control, which enables the data interface of the application and COM. This code passes 100 points of cosine data to the COM control, and then the control is responsible for drawing, and the data is passed. Because COM does not support pointers, the data is transferred to the COM control using the variable of secure array type SafeArray. Run the program, click the button, and the running interface coming out is shown in Figure 6, it can be seen that it is exactly the same as in the MATLAB environment.

Figure 6 Operation interface

4 COM components package and programs release

If the program is developed, it will be released. For the COM component to be used, you can use Matlab COM Builder to package. The specific operation is done by menu company component-> package component. A self-extracting file TESTOUT.exe is generated under the Distrib to implement the necessary dynamic link library installation and the registration of COM components, follow the prompts. The main operation has two: one is to enter the decompressed directory, one is to set the system's search path (PATH). The problem that needs attention is that the input folder directory cannot contain Chinese (it is a regret, after all, this is not a Chinese development). Datual, you can run the program.

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

New Post(0)