typesbstr

BSTR, how to make your own?


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


Solution

  • 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.