If you can't interact with components that are written in another programming language, the gold content of this programming technique will be greatly compromised. NET environment provides us with a good mechanism for invoking the components written in different programming languages. Just follow. NET can manipulate the code to write components, for the client program, the caller's component is written in which language does not matter, the way of invocation is actually no different.
Here we have written our own components using C + +, VB and C #. This is a simplified dictionary component that is constructed without loading the language library and needs to be done using LoadLibrary. Use the FreeLibrary method to uninstall the language library. The property currentlibrary represents the current language library. Components written in three languages are also invoked in C #, and the reader will be more likely to glance at it.
18.2.1 C + + components
First look at the components in C + +
Program Listing 18-3:
DICTVC.cpp
#using
using namespace System;
Namespace dictve
{
__gc public class dictionarycomponent//Definition Dictionary component
{
private:
string* LanguageName;
string* availablelanguage[];
Public:
dictionarycomponent ()
{
languagename=null;
Availablelanguage=new string*[4];
Availablelanguage[0]=new String (L "Chinese");
Availablelanguage[1]=new String (L "中文版");
Availablelanguage[2]=new String (L "German");
Availablelanguage[3]=new String (L "French");
}
BOOL LoadLibrary (string* Language)
{for
(int i=0;i<4;i++) {
if (Language==availablelanguage[i]) Break
;
if (i==4) {return
false;
}
Languagename=language;
return true;
}
__property string* get_currentlibrary{return
LanguageName;}}
First, we declare a new namespace to encapsulate the new class that is created.
Namespace dictvc{...};
Note that namespaces cannot be nested, but can be separated into multiple files. A simple source code file can also contain more than one nested namespace. In VB and C #, all classes can be manipulated. But in the namespace defined by C + +, we can contain both controllable and not manipulated code, so we need to specifically specify that our new class is modern.
__gc public class dictionarycomponent{...};
This declaration indicates that the class will be created at run time and is controlled by the garbage collector managed heap. We may also specify the/clr option at compile time to ensure that all classes in the program are controllable.
The constructor of a class is invoked every time an instance of the class is created, the name of the constructor is the same as the class name, and there is no return type.
Public:
dictionarycomponent{...};
And because classes should be controllable, we have to explicitly tell the compiler that the array is a manageable object. Therefore, when assigning strings, we use modifiers __gc.
Note: C + + does not have the default initialization function for a variable, you must manually set the value of the languagenamed.
Languagename=null;
Availablelanguage=new String*[4];
Here is the LoadLibrary method, with a string-type argument that returns a Boolean value:
BOOL LoadLibrary (string* language) {
...
if (i==4) return
false;
Languagename=language;
return true;
}
Here is the FreeLibrary method with no parameters and no return value.
void FreeLibrary () {
languagename=null;
}
Finally, we created a read-only property count:
__property string* get_currentlibrary{return
languagename;
}
The command options when compiling this C + + component are slightly more complex:
CL/CLR/C DICTVC.cpp
Link-noentry-dll/out...\bin\dictvc.dll DICTVC
Here, we need to use the/clr option to inform the compiler that the generated code assembly must be controllable. We also need a separate command to perform the link (unlike C # Direct compilation produces target code, C + + must compile and link two processes). Assembly is defined in the. NET structure framework, the physical units that can be deployed, versioned, and reusable. Each assembly builds a collection of types that contain the runtime-based classes and other resources that work together according to the Assembly's designation. The assembly specification describes which components are part of the assembly, which types are derived from the assembly, and the type independence. The runtime environment will use assembly to locate and bind the types you refer to.
For convenience, we assume that the component is created in the "//\bin" subdirectory of the current directory. To specify where the component is created, we use the/OUT option to specify the path full name of the output file.
Note that even if we include the. dll extension in the specified output file name, we must also attach the-dll option to tell the compiler that we are going to create a DLL instead of an executable file.