need to call managed code from unmanaged c++ code

1k views Asked by At

I wrote COM class in C#, and one function like this :

public void SignAttached(
        byte[] message,
        ref byte[] signedMessage, 
        string userName = "USER"
        )

}

when i try to call this function from C++ code i have exeption: - "SafeArray of rank 12536 has been passed to a method expecting an array of rank 1.Unknown error 0x80131538", and first rank changing every time, C++ code:

SAFEARRAY*message = SafeArrayCreateVector(VT_UI1, 0, 1);
    SAFEARRAY*signedMessage = SafeArrayCreateVector(VT_UI1, 0, 1);

    _bstr_t userName = "USER";

    pInstanse ->SignAttached(message,&signedMessage,userName);

and compilator create wrapper in *.tlh file for function:

HRESULT SignAttached (
    SAFEARRAY * message,
    SAFEARRAY * * signedMessage,
    _bstr_t userName );

and if to see into *.tlb file with help oleviewer (IDL language) exporting function is:

    HRESULT SignAttached(
                [in] SAFEARRAY(unsigned char) message, 
                [in, out] SAFEARRAY(unsigned char)* signedMessage, 
                [in, optional, defaultvalue("USER")] BSTR userName);

can anybody help ?

1

There are 1 answers

1
Martin On

Your C# array parameters are not exposed to COM as SAFEARRAYs. See if you can inspect the type library for your C# code by using tlbexp and then using some tool to view the typelib, but my guess is that you'll see something like

[in] unsigned char *

and

[in,out] unsigned char *

for your first 2 parameters.

I just found oleview.exe on my machine (I thought it wasn't distributed any more). You can use this to inspect your typelib, after exporting with tlbexp. I think you'll need to install a Windows SDK if you don't already have it. For me, oleview.exe is at...

C:\Program Files (x86)\Windows Kits\8.0\bin\x86

Edit: I just tried to use that tool, and it doesn't seem to work nearly as well as I remember. Anyway, I'm pretty sure your typelib won't be using SAFEARRAYs.