Use C classes in Visual Basic
Create reusable OLE Automation with C Builder
Li Guoxing
I defined a class that generated an AutoCAD DXF graphic exchange file when preparing a project auxiliary rib design program in the Borland C for Windows environment. When writing another auxiliary rib design program with Visual Basic, it needs to generate a DXF graphic file, in order to use Some code, I converted the ordinary C class into the autoMation Object process in Borland C Builder 3.0 for Visual Basic, successfully implemented reuse of different language source program levels. The conversion method is now simply introduced to everyone. For reference.
The original C category declaration is as follows: (For the saving space, there is a large number of simplification only for basic functions)
Class DXF {
Private:
Hfile handle; // DXF file header
String secret; //1
String secend; // tail
String Tables; // Meter String
String blocks; // block section string
String Entities; // Entity section
protected:
Bool WritetitleSec (); // Write the title section
Bool writeblocksec (); // Write block section
Bool WritentitySec (); //
PUBLIC:
DXF ();
Bool SaveTofile (Char * filename); // Save Data to File
Void DXF_LINE (float x1, float y1, float x2, float y2, int layer = 0); // Painted straight line
}
The process of converting C classes into a process server is as follows:
1. Open the integrated development environment of Borland C Builder 3.0, select the New submenu under the File menu, C Builder will open the project selection window, select the ActiveX page, select Active Library in the Active page. Then select the New submenu under the File menu, select the ActiveX page, select Automation Object to open the Automation Object Wizard dialog box in the Active page, fill in DXFCLS in the Class Name box as the class name, single point of the DXF class OK is done. C Builder 3.0 will establish a type library to complete the establishment of the project. And open the type library editing window. (Little description, because Borland C Builder is built when establishing Automation Object, the default is an automated server outside the process. Here, I only need to use an automation server in the process of DLL, so I will first create a library, and then Add the automation server to the type library.
2. Select the project with a DXFCLS name store and set the Unit1 in a DXF.
3. In the Attributes page in the Type Library Editing window to DXF, change the Help String in the HELP group box to DXF library, which will be displayed in the VB's reference selection window. Others don't have to be modified. 4. Exposes a function other than constructors and designs in the original DXF class to VB, which is to select the IDXFCLS node, click Method on the Toolbar, and then modify the Method1 in the Name dialog in the Attributes page to you need. Function name, select the return data type and type ginseng of the Parameters page setting function. When selecting the return data, Note that Bool does not directly support Bool as the return type in the drop-down list box, you can choose Short or select Varian_Bool as the return type, here I choose short to replace BOOL as the return type. In addition, the first two corresponding to the usual C function in the usual C function, the data type, the data type, the version selection of the list. If you want to set the default parameters of the function to double-click the third parameter, the Parameters Flags edit window will pop up, select HAS DEFAULT VALUE in the edit window, and fill in the default data in the following edit box.
5. Click the Refresh on the toolbar, the editor automatically adds the function declaration you set in the type library in your DXF.cpp and DxF.h.
6. Open a DXF.cpp file, you can find a function of SaveTofile and other functions as follows:
Short StdMethodCallType Tdxfclsimpl :: Savetofile (LPSTR FileName)
{
Try
{
}
Catch (Exception & E)
{
Return Error (E.Message.c_str (), IID_IDXFCLS);
}
Return S_OK;
}
Paste the implementation of exposed functions in the original DXF.cpp into the TRY section of each function in the new DXF.cpp, and modify the error handling code and return data. Other private functions are pasted directly into new files, only modify the class name before the function.
7. Open the original DXF.h file, paste the other parts other than the exposed function in the class declaration section to the blend plate, and then open the DXF.H window of the currently engineered, copy the contents of the clipboard to the middle of the TDXfclsimpl class.
8. Select Make DXFCLS in the Project menu to compile the project into a DLL file. This completes the migration of the common class to the automation server in C .
9. Select the Register ActiveX Server under the Run menu to register DXF. You can use the DXF class in VB in the future.
10. In VB 5.0, use the object in the Visual Basic project, select Project | Reference, open the reference window, select the DXF library library, return to the confirmation, then turn on the view | Object Vault, open all library drop-down list After select DXF, Select DXFCLS to see the SaveTOFILE and DXF_LINE functions exposed above. The VB project can be created with DIM DXF AS New DXFCLS. The new DXF implements the declaration and implementation of the class, the fine words in the header file are automatically generated by C Bulider 3.0, and the bold words are added. The reader can automatically generate a frame file comparison with C Bulider 3.0. In order to save space, the automatically generated annotation has been removed.
Attached one. DXF.h header file
#ifndef dxfh
#define dxfh
#include
/ / -------------------------------------------------------------------------------------------- ---------------------------
#include "dxf_tlb.h"
#define libid_dxfcls loid_dxf
Class ATL_NO_VTABLE TDXFCLSIMPL:
AutoObject_Impl (TDXFClsImpl, Dxfcls, IDXFCLS)
{
PUBLIC:
TDXFClsImpl ();
Begin_COM_MAP (TDXFClsImpl)
AutoObject_com_interface_entries (idxfcls)
END_COM_MAP ()
Declare_typed_comserver_registry ("dxf.dxfcls")
Private:
Hfile Handle;
Int vports;
int Layernum;
STRING SecStart;
String secend;
String Tables;
String blocks;
String Entities;
Void WritetitleSec ();
Void WriteBlocksec ();
Void Writeentitiessec ();
protected:
Short StdMethodCallType Savetofile (LPSTR FileName);
Stdmethod (DXF_LINE (Float X1, Float Y1, Float X2, Float Y2, Short Layer);
}
#ENDIF
Addition. DXF.cpp file
#include
#pragma HDRSTOP
#include
#include
#include "dxf.h"
/ / -------------------------------------------------------------------------------------------- ---------------------------
#pragma package (smart_init)
TDXFCLSIMPL :: TDXFClsImpl ()
{
SECSTART = "0 / nsection / n";
SECEND = "0 / NendSec / N";
Vports = 2;
Layernum = 1;
Tables = "2 / n / Tables / N 0 / N / Table / N 2 / N / VPORT / N / 70 / N";
Blocks = "2 / nblocks / n";
Entities = "2 / n / Entities / N";
}
Void TdxfclsImpl :: WriteTitleSec ()
{
Write (Handle, SecStart.c_str (), second.length ()); // Write head
Write (Handle, "2 / NHEADER / N", Strlen ("2 / NHEADER / N");
Write (handle, secend.c_str (), secend.Length ()); // Write
}
Void TdxfclsImpl :: WriteBlocksec ()
{
Write (Handle, SecStart.c_str (), second.length ()); // Write head
Write (Handle, Blocks.c_Str (), Blocks.Length ()); Write (Handle, SECEND.C_STR (), secend.length ()); // Write
}
Void TdxfclsImpl :: WritentityIssec () // Real Substitution
{
Write (Handle, SecStart.c_str (), SecStart.length ());
Write (Handle, Entities.c_STR (), Entities.Length ());
Write (Handle, Secend.c_STR (), Secend.Length ());
}
Short StdMethodCallType Tdxfclsimpl :: Savetofile (LPSTR FileName)
{
INT I;
Try {
IF (FileExists (fileEname) {
i = filegetattr (filename);
IF (I & FAREADOONLY)
FileSetattr (Filename, I & 0xfe);
Deletefile (filename);
Handle = Open (filename, o_creat | o_text | o_rdwr);
IF (Handle == - 1) Return False;
WritetitleSec (); // Write the title section
Writeblocksec ();
Writeentitiessec ();
Write (Handle, "0 / NeOF / N", Strlen ("0 / NEOF / N");
Close (Handle);
}
Catch (Exception & E)
{
Return False;
}
Return True;
}
/ / -------------------------------------------------------------------------------------------- ---------------------------
StdMethodimp TdxfclsImpl :: DXF_LINE (Float X1, Float Y1, Float X2, Float Y2,
Short Layer)
{
Char buffer [16];
Try {
Entities = "0 / nLINE / N 8 / N";
Entities = INTOSTOSTR (Layer);
Entities = "/ n 10 / n";
Sprintf (buffer, "% 6.1f", x1);
Entities = buffer;
Entities = "/ n 20 / n";
Sprintf (buffer, "% 6.1f", y1);
Entities = buffer;
Entities = "/ n 30 / n 0.0 / n 11 / n";
Sprintf (buffer, "% 6.1f", x2);
Entities = buffer;
Entities = "/ n 21 / n";
Sprintf (buffer, "% 6.1f", y2);
Entities = buffer;
Entities = "/ n 31 / n 0.0 / n";
Catch (Exception & E) {
Return Error (E.Message.c_str (), IID_IDXFCLS);
}
Return S_OK;
}
Examples with three:
Built a new window in VB 5, put a commandbutton on the window, name cmdcreate, then enter the following code, then run, click CmdCreate to generate the .dxf files used under basic autocad.
Private subdcreate_click ()
DIM DXF AS New DXFCLS
DXF.DXF_LINE 100, 100, 500, 100
DXF.SAVETOFILE App.Path & "/Dxftest.dxf"
End Sub
The above procedures run through the Borland C Builder 3 Server / Client Suit and VB 5.0. The generated dxftest.dxf displays the normal graphics in AutoCAD for Windows 12.
Little experience: Now the various languages provide the function of creating an OLE Automation server, providing us to reuse the previous code and use a variety of languages to make mixed programming. We can use a variety of languages to quickly develop efficient application software.
Reference: Borland C Builder 3 Server / Client Suit Online Help File.