Hello,
I am trying to write a GUI in Visual C# which controls a C++ DLL, specifically getting hold of C++ objects and editing them via the C# propertygrid controls.
- The first part of the question is 'is this actually possible'?
I know I can use wrapper functions to return an object and marshall it but doesn't that essentially require me re-coding all of the classes in C#? All I want to do I have access to the properties so it seems a little extreme (as well as requiring multiple changes if I change a class).
- To hopefully avoid having to re-code everything, I saw that you can export a C++ class to a DLL but have had no luck in accessing its member functions or properties.
Here is the snippet of code I am attempting to get working:
C++ Class
#define LIB_IMPORT __declspec(dllimport)
#define LIB_EXPORT __declspec(dllexport)
namespace TestNamespace
{
class LIB_EXPORT TestClass;
public class TestClass
{
public:
TestClass()
{
}
virtual ~TestClass()
{
};
virtual int GetIntValue()
{
return value;
}
private:
int value;
};
}
C# access
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using TestNamespace;
namespace DLL
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
TestClass c = new TestClass();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Whilst this code does compile, TestClass c is just a struct that has been declared in metadata with no functions or variables/
Is what I am trying to do possible or am I completely off the mark?
(Also sorry if this is in the wrong place, I assumed that C# people might have more experience with linking dlls)
Any help would be much appreciated.