Convert C# string to Byte for COM Interop -


i calling c++ com component using interop , marshalling requires 1 of parameters passed in ref byte. argument string. how convert string (or char array) byte pass method?

method idl

 [helpstring("method read")]     hresult _stdcall read(                     [in] unsigned char* path,                      [in] unsigned char command,                      [in] unsigned char nshortaddr,                      [out] short* pndatasize,                      [out] unsigned char** ppbydata,                      [out] unsigned long* pnerror); 

the il

.method public hidebysig newslot virtual          instance void  read([in] uint8& path,                             [in] uint8 command,                             [in] uint8 nshortaddr,                             [out] int16& pndatasize,                             [out] native int ppbydata,                             [out] uint32& pnerror) runtime managed internalcall 

the method in wrapper seen in visual studio

    public virtual void read(ref byte path, byte command, byte nshortaddr,   out short pndatasize, system.intptr ppbydata, out uint pnerror) 

this poorly authored com interface. can work when used in-process c++ code. creator had intended. argument type declaration unsigned char* ambiguous, mean single byte passed reference indicate array of bytes. can use attributes in idl distinguish between 2 not used. type library importer therefore not assume else ref byte.

there's 1 decent fix if source idl can't updated, have edit interop library. decompiling existing 1 first ildasm.exe. edit argument type, best done creating dummy c# interface same declaration example. put humpty-dumpty ilasm.exe


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -