Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a C++ class and I want to import it into C#.

class __declspec(dllimport) string
	string(const char*);
	string(const std::string&);
	int   size() const;
	int   lenth() const;
	const char* c_str() const;

	string& operator=(const char* str);
	string& operator=(const std::string& str);
	char* _data;
	int _lenth;

What I have tried:

I've tried to convert it to C# but I couldn't find a way to DllImport a class and Operation overloading for '='?
I need an object from this class in my C# code. Is there any way to do this?
Updated 7-Feb-23 23:28pm
Richard MacCutchan 1-Jun-21 8:23am    
See Solution 2

I feel like this is a follow-up to the other question asked here[^], why didn't you just put a comment on that question?

As far as I'm aware you cannot share classes between C# and C++, it's just not a supported option. As I mentioned on the other question, you can use a char* if you need to pass string values between the two. From your original question I don't particularly see why it has to be an instance of your string class?

Otherwise, this StackOverflow question[^] highlights how you can export functions to create, call and drop a class instance. But using the interop services, this just isn't going to be possible.
Share this answer
mohammad hasani 1-Jun-21 8:35am    
Thank you for your response.
I'm using a DLL which is accepting an object of this class 'base::string'.
and I can't change the DLL to accept char* instead of base::string.
I'm importing the DLL in my C# code and I need to pass an object of base::string to the function otherwise It's going to throw an AccessViolation Exception.
The code above is the header for base::string which is implemented in the DLL.
Chris Copeland 1-Jun-21 10:46am    
Chris Losinger below provides a good compromise. If you absolutely cannot change the DLL then why not write an intermediary C++ DLL which has a method which does accept the char*? Then simply have that method forward the invoke onto the correct method after creating the base::string instance?
mohammad hasani 1-Jun-21 12:36pm    
Thank you I'll try that
You cannot import a C++ class into C#. The two languages are completely different and C++ runs under native Windows, while C# requires .NET.

C# programs can access C-style functions via Platform Invoke (P/Invoke) | Microsoft Docs[^].
Share this answer
I suspect you're going to have to create a DLL that can talk to the C++ DLL, and which exports a plain C version of the API. Then, you can use that DLL from C#.

C++ <-> C <-> C#
Share this answer
Importing an class isnt always the best idea, but importing the data is more simple. You cant import classes with members of classes which arent supported.

Read my article Calling-All-Stations and its code about a possible solution.
When transfering string between C++ and C# the best solutions are simple char-arrays or the more complex _bstr_t.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900