Sample: Mixing Unmanaged C++, C++/CLI, and C# code
2010-07-07 15:20
489 查看
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
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
相关文章推荐
- C++/CLI and C# VS COM and unmanaged Code
- managed and unmanaged code in c#
- [原] bit block transfer (bitBlt) c# , vb and c++cli Implementation
- [C#][SAMPLE][CODE][Control]ListBox和CheckListBox
- This code compacts and repairs an MS Access database from a C# .NET application
- Corporation of cuda and openGL Texture( Source Code sample)
- 101 Visual Basic and C# Code Samples
- Inter-Process Communication (IPC) Introduction and Sample Code
- Java ThreadLocal - How to use and code sample
- Why do I keep getting mixed tabs and spaces in a Visual Studio C# code window?[vs power tools issue transfered]
- Mysql : C# sample code using ObjectDataSources
- How to call code behind method in JavaScript in C# and VS2010?
- [C#][SAMPLE][CODE][Control]TextBox和Validating事件的相关处理
- C# 安全性教程---非托管代码安全性UnmanagedCode security
- Enums and Structs in C#(C#里的枚举和结构) (from codeproject)
- An interesting difference between C# and C++/CLI
- csharp:SQLite and Access using C# code read data
- ASP.NET Razor - C# and VB Code Syntax
- Calling Managed .NET C# COM Objects from Unmanaged C++ Code
- CLR Inside Out: Marshaling between Managed and Unmanaged Code