internal static extern void NdrMesTypeDecode2( IntPtr Handle, ref MIDL_TYPE_PICKLING_INFO pPicklingInfo, ref MIDL_STUB_DESC pStubDesc, [MarshalAs(UnmanagedType.LPArray)] byte[] pFormatString, ref IntPtr pObject);
protected void InitializeStub(Guid interfaceID, byte[] MIDL_ProcFormatString, byte[] MIDL_TypeFormatString, string pipe, ushort MajorVerson = 1, ushort MinorVersion = 0) { this.MIDL_ProcFormatString = MIDL_ProcFormatString; this.MIDL_TypeFormatString = MIDL_TypeFormatString; PipeName = pipe; procString = GCHandle.Alloc(this.MIDL_ProcFormatString, GCHandleType.Pinned); RPC_CLIENT_INTERFACE clientinterfaceObject = new RPC_CLIENT_INTERFACE(interfaceID, MajorVerson, MinorVersion); GENERIC_BINDING_ROUTINE_PAIR bindingObject = new GENERIC_BINDING_ROUTINE_PAIR(); // important: keep a reference to avoid CallbakcOnCollectedDelegate Exception BindDelegate = Bind; UnbindDelegate = Unbind; bindingObject.Bind = Marshal.GetFunctionPointerForDelegate((bind)BindDelegate); bindingObject.Unbind = Marshal.GetFunctionPointerForDelegate((unbind)UnbindDelegate); faultoffsets = GCHandle.Alloc(new COMM_FAULT_OFFSETS() { CommOffset = -1, FaultOffset = -1 }, GCHandleType.Pinned); clientinterface = GCHandle.Alloc(clientinterfaceObject, GCHandleType.Pinned); formatString = GCHandle.Alloc(MIDL_TypeFormatString, GCHandleType.Pinned); bindinghandle = GCHandle.Alloc(bindingObject, GCHandleType.Pinned); MIDL_STUB_DESC stubObject = new MIDL_STUB_DESC(formatString.AddrOfPinnedObject(), clientinterface.AddrOfPinnedObject(), Marshal.GetFunctionPointerForDelegate(AllocateMemoryDelegate), Marshal.GetFunctionPointerForDelegate(FreeMemoryDelegate), bindinghandle.AddrOfPinnedObject()); rpcClientInterface = stubObject.RpcInterfaceInformation; stub = GCHandle.Alloc(stubObject, GCHandleType.Pinned); }
private static IntPtr GetStubPtr() { if (!stub.IsAllocated) { procString = GCHandle.Alloc(ms2Dpar__MIDL_ProcFormatString, GCHandleType.Pinned); COMM_FAULT_OFFSETS commFaultOffset = new COMM_FAULT_OFFSETS { CommOffset = -1, FaultOffset = -1 }; faultoffsets = GCHandle.Alloc(commFaultOffset, GCHandleType.Pinned); formatString = GCHandle.Alloc(ms2Dpar__MIDL_TypeFormatString, GCHandleType.Pinned); allocMemoryFunctionDelegate = AllocateMemory; freeMemoryFunctionDelegate = FreeMemory; IntPtr pAllocMemory = Marshal.GetFunctionPointerForDelegate(allocMemoryFunctionDelegate); IntPtr pFreeMemory = Marshal.GetFunctionPointerForDelegate(freeMemoryFunctionDelegate); stringHandleBindFunctionDelegate = StringHandleBind; stringHandleUnBindFunctionDelegate = StringHandleUnBind; IntPtr pStringHandleBind = Marshal.GetFunctionPointerForDelegate(stringHandleBindFunctionDelegate); IntPtr pStringHandleUnBind = Marshal.GetFunctionPointerForDelegate(stringHandleUnBindFunctionDelegate); GENERIC_BINDING_ROUTINE_PAIR rp = new GENERIC_BINDING_ROUTINE_PAIR(); rp.Bind = pStringHandleBind; rp.Unbind = pStringHandleUnBind; genericRuotinePair = GCHandle.Alloc(rp, GCHandleType.Pinned); hLogon = IntPtr.Zero; MIDL_STUB_DESC stubObject = new MIDL_STUB_DESC { RpcInterfaceInformation = GetClientInterface(), pfnAllocate = pAllocMemory, pfnFree = pFreeMemory, pAutoBindHandle = hLogon, apfnNdrRundownRoutines = IntPtr.Zero, aGenericBindingRoutinePairs = genericRuotinePair.AddrOfPinnedObject(), apfnExprEval = IntPtr.Zero, aXmitQuintuple = IntPtr.Zero, pFormatTypes = formatString.AddrOfPinnedObject(), fCheckBounds = 1, Version = 0x60000, pMallocFreeStruct = IntPtr.Zero, MIDLVersion = 0x8000253, CommFaultOffsets = IntPtr.Zero, aUserMarshalQuadruple = IntPtr.Zero, NotifyRoutineTable = IntPtr.Zero, mFlags = new IntPtr(0x00000001), CsRoutineTables = IntPtr.Zero, ProxyServerInfo = IntPtr.Zero, pExprInfo = IntPtr.Zero, }; stub = GCHandle.Alloc(stubObject, GCHandleType.Pinned); } return(stub.AddrOfPinnedObject()); }