Starting from this example: http://support.microsoft.com/kb/828736 I have tried to add a test function in my C# DLL which takes strings as parameters. My C# DLL code is as follows:
namespace CSharpEmailSender
{
// Interface declaration.
public interface ICalculator
{
int Add(int Number1, int Number2);
int StringTest(string test1, string test2);
};
// Interface implementation.
public class ManagedClass : ICalculator
{
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int StringTest(string test1, string test2)
{
if (test1 == "hello")
return(1);
if (test2 == "world")
return(2);
return(3);
}
}
I then register this DLL using regasm. I use it in my C++ app like so:
using namespace CSharpEmailSender;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
ICalculatorPtr pICalc(__uuidof(ManagedClass));
long lResult = 0;
long lResult2 = 0;
pICalc->Add(115, 110, &lResult);
wprintf(L"The result is %d", lResult);
pICalc->StringTest(L"hello", L"world", &lResult2);
wprintf(L"The result is %d", lResult2);
// Uninitialize COM.
CoUninitialize();
return 0;
}
After running this, lResult is correct (with value of 225), but lResult2 is zero. Any idea what I’m doing wrong?
Without trying to compile this, perhaps the interop expects the string to be of
BSTRtype. Can you try the following?OR