<p>A C++ CLR (Common Language Runtime) Class Library simplifies the interop process between C++ and C#.</p>
<blockquote>
<p>Ensure that the project configurations (x86 or x64) match between the
C# application and the C++ CLR Class Library to prevent compatibility
issues.</p>
</blockquote>
<p><strong>1. Create the C++ CLR Class Library:</strong></p>
<p>Start by creating a new C++ CLR Class Library project in Visual Studio. This type of project allows you to write managed C++ code that can be easily accessed from C#.</p>
<p><strong>2. Define the Managed Class:</strong></p>
<p>Start by creating a new C++ CLR Class Library project in Visual Studio. This type of project allows you to write managed C++ code that can be easily accessed from C#.</p>
<p><em>MyCppCLRClass.h</em></p>
<pre><code>#pragma once

using namespace System;

namespace MyCppCLRClassLibrary
{
    public ref class MyCppCLRClass
    {
        public:
            void SendMessageFromCSharp(String^ message);
    };
}
</code></pre>
<p><em>MyCppCLRClass.cpp</em></p>
<pre><code>// MyCppCLRClass.cpp
#include &quot;MyCppCLRClass.h&quot;
#include &lt;iostream&gt;

namespace MyCppCLRClassLibrary
{
    void MyCppCLRClass::SendMessageFromCSharp(String^ message)
    {
        std::cout &lt;&lt; &quot;Message received in C++ CLR Class: &quot; &lt;&lt; msclr::interop::marshal_as&lt;std::string&gt;(message) &lt;&lt; std::endl;
        // Process the message or perform any required actions
    }
}
</code></pre>
<p><strong>3. Build the C++ CLR Class Library:</strong></p>
<p>Build the C++ CLR Class Library project. Visual Studio should generate a managed DLL that you can use from your C# application.</p>
<p><strong>4. Create the C# WPF Application:</strong></p>
<p>Create a new C# WPF application project or use your existing one.</p>
<p><strong>5. Reference the C++ CLR Class Library:</strong></p>
<p>In your C# project, add a reference to the compiled C++ CLR Class Library DLL.</p>
<p><strong>6. Call the Managed C++ Functions:</strong></p>
<p>In your C# code, you can directly call the functions exposed by the managed C++ class:</p>
<pre><code>using System;
using MyCppCLRClassLibrary;

namespace MyCSharpWpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string message = &quot;Hello from C#!&quot;;
            MyCppCLRClass myCppCLRInstance = new MyCppCLRClass();
            myCppCLRInstance.SendMessageFromCSharp(message);
        }
    }
}
</code></pre>
<p><strong>7. Run the Application:</strong></p>
<p>Run your C# WPF application, and when the button is clicked, it should call the function in the C++ CLR Class Library.</p>

CC BY-SA 4.0
2023-08-16T00:13:17.477Z
2023-08-16T00:54:07.043Z
2023-08-16T00:54:07.043Z
Arjan Singh
<p>I have a C++ graphics engine and I would like to build an editor for it in C#. I want to be able to send data from the editor to the engine such as what tool is being used and any user-defined parameters.</p>
<p>In a previous <a href="https://computergraphics.stackexchange.com/questions/12791/hosting-opengl-c-window-in-c-wpf">question</a>, I have already found a method to render my C++ engine to a C# window.</p>

<c++><ui><c#>
How to send data from a C# application to a C++ DLL?
Arjan Singh
# How to send data from a C# application to a C++ DLL?

(Asked by: username_0 on 2023-08-16)

I have a C++ graphics engine and I would like to build an editor for it in C#. I want to be able to send data from the editor to the engine such as what tool is being used and any user-defined parameters.

In a previous [question](https://computergraphics.stackexchange.com/questions/12791/hosting-opengl-c-window-in-c-wpf), I have already found a method to render my C++ engine to a C# window.

**Tags:** c++, ui, c#


---

## 1 answers

### Answer by username_0 on 2023-08-16. Score: 0 | Accepted answer

A C++ CLR (Common Language Runtime) Class Library simplifies the interop process between C++ and C#.

> Ensure that the project configurations (x86 or x64) match between the
> C# application and the C++ CLR Class Library to prevent compatibility
> issues.

**1. Create the C++ CLR Class Library:**

Start by creating a new C++ CLR Class Library project in Visual Studio. This type of project allows you to write managed C++ code that can be easily accessed from C#.

**2. Define the Managed Class:**

Start by creating a new C++ CLR Class Library project in Visual Studio. This type of project allows you to write managed C++ code that can be easily accessed from C#.

*MyCppCLRClass.h*

```
#pragma once

using namespace System;

namespace MyCppCLRClassLibrary
{
    public ref class MyCppCLRClass
    {
        public:
            void SendMessageFromCSharp(String^ message);
    };
}

```

*MyCppCLRClass.cpp*

```
// MyCppCLRClass.cpp
#include "MyCppCLRClass.h"
#include <iostream>

namespace MyCppCLRClassLibrary
{
    void MyCppCLRClass::SendMessageFromCSharp(String^ message)
    {
        std::cout << "Message received in C++ CLR Class: " << msclr::interop::marshal_as<std::string>(message) << std::endl;
        // Process the message or perform any required actions
    }
}

```

**3. Build the C++ CLR Class Library:**

Build the C++ CLR Class Library project. Visual Studio should generate a managed DLL that you can use from your C# application.

**4. Create the C# WPF Application:**

Create a new C# WPF application project or use your existing one.

**5. Reference the C++ CLR Class Library:**

In your C# project, add a reference to the compiled C++ CLR Class Library DLL.

**6. Call the Managed C++ Functions:**

In your C# code, you can directly call the functions exposed by the managed C++ class:

```
using System;
using MyCppCLRClassLibrary;

namespace MyCSharpWpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string message = "Hello from C#!";
            MyCppCLRClass myCppCLRInstance = new MyCppCLRClass();
            myCppCLRInstance.SendMessageFromCSharp(message);
        }
    }
}

```

**7. Run the Application:**

Run your C# WPF application, and when the button is clicked, it should call the function in the C++ CLR Class Library.


---

