How to serialize a VARIANT or BSTR?

3

I am trying to figure out some COM marshalling features. Eventually, I want to persist a deeply nested variant array to file but I am trying first with a simple string. I realise these APIs are meant for remote procedure calls but I was hoping this serialization was also suitable for file persistence.

I work a lot with Excel VBA and to discover this serialization APIs is a real eye opener.

The code below serializes a BSTR to a buffer, the buffer is copied which will serve as a substitute for saving to and loading from a file.

The current problem is the BSTR_UserUnmarshal is throwing an exception Unhandled exception at 0x7631C762 (KernelBase.dll) in RPCMarshalling.exe: 0x00000057: The parameter is incorrect. occurred

I probably have the syntax wrong because I am working with scraps of sample code. The goal is to get the string from srctest to desttest with RPC serialization API calls.

// RPCMarshalling.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>

//https://searchcode.com/file/140723732/dlls/oleaut32/tmarshal.c#l-818

typedef struct _marshal_state {
    LPBYTE  base;
    int     size;
    int     curoff;
} marshal_state;


int main()
{
    ::CoInitialize(0);

    CComBSTR srctest("Hello");
    marshal_state srcbuf;
    memset(&srcbuf, 0, sizeof(srcbuf));
    ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);

    ULONG size = ::BSTR_UserSize(&flags, 0, &srctest);

    DWORD newsize = max(size, 256);
    (&srcbuf)->base = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
    if (!(&srcbuf)->base)
        return E_OUTOFMEMORY;

    ::BSTR_UserMarshal(&flags, (&srcbuf)->base + (&srcbuf)->curoff, &srctest);
    (&srcbuf)->curoff = size;


    std::cout << "Hello World!\n" << size << "\n";


    marshal_state destbuf;
    memset(&destbuf, 0, sizeof(destbuf));

    (&destbuf)->base = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
    if (!(&destbuf)->base)
        return E_OUTOFMEMORY;

    /* pretend we are loading from file saved by src buffer */
    RtlCopyMemory((&destbuf)->base, (&srcbuf)->base, newsize);


    CComBSTR desttest("");
    BSTR deststring;

    try
    {
        unsigned char *buffer;
        buffer = ::BSTR_UserUnmarshal(&flags, (&destbuf)->base, &deststring);
    }
    catch (int e)
    {
        std::cout << "Error:" << e << "\n" << size << "\n";
    }



    ::CoUninitialize();

}
c++
com
marshalling
asked on Stack Overflow Jun 9, 2019 by S Meaden • edited Jun 10, 2019 by S Meaden

2 Answers

0

An alternative method to achieve the goal of serializing a Variant (as per OP's question text) Work in progress, using Simon Mourier's suggestion about StgSerializePropVariant

int main()
{
    ::CoInitialize(0);

    CComVariant srctest("Hello");

    SERIALIZEDPROPERTYVALUE* serialized;
    ULONG cb;
    ::StgSerializePropVariant((PROPVARIANT*)&srctest, &serialized, &cb);

    CComVariant pvDest;
    ::StgDeserializePropVariant(serialized, cb, (PROPVARIANT*)&pvDest);

    CoTaskMemFree(serialized);

    CComBSTR strDest(pvDest.bstrVal);
    std::cout << "This got serialized:\n" << LPCSTR(_bstr_t(strDest, true)) << "\n";

    ::CoUninitialize();
}
answered on Stack Overflow Jun 10, 2019 by S Meaden • edited Jun 10, 2019 by S Meaden
0

And actually if serializing is the goal then why not serialize to an IStream

int main()
{
    ::CoInitialize(0);

    CComVariant srctest("Hello");

    CComPtr<IStream> pStream;
    HRESULT hr;
    hr = CreateStreamOnHGlobal(NULL, TRUE,(LPSTREAM *) &pStream.p);
    if (hr != S_OK) return hr;

    hr = srctest.WriteToStream(pStream, VT_BSTR);
    if (hr != S_OK) return hr;

    {
        // stream needs resetting to the start before we 
        // attempt to read it
        LARGE_INTEGER  dlibMove;
        dlibMove.HighPart = 0;
        dlibMove.LowPart = 0;
        DWORD dwOrigin;
        dwOrigin = 0;
        ULARGE_INTEGER  libNewPosition;
        hr = pStream->Seek(dlibMove, dwOrigin, &libNewPosition);
        if (hr != S_OK) return hr;
    }

    CComVariant pvDest;
    hr = pvDest.ReadFromStream(pStream, VT_BSTR);
    if (hr != S_OK) return hr;

    CComBSTR strDest(pvDest.bstrVal);
    std::cout << "This got serialized:\n" << LPCSTR(_bstr_t(strDest, true)) << "\n";
    //std::cout << "This got serialized:\n" << strDest.m_str << "\n";

    ::CoUninitialize();
}
answered on Stack Overflow Jun 10, 2019 by S Meaden • edited Jun 10, 2019 by S Meaden

User contributions licensed under CC BY-SA 3.0