I'm trying to figure out how to make a C# DLL work with C++. I have gotten a simple function like int add(int a, int b) { return a + b; }
to work, however I can't seem to get a string
function to work. I know that string
in C# is different from any type in C++, but here is the code and I would like to know what exactly is going on.
C#:
namespace DllTest
{
[ComVisible(true)]
public interface MyClass
{
string doSomething(string a);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Class1 : MyClass
{
public string doSomething(string a)
{
return a;
}
}
}
C++:
#include <iostream>
#include <Windows.h>
#import "C:\Users\Archie\Documents\Visual Studio 2013\Projects\DllTest\DllTest\bin\Debug\DllTest.tlb" raw_interfaces_only
using namespace DllTest;
int main()
{
CoInitialize(NULL);
MyClassPtr obj;
obj.CreateInstance(__uuidof(Class1));
std::cout << obj->doSomething("Hello");
CoUninitialize();
}
When I call the function, I actually get an error saying that I am missing a parameter, which shouldn't be the case. Here's what pops up when I peek the definition of doSomething
while working with C++:
virtual HRESULT __stdcall doSomething (
/*[in]*/ BSTR a,
/*[out,retval]*/ BSTR * pRetVal ) = 0;
It looks like the function is actually returning a long
now, with two wchar_t
-type parameters. I guess I'm really just wondering why this is happening. Is it even possible to accomplish what I'm trying to do? To make a string function in C# and call it in C++?