0

I'm trying to call a string function from a C# DLL that I made (to test COM out). I have a function called add which takes two ints and returns the sum. This seems to work. My other function, returnString, takes a string and returns it. This ends up printing out something blank. Here is the code:

C#:

public class Class1 : MyClass
{
    public string returnString(string a)
    {
        return a;
    }

    public int add(int a, int b)
    {
        return a + b;
    }
}

C++:

int main()
{
    CoInitialize(NULL);

    MyClassPtr obj;
    obj.CreateInstance(__uuidof(Class1));

    BSTR string_result = L"\0";
    int int_result = 0;

    HRESULT hr1 = obj->returnString(L"Hello", &string_result); // should set string_result to "Hello"
    HRESULT hr2 = obj->add(5, 7, (long*)&int_result); // should set int_result to 12

    if (hr1 != S_OK)
        std::cout << "hr1: " << hr1 << std::endl;
    else{ 
        _bstr_t str(string_result);
        std::cout << str << std::endl; // prints a blank line rather than "Hello"
    }

    if (hr2 != S_OK)
        std::cout << "hr2: " << hr2 << std::endl;
    else
        std::cout << int_result << std::endl; // prints 12

    CoUninitialize();
}

Here's what I get when I peak the definitions:

virtual HRESULT __stdcall returnString (
    /*[in]*/ BSTR a,
    /*[out,retval]*/ BSTR * pRetVal ) = 0;
  virtual HRESULT __stdcall add (
    /*[in]*/ long a,
    /*[in]*/ long b,
    /*[out,retval]*/ long * pRetVal ) = 0;

So what's wrong with the code? I posted a similar question recently and found out a little bit more, but I still cannot get it to behave properly. Thank you!

9
  • I could be totally off (I'm not a C#/COM guy), but can a C# string be mapped correctly to a BSTR? In that, when transferring things to and from C++ it's common to use character pointers, as most string types don't translate, Does COM have similar issues? Commented Feb 26, 2016 at 1:41
  • I'm not a C# guy myself. I just wanted to make a C# DLL compatible with C++ so that I could help someone make a more versatile/portable DLL. So I wouldn't know. Commented Feb 26, 2016 at 1:45
  • try std::wcout when printing unicode. Commented Feb 26, 2016 at 1:57
  • @SHR still doesn't work :/ Commented Feb 26, 2016 at 1:58
  • 1
    I figured out the problem. I had to cast L"Hello" to type "CComBSTR", and now it prints out fine. But I will consider standard linkage, as this seems a bit stressful. Commented Feb 26, 2016 at 2:12

1 Answer 1

1

Here was the problem: L"Hello" needed to be casted to type CComBSTR. This is what the result should look like:

HRESULT hr1 = obj->returnString((CComBSTR)L"Hello", &string_result);

Everything else can remain the same :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.