From: sample: mixing unmanaged C ++, C ++/CLI, and C # code
We have this simple unmanaged C ++ class unmanagedfoo:
E: \ sample \ Vc \ mixed> More ufoo. h
# Include <stdio. h>
Class unmanagedfoo
{
Public:
Unmanagedfoo () {printf ("constructing unmanagedfoo \ n ");}
~ Unmanagedfoo () {printf ("destructing unmanagedfoo \ n ");}
Void showyourself ();
};
And implementation:
E: \ sample \ Vc \ mixed> More ufoo. cpp
# Include "ufoo. H"
Void unmanagedfoo: showyourself ()
{
Printf ("unmanagedfoo \ n ");
}
Another simple C ++/CLI class managedfoo, with a member of unmanagedfoo.
E: \ sample \ Vc \ mixed> More mfoo. cpp
# Include "ufoo. H"
# Using <mscorlib. dll>
Using namespace system;
Ref class managedfoo
{
Public:
Managedfoo ()
{
Console: writeline ("constructing managedfoo ");
M_foo = new unmanagedfoo ();
If (! M_foo ){
Throw gcnew outofmemoryexception ();
}
}
~ Managedfoo () {showdestruction ();}
! Managedfoo () {showdestruction ();}
Void showyourself ()
{
Console: writeline ("managedfoo ");
M_foo-> showyourself ();
}
PRIVATE:
Void showdestruction ()
{
If (m_foo ){
Delete m_foo;
}
Console: writeline ("destructing managedfoo ");
}
Unmanagedfoo * m_foo;
};
C # class bar uses managedfoo:
E: \ sample \ Vc \ mixed> more bar. CS
Using system;
Class bar
{
Public static void main ()
{
Managedfoo Foo = new managedfoo ();
Foo. showyourself ();
}
}
Let us build the project.
First, compile ufoo. cpp into (unmanaged) OBJ file.
E: \ sample \ Vc \ mixed> cl.exe/Zi/MD/C ufoo. cpp
Microsoft (r) 32-bit C/C ++ Optimizing Compiler version 14.00.50727.42 for 80x86
Copyright (c) Microsoft Corporation. All rights reserved.
Ufoo. cpp
And mfoo. cpp
E: \ sample \ Vc \ mixed> cl.exe/Zi/CLR/C mfoo. cpp
Microsoft (r) C/C ++ Optimizing Compiler version 14.00.50727.42
For Microsoft (R). Net Framework Version 2.00.50727.88
Copyright (c) Microsoft Corporation. All rights reserved.
Mfoo. cpp
And bar. CS, referencing mfoo. OBJ
E: \ sample \ Vc \ mixed> CSC/Target: module/addmodule: mfoo. OBJ bar. CS
Microsoft (r) Visual C #2005 compiler version 8.00.50727.88
For Microsoft (r) Windows (r) 2005 Framework Version 2.0.50727
Copyright (c) Microsoft Corporation 2001-2005. All rights reserved.
Finally, let's link everything together.
E: \ sample \ Vc \ mixed> link mfoo. OBJ ufoo. OBJ bar. netmodule/entry: bar. Main/out: test.exe/Subsystem: console/ltcg
Microsoft (r) incremental linker version 8.00.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Generating code
Finished generating code
Now we have a single file assembly test.exe, mixed with unmanaged C ++, C ++/CLI, and C #.
E: \ sample \ Vc \ mixed> Test
Constructing managedfoo
Constructing unmanagedfoo
Managedfoo
Unmanagedfoo
Destructing unmanagedfoo
Destructing managedfoo
We can also put ufoo. OBJ into a lib and link the Lib in the final step, instead of using ufoo. OBJ directly.
E: \ sample \ Vc \ mixed> lib ufoo. OBJ/out: ufoo. Lib
Microsoft (r) Library Manager version 8.00.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
E: \ sample \ Vc \ mixed> link mfoo. OBJ ufoo. Lib bar. netmodule/entry: bar. Main/out: test2.exe/subsystem: console/ltcg
Microsoft (r) incremental linker version 8.00.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Generating code
Finished generating code
E: \ sample \ Vc \ mixed> Test2
Constructing managedfoo
Constructing unmanagedfoo
Managedfoo
Unmanagedfoo
Destructing unmanagedfoo
Destructing managedfoo