Hi Guys - I'm literally pulling my hair out over this one.
I'm having an issue with string assignment in my unmanaged C++ code. Everything works fine without the C++/CLI wrapper and the C# GUI. However, the string is just not being assigned to what it should be. I've created a very small app to demonstrate this.
These are the source and header files of the unmanaged c++ app:
Test.h
#pragma once
#define DllExport __declspec( dllexport )
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TestCLR;
namespace Test1GUI
{
public partial class Form1 : Form
{
Class1 cs = new Class1();
public Form1()
{
InitializeComponent();
cs.CheckStringAssignment();
}
}
}
If I go into CheckStringAssignment then Process (unmanaged code), I next the line where the assignment happens, and the string remains empty.
Don't know if my comments will help, and I don't do C++ CLI, but you said you were desperate (or pulling your hair out or whatever), so I'll try.
You do realize that strings in object oriented languages are objects, don't you? In other words, they aren't data primitives like integers or floats which the hardware directly supports. In my mind its very unlikely that C++ CLI, which I'm assumming uses the .NET framework, knows anything about std::string, which is a C++ Standard Library specific object. std::string isn't even a part of C++; its in the Standard Library. In terms of myself, I don't even use it. Years ago I wrote my own String Class and I use that.
So I'd actually find it somewhat surprising if I had an unmanaged C++ dll that exported functions or classes that exposed std::string, and a .NET host was able to deal with it.
B) I don't see an assignment in your demo? So I have no idea which string remains empty!
But it appears to do as expected -- when I run the C# app a string is written out to the console.
Note that I created a C# console app, not a Forms app. If you C# app was a Forms app then you'd only see the string output by Test.dll if you created a console for the process.
(Your unmanaged C++ app does nothing so I didn't bother with it. Was it supposed to be something like
std::string isn't even a part of C++; its in the Standard Library.
I think this depends on where you think the borders are. As std::string is part of the C++ Standard Library I think it is part of C++ (in the broader sense.)
Of course, it's not a built-in (or primitive) type, so it's not part of the core language.
So I'd actually find it somewhat surprising if I had an unmanaged C++ dll that exported functions or classes that exposed std::string, and a .NET host was able to deal with it.
That's the point of TestCLI -- it provides a .NET interface to the native C++ "Test" DLL. C++/CLI can interface with native C++ DLLs as well as .NET assemblies.