tags:

views:

51

answers:

1

I need to interface a Linux app to a server that uses bstr data. Can I "roll" my own code to make a bstr? I know the basics of a bstr, that it has a header with the byte size minus the null terminator and because of the header it can contain nulls in the middle of the string and basically all the rest of the rules that follow a bstr.

I am not sure on byte ordering the header or more intimate details such as do I pass the data on pointing to the header or the 5th byte like com does? Does anyone know where I can get this information or if anyone has written a bstr type class for linux? Or in general where I can find info on bstr details rather than just general overviews based on the Microsoft libs?

Thanks

+2  A: 

This may be interesting for you:

Eric's Complete Guide To BSTR Semantics

EDIT: Some more details, as gleaned from that article:

DISCLAIMER: This is off the top of my head, and my contain serious errors, up to but not limited the destruction of causality and the end of the known universe.

struct BSTR_data {
    short count;
    wchar_t[] data;
};

typedef wchar BSTR;

BSTR * AllocateBSTR(wchar * str) {
    if(str == 0) return 0;

    short len = wstrlen(str);

    BSTR_data * ret = new char[sizeof(short) + (sizeof(wchar_t) + 1) * len];

    ret->count = len;

    memcpy(ret->data, str, sizeof(wchar_t) * 2 * len);

    ret->data[len] = 0;

    return (BSTR *)(ret + sizeof(short));
}

void DeallocateBSTR(BSTR * str) {
    if(str == 0) return;

    BSTR_data * bstr = (BSTR_data*)(str - sizeof(short));

    delete bstr;
}

This should give you a good idea of what's going on. Note that if cross-comparability with Win32 is important, you need to use SysAllocString, etc instead of this code.

Mike Caron
Thanks, there is actually an article on MSDN that is a mirror of this, don't know who copied who. This helps but I still need lower lever details.
Dixon Steel
lol Sorry that is the same article on msdn, I'm burned out today.
Dixon Steel
Okay, I pulled some code out of nowhere, and added it to my answer. I make no guarantees if it's correct, however.
Mike Caron
Thanks, I will look this over, it is the detail I am looking for thanks!
Dixon Steel
Why is count a short? I thought the header was 4bytes?
Dixon Steel
Never mind I see it
Dixon Steel
Thanks, so far this seems to be working, I have to wait until my server is up to do a real test but side by side testing next to a "real" BSTR seems to work.
Dixon Steel
This will not work so far in Linux, it will in Windows but porting it is not the same. wchar to me would be a unsigned short but wcslen will not accept and unsigned short if I convert the wchars over to that type. I have tried a bunch of different ways and so far nothing is working for linux. Also the L attribute wont work when allocating, again the type mismatch and so far nothing else will work for me.
Dixon Steel
Actually, it won't work in Windows either, since a BSTR has to be allocated from a special heap, but that's irrelevant on Linux. Anyway, try changing `wchar` to `wchar_t`. I don't think about this normally (`tchar` in Visual Studio is either `char` or `wchar_t`, depending on whether you're compiling unicode or not)
Mike Caron
By the way, if you still have trouble, shoot me an email, so we can stop cluttering Stack Overflow :) We'll work it out, and report back here. My email is "`myLastName`.`myFirstName`@gmail.com" (where the substitutions are exactly as they appear in my user name)
Mike Caron
Thanks, I will send one, I have not been back here in a few days. I have been swamped with some many things.
Dixon Steel