I\'m writing a C++ program that needs to call a DLL written in C#. I followed these instructions for creating my C# DLL and linking in to it from my C++.
https://suppor
I couldn't find a concise answer to this question, so I'm putting my solution here in the hope that it helps someone in the future.
TL;DR: You need to use a BSTR to pass strings back and forth between C# and C++.
Here is how I did it.
Here is a sample of my C# code. A few things to note:
interface
section. stringToPrint
argument both in the interface and the function definition. Prefacing string
with [MarshalAs(UnmanagedType.BStr)]
is crucial.string
argument as if it were a normal string. You don't need to convert from BSTR
in C# the way you do in C++. More on that below.//Reference where I got all this:
//https://support.microsoft.com/en-us/kb/828736
// Class1.cs
// A simple managed DLL that contains a method to add two numbers.
using System;
using System.Runtime.InteropServices;
namespace ManagedDLL
{
// Interface declaration.
public interface ICalculator
{
//Test functions
int Add(int Number1, int Number2);
int ReturnAge();
string StringTest();
void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint);
};
// Interface implementation.
public class ManagedClass : ICalculator
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Test functions
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int ReturnAge()
{
return 35;
}
public string StringTest()
{
return "Can you hear me now?";
}
public void PrintAString([MarshalAs(UnmanagedType.BStr)] string stringToPrint)
{
Console.WriteLine("Trying to print a BSTR in C#");
Console.WriteLine(stringToPrint);
Console.WriteLine("Done printing");
}
}
}
A few things to notice in the C++:
#import
call in the header. This is where you tell the C++ how to find your C# library. This is mentioned in the tutorial I link to in the question.AddTest()
function for an example of this.BSTR
type variables. It's fairly easy to convert a std::string
to a BSTR
, I have functions for doing the conversion in either direction.//csLink.h
#include <windows.h>
#include <iostream>
#include <string>
#import "path/to/C#/dll.tlb" raw_interfaces_only
using namespace std;
namespace Sample{
class CSLink {
public:
CSLink();
//Test functions
int AddTest(int i1, int i2);
int AgeTest();
string StringTestCall();
void stringArgTest(string s);
private:
ICalculatorPtr pCalc;
long lResult;
string convertBSTR(BSTR *s);
BSTR convertBSTR(string s);
};
}
//csLink.cpp
#include "stdafx.h"
#include "csLink.h"
using namespace std;
namespace Sample{
//Constructor
CSLink::CSLink(){
cout << "You have created a CS Link" << endl;
//https://support.microsoft.com/en-us/kb/828736
HRESULT hr = CoInitialize(NULL);
pCalc = ICalculatorPtr(__uuidof(ManagedClass));
lResult = 0;
}
//Test functions
int CSLink::AddTest(int i1, int i2){
cout << "you are adding " << i1 << " and " << i2 << endl;
pCalc->Add(i1, i2, &lResult);
cout << "The result should have been " << i1 + i2 << " and it was " << lResult << endl;
return 0;
}
int CSLink::AgeTest(){
cout << "Trying to get my age" << endl;
pCalc->ReturnAge(&lResult);
return lResult;
}
string CSLink::StringTestCall(){
BSTR s;
pCalc->StringTest(&s);
return convertBSTR(&s);
}
void CSLink::stringArgTest(string s)
{
//References I used figuring this all out:
//http://stackoverflow.com/questions/28061637/how-to-pass-string-parameters-between-c-and-c
//https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(v=vs.110).aspx
//http://forums.codeguru.com/showthread.php?193852-How-to-convert-string-to-wstring
//http://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa
BSTR bSTR = convertBSTR(s);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "Testing conversion: " << convertBSTR(&bSTR) << "|end test" << endl;
pCalc->PrintAString(bSTR);
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
//Utility functions
string CSLink::convertBSTR(BSTR *s){
if (*s == nullptr){
return "NULL STRING";
}
else{
wstring ws(*s, SysStringLen(*s));
string ss(ws.begin(), ws.end());
return ss;
}
}
BSTR CSLink::convertBSTR(string s){
wstring wStr = wstring(s.length(), L' ');
copy(s.begin(), s.end(), wStr.begin());
return SysAllocStringLen(wStr.data(), wStr.size());
}
}
That's about it. Comment with any questions, I'll do my best to answer.