1. Open vs-2005: Create a Win32 console application comincshape.
Compile. No problem.
2. Add a class library project libshape C # to the solution.
(1) Change the class1.cs file name to shapeinterface. CS, and the class name is automatically changed to shapeinterface.
(2) Add the following code to the class:
Private string m_name;
Public shapeinterface ()
{
}
Public void setname (string name)
{
M_name = Name;
}
Public void welcome ()
{
Console. writeline ("Welcome you" + m_name );
}
Compile. No problem.
3. Add an atlproject (the project name is interfaceproxy) to the solution. In application settings, select the attributed attribute. (if no option is selected, it cannot be run later ). Server type is DLL, and others are default.
(1) Add an API iproxyinterface to the project. Add two methods to this interface:
Hresult setname ([in] BSTR name );
Hresult showwelcome (void );
(2) add a simple C ++ class creference to the Project
(3) Select reference. cpp. Right-click and select propertise. In general under C/C ++ item, find compile with common language runtime support and select common language runtime support (/CLR). (do not select common language runtime support, old syntax (/CLR: oldsyntax), which is the old syntax. There are many differences between them );
(4) Compile the reference separately. CPP. the compiler prompts "CL: Command Line Error d8016: '/rtc1' and '/CLR' command-line options are incompatible. Open the reference. the properties attribute of the CPP file. In C/C ++-> codegeneration, the basic runtime checks attribute is now (both (/rtc1, equiv. to/rtcsu. Now change to: default; then compile the reference separately. CPP. the compiler prompts "Command Line Error d8016: '/G' and'/CLR 'command-line options are incompatible ". the same method. Find no in properties. All the errors are listed below:
A: "Command Line Error d8016: '/G' and'/CLR 'command-line options are incompatible "to change properties-> C/C ++-> code generation-> enagle minimal rebuild to No.
B: "error d8016: '/EHS' and '/CLR' command-line options are incompatible "to change properties-> C/C ++-> code generation-> enable C ++ exceptions to No.
D: "d8016: '/zi' and'/CLR 'command-line options are incompatible" change properties-> C/C ++-> General-> debug information format to Disabled
Note: e: "error c2855: command-line option '/CLR 'inconsistent with precompiled header "change properties-> C/C ++-> precompiled headers-> Create/use precompiled header is not using precompiled headers ".
Compile reference. cpp. Compiled succeeded after modifying the preceding errors;
(5) Open the reference. h file and add the following statement under # pragma once:
# Using "../libshape/bin/debug/libshape. dll"
Using namespace libshape; // (to find the DLL you use C );
Compilation is normal.
(6) now add in class creference
Public:
Static shapeinterface ^ m_shape = gcnew shapeinterface ();
Shapeinterface is the class we define in C. Note that the symbols "^" and gcnew are the new marks of the new Public Language Runtime Library, which is a new pointer.
The following error message is displayed when creference. cpp is compiled and modified.
"Error c3265: cannot declare a managed'm _ shape' in an unmanaged 'creference'
May not declare a global or static variable, or a member of a native type that refers to objects in the GC Heap "this is because the creference class is not hosted. In the class view, right-click the class and find properties to set the ismanaged attribute to true. In this case, a symbol ref is added before the class of this class. The content of reference. h file is
『
# Pragma once
# Using "../libshape/bin/debug/libshape. dll"
Using namespace libshape;
Ref class creference
{
Public:
Static shapeinterface ^ m_shape = gcnew shapeinterface ();
Public:
Creference (void );
Public:
~ Creference (void );
};
"Compile again. Everything is normal.
(7) add two static methods to the creference class:
Static void setname (cstring name );
Static void showwelcom ();
The implementation in creference. cpp is as follows:
『
Void creference: setname (cstring namedd)
{
System: String ^ cname = gcnew string (namedd );
M_shape-> setname (cname );
}
Void creference: showwelcom ()
{
M_shape-> welcome ();
}
』
Note: add it under # include "refercefile. H ".
# Using <mscorlib. dll>
Using namespace system; otherwise there is a compilation problem. Pay special attention to the method for converting the cstring type to system: String in setname. This conversion method may not be very good, but it can work and is still under study.
(8) Now, a proxy creference for communicating with C # is ready. Now find the CPP file of the cproxyinterface class that implements the iproxyinterface. The two methods setname (BSTR name) and showwelcome (void) are already in it. Add # include "reference. H" under # include "proxyinterface. H ". Add our proxy class. Now we can implement these two methods. The final result is as follows:
『
# Include "stdafx. H"
# Include "proxyinterface. H"
# Include "refercerence. H"
Stdmethodimp cproxyinterface: setname (BSTR name)
{
Creference: setname (name );
Return s_ OK;
}
Stdmethodimp cproxyinterface: showwelcome (void)
{
Creference: showwelcom ();
Return s_ OK;
}
』
Compilation has many errors. Just like an error in the reference. cpp file, change the file to hosted.
Build until successful.
4 now you can access the methods in C # Through the com of this proxy in other C ++ files. This project is no longer hosted. Find the comincshape. cpp file under the project comincshape. Import The TLB file of the DLL. This is also the longest method to call COM.
# Import "../interfaceproxy/_ interfaceproxy. TLB" rename_namespace ("Daman ")
Add the following statement to the _ tmain function:
『
: Coinitialize (null );
{
Ccomptr <Daman: iproxyinterface> pproxy;
Hresult hR = s_ OK;
HR = pproxy. cocreateinstance (_ uuidof (Daman: cproxyinterface ));
Pproxy-> setname ("Daman ");
Pproxy-> showwelcome ();
}
: Couninitialize ();
』
Note that the braces in the braces do not cause any running errors (You may want ). There are also methods to call interfaces. I think this method is the simplest.
Compilation is fine. However, an error occurs during running. Test bin-> debug-> libshape. dll under libshape to the comincshape application program, and then run. OK.