C#

C#调用C++类

Posted by Kerwen Blog on February 26, 2019

Create a new C++ project

  1. 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.
  2. Right click on the project, property, in “General” tab, change “Character set” to “Use Unicode Character Set”.
  3. In this empty project, add a new class “MessageBoxShow”
  4. 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);
}
  1. Add a new cpp file, named “main.cpp”, add an empty main function to avoid compile error.

main.cpp

1
2
3
void main()
{
}
  1. Compile this project, should succeed.

Create a wrapper project

  1. Right click on solution, “Add” -> “Project”, select “Visual C++” -> “CLR” -> “Class Library”.
  2. 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
  3. Input project name “MessageBoxShowWrapper”, click “OK” button
  4. Right click on this project, property, “Linker” -> “Input”, change “Additional Dependencies” to “inherit from parent or project defaults”
  5. In MessageBoxShowWrapper.h, rename class name to “MessageBoxShowWrapper”
  6. Include MessageBoxShow.h and MessageBoxShow.cpp
  7. Add a new instance of MessageBoxShow class, and initialize this instance in the constructor.
  8. 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();
}
  1. Compile this project, should succeed.

Create a C# proejct

  1. Right click on solution, “Add” -> “Project”, select “Visual C#” -> “Console App (.NET Framework)”.
  2. Add new reference, and select “MessageBoxShowWrapper” in “Projects” tab.
  3. Right click on this project, property, “Build” tab, change “Platform target” to “x86”.
  4. 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();
    }
}
  1. Compile the whole solution, 3 succeed.
  2. 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