C# string marshalling and LocalAlloc

不问归期 提交于 2019-12-02 21:31:20

问题


I have a COM callback from an unmanaged DLL that I need to use in C#. The unmanaged DLL expects the callee to allocate memory using LocalAlloc (which the caller will LocalFree), populate it with WSTR and set value and chars to the WSTR pointer and string length respectively.

Code snippet I'm trying to convert to C#:

STDMETHODIMP CMyImpl::GetString(LPCSTR field, LPWSTR* value, int* chars) {
    CStringW ret;

    if (!strcmp(field, "matrix")) {
        ret = L"None";
        if (...)
            ret.Append(L"001");
        else if (...) 
            ret.Append(L"002");
        else
            ret.Append(L"003");
    }

    if (!ret.IsEmpty()) {
        int len = ret.GetLength();
        size_t sz = (len + 1) * sizeof(WCHAR);
        LPWSTR buf = (LPWSTR)LocalAlloc(LPTR, sz);

        if (!buf) {
            return E_OUTOFMEMORY;
        }

        wcscpy_s(buf, len + 1, ret);
        *chars = len;
        *value = buf;

        return S_OK;
    }

    return E_INVALIDARG; 
}

What would the equivalent C# code be?

EDIT: COM Interface:

[id(2)] HRESULT GetString([in] LPCSTR field, [out] LPWSTR* value, [out] int* chars);


回答1:


Straightforward way would be to import LocalAlloc function, convert the string to bytes using UnicodeEncoding.GetBytes and copy them to allocated memory with Marshall.Copy.



来源:https://stackoverflow.com/questions/26054040/c-sharp-string-marshalling-and-localalloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!