2
    cVector3d newPosition = tool->getDeviceGlobalPos();
    char sendBuf[sizeof(double)*3 + 1];
    int sendBufLen = sizeof(double)*3 + 1;

     memset( (void *)&sendBuf, '\0', sizeof(sendBuf));
    memcpy(&sendBuf, &newPosition[0], sendBufLen -1);

where cpi is a pointer to a com interface's object. Multicast is a c# method which accepts string arguments. When I print the message.length that Multicast has received, which is sendBuf it just says 2 instead of 24.

reference for cvector3d http://www.chai3d.org/doc/structc_vector3d.html

what is wrong with cpi->Multicast(sendBuf); ?

--edit-- I need to convert sendBuf to _bstr_t I think

I converted to _bstr_t but the message.length still shows 2 instead of 24?

I used

_bstr_t bstrt(sendBuf);

I guess bstr is corrupt as when I debug I see a BAD PTR in bstr->m_data->m_str


----edit---- Whnen I convert "123456789012378901234"

char sendBuf[sizeof(double)*3 + 1];
memcpy(&sendBuf, "123456789012345678901234", sendBufLen -1);
_bstr_t bstrt(sendBuf);

bstrt has "123456789012378901234"

but when I have weird characters in sendBuf as in for eg. sendBuf[14] = "16 'ð' " which my program needs bstr doesnt get these values from sendBuf

why?

3 Answers 3

1

As your edit shows, check out _bstr_t.

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

1 Comment

@user494461 It's worth noting (and this may or may not be related), that your char buffer is not zero-terminated. It needs to be sizeof(double)*3 + 1. You'd be advised to memset that buffer to 0 prior to the memcpy call.
0

You cannot make this work. You are copying binary data into a string. On the .NET side, the char[] gets marshaled to a .NET string, a utf-16 encoded version of the char[] that's converted by applying the system default locale. Which changes the char values. There's more, the bytes you copy into the char[] may contain a zero, it acts as a string terminator in C strings.

You need to rewrite the C# version to take a byte[] instead. On the C side, you need to create a SAFEARRAY to match that argument. Use SafeArrayCreate() to create it.

1 Comment

I changed the c# code to accept byte[] but I a somewhat lost as what do in cpp to get a byte array from char*
0
_bstr_t bstrt(sendBuf);
cpi->Multicast(bstrt);

Comments

Your Answer

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