Create a new C++ project
- Open Visual Studio 2017, File -> New -> Projects, In Visual C++ general tab, select “Empty project”, Enter the project name “MessageBoxShow”, select checkbox “create directory for solution”, click “OK” button.
- Right click on the project, property, in “General” tab, change “Character set” to “Use Unicode Character Set”.
- In this empty project, add a new class “MessageBoxShow”
- Add a new function “ShowMessageBox”, the final result:
MessageBoxShow.h
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include <windows.h>
using namespace std;
class MessageBoxShow
{
public:
MessageBoxShow();
~MessageBoxShow();
void ShowMessageBox();
};
MessageBoxShow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "MessageBoxShow.h"
#include <windows.h>
MessageBoxShow::MessageBoxShow()
{
}
MessageBoxShow::~MessageBoxShow()
{
}
void MessageBoxShow::ShowMessageBox()
{
MessageBox(NULL, L"This is a test", L"test title", MB_ICONWARNING | MB_OK | MB_DEFBUTTON2);
}
- Add a new cpp file, named “main.cpp”, add an empty main function to avoid compile error.
main.cpp
1
2
3
void main()
{
}
- Compile this project, should succeed.
Create a wrapper project
- Right click on solution, “Add” -> “Project”, select “Visual C++” -> “CLR” -> “Class Library”.
- If you could not find “CLR” under “Visual C++” tab, please refer following article to install C++/CLR component.
https://superuser.com/questions/1197876/just-installed-visual-studio-community-2017-but-cannot-find-clr - Input project name “MessageBoxShowWrapper”, click “OK” button
- Right click on this project, property, “Linker” -> “Input”, change “Additional Dependencies” to “inherit from parent or project defaults”
- In MessageBoxShowWrapper.h, rename class name to “MessageBoxShowWrapper”
- Include MessageBoxShow.h and MessageBoxShow.cpp
- Add a new instance of MessageBoxShow class, and initialize this instance in the constructor.
- Add a new function “ShowMessageBox” to call cpp function “ShowMessageBox”
The final code:
MessageBoxShowWrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include ".\..\MessageBoxShow\MessageBoxShow.h"
#include ".\..\MessageBoxShow\MessageBoxShow.cpp"
using namespace System;
namespace MessageBoxShowWrapper {
public ref class MessageBoxShowWrapper
{
public:
MessageBoxShowWrapper();
void ShowMessageBox();
private:
MessageBoxShow * pInstance;
};
}
MessageBoxShowWrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include "MessageBoxShowWrapper.h"
MessageBoxShowWrapper::MessageBoxShowWrapper::MessageBoxShowWrapper()
{
pInstance = new MessageBoxShow();
}
void MessageBoxShowWrapper::MessageBoxShowWrapper::ShowMessageBox()
{
pInstance->ShowMessageBox();
}
- Compile this project, should succeed.
Create a C# proejct
- Right click on solution, “Add” -> “Project”, select “Visual C#” -> “Console App (.NET Framework)”.
- Add new reference, and select “MessageBoxShowWrapper” in “Projects” tab.
- Right click on this project, property, “Build” tab, change “Platform target” to “x86”.
- Input following code in main function:
Program.cs
1
2
3
4
5
6
7
8
9
class Program
{
static void Main(string[] args)
{
MessageBoxShowWrapper.MessageBoxShowWrapper message = new MessageBoxShowWrapper.MessageBoxShowWrapper();
message.ShowMessageBox();
Console.ReadKey();
}
}
- Compile the whole solution, 3 succeed.
- Select this C# project as startup project, press F5 to run, should popup messagebox
Others
Some ways to call C++ in C#:
1
2
3
4
可以用CLR(新)或者Managed c++(老)将lib封装成managed dll供C#直接调用。
将lib封装成native dll,C#中通过DllImport调用dll。
将lib封装成native dll, 再用CLR封装native dll成managed dll供C#直接调用。
将lib封装为COM,在C#中调用COM。
Reference:
http://blogs.microsoft.co.il/sasha/2008/02/16/net-to-c-bridge/
https://www.youtube.com/watch?v=uEQKcjRXMDE
https://www.youtube.com/watch?v=_Fdex3-uiFk
https://www.cnblogs.com/skynet/p/3372855.html
https://www.jianshu.com/p/d985ba365c63