I need to use a third party COM component. One of the methods has the idl signature:- HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, [out, retval] BYTE* pHashResult); Both pDataBuffer and pHashResult are pointers to BYTE arrays, the size of pDataBuffer is governed by nDataBufferSize; pHashResult has a fixed size. When I use tlbimp to create a C# wrapper library, the method signature resulting is:- byte CalculateHash(ref byte pDataBuffer, int nDataBufferSize); Given a byte array byteData, I am hoping that "ref byteData[0]" can be marshalled successfully for the parameter, but I am stumped as to how to access the return value. Any ideas? Thanks in advance, -- Tony Towers
Tony, [quoted text, click to view] >HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, >[out, retval] BYTE* pHashResult);
If the method returns a byte array, the type of pHashResult should be BYTE**. [quoted text, click to view] >Given a byte array byteData, I am hoping that "ref byteData[0]" can be >marshalled successfully for the parameter,
You'd probably get away with it, but it's not correct. A better way to do it is described here http://msdn2.microsoft.com/en-us/library/ek1fb3c6.aspx Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Hi Mattias, [quoted text, click to view] Mattias Sj=F6gren wrote: > Tony, > > >HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, > >[out, retval] BYTE* pHashResult); > > If the method returns a byte array, the type of pHashResult should be > BYTE**.
Oh, I agree. Unfortunately it's a third-party component over which I have no control. [quoted text, click to view] > >Given a byte array byteData, I am hoping that "ref byteData[0]" can be > >marshalled successfully for the parameter, > > You'd probably get away with it, but it's not correct. A better way to > do it is described here > > http://msdn2.microsoft.com/en-us/library/ek1fb3c6.aspx The "Preserve Signature" section looks like it could be a solution to the problem, thank-you. --=20 Tony Towers
[quoted text, click to view] tony.towers@gmail.com wrote: > I need to use a third party COM component. One of the methods has > the idl signature:- > > HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, > [out, retval] BYTE* pHashResult); > > Both pDataBuffer and pHashResult are pointers to BYTE arrays, the size > of pDataBuffer is governed by nDataBufferSize; pHashResult has a fixed > size. > > When I use tlbimp to create a C# wrapper library, the method signature > resulting is:- > > byte CalculateHash(ref byte pDataBuffer, int nDataBufferSize); > > Given a byte array byteData, I am hoping that "ref byteData[0]" can be > marshalled successfully for the parameter, but I am stumped as to how > to access the return value. > > Any ideas? > > Thanks in advance, >
Try uint CalculateHash( [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeParamIndex=1), In] byte[] pDataBuffer, int nDataBufferSize, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeConst=<int>, In, Out] byte[] pHashResult); replace "<int>" part in SizeConst attribute with real value of array size. pHashResult better be preallocated before call. You might still
[quoted text, click to view] Michael Shishkin wrote: > tony.towers@gmail.com wrote: > > I need to use a third party COM component. One of the methods has > > the idl signature:- > > > > HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, > > [out, retval] BYTE* pHashResult); > > > > Both pDataBuffer and pHashResult are pointers to BYTE arrays, the size > > of pDataBuffer is governed by nDataBufferSize; pHashResult has a fixed > > size. > > > > When I use tlbimp to create a C# wrapper library, the method signature > > resulting is:- > > > > byte CalculateHash(ref byte pDataBuffer, int nDataBufferSize); > > > > Given a byte array byteData, I am hoping that "ref byteData[0]" can be > > marshalled successfully for the parameter, but I am stumped as to how > > to access the return value. > > > > Any ideas? > > > > Thanks in advance, > > > > Try > > uint CalculateHash( > [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, > SizeParamIndex=1), In] > byte[] pDataBuffer, > int nDataBufferSize, > [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, > SizeConst=<int>, In, Out] > byte[] pHashResult); > > replace "<int>" part in SizeConst attribute with real value of array > size. pHashResult better be preallocated before call. You might still > need to play with In, Out attributes in pHashResult to get it working right.
I'm afraid I don't understand - this declaration should go into the IL file before recompiling the wrapper library? -- Tony Towers
[quoted text, click to view] tony.towers@gmail.com wrote: > Michael Shishkin wrote: >> tony.towers@gmail.com wrote: >>> I need to use a third party COM component. One of the methods has >>> the idl signature:- >>> >>> HRESULT CalculateHash([in]BYTE* pDataBuffer, [in]long nDataBufferSize, >>> [out, retval] BYTE* pHashResult); >>> >>> Both pDataBuffer and pHashResult are pointers to BYTE arrays, the size >>> of pDataBuffer is governed by nDataBufferSize; pHashResult has a fixed >>> size. >>> >>> When I use tlbimp to create a C# wrapper library, the method signature >>> resulting is:- >>> >>> byte CalculateHash(ref byte pDataBuffer, int nDataBufferSize); >>> >>> Given a byte array byteData, I am hoping that "ref byteData[0]" can be >>> marshalled successfully for the parameter, but I am stumped as to how >>> to access the return value. >>> >>> Any ideas? >>> >>> Thanks in advance, >>> >> Try >> >> uint CalculateHash( >> [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, >> SizeParamIndex=1), In] >> byte[] pDataBuffer, >> int nDataBufferSize, >> [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, >> SizeConst=<int>, In, Out] >> byte[] pHashResult); >> >> replace "<int>" part in SizeConst attribute with real value of array >> size. pHashResult better be preallocated before call. You might still >> need to play with In, Out attributes in pHashResult to get it working right. > > I'm afraid I don't understand - this declaration should go into the IL > file > before recompiling the wrapper library? >
This would be C# wrapper definition for COM method. If you know COM class GUID, it will not be necessary to compile wrapper library, you can define your wrapper interfaces for COM interfaces you want to use in your C# code. For example: [ComVisible(false), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("E561901F-03A5-4afe-86D0-72BAEECE7004")] public interface IVssProviderNotifications { [PreserveSig()] uint OnLoad([MarshalAs(UnmanagedType.IUnknown)] object pCallback); [PreserveSig()] uint OnUnload(int bForceUnload); } the rest of the code (how to use your interfaces, create and instantiate COM objects) you can find here: http://www.codeguru.com/csharp/csharp/cs_misc/com/article.php/c9065__2
Don't see what you're looking for? Try a search.
|