private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr <MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int szResponse = 0; IntPtr response, result; using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input)) { if (RpcApi.Is64BitProcess) { try { result = NdrClientCall2x64(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } } else { using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[10])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof(int) * 6); pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof(int) * 8); pStack32.Data[5] = 0; //reserved pStack32.Data[6] = 0; //output: int dwSizeResponse pStack32.Data[8] = 0; //output: byte* lpResponse try { result = NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } szResponse = pStack32.Data[6]; response = new IntPtr(pStack32.Data[8]); } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); Log.Verbose("InvokeRpc.InvokeRpc response on {0}, recieved {1} bytes", handle.Handle, szResponse); byte[] output = new byte[szResponse]; if (szResponse > 0 && response != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcApi.Free(response); return(output); }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr <MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int szResponse = 0; var pszResponse = new Ptr <int>(szResponse); IntPtr response = IntPtr.Zero; var pResponse = new Ptr <IntPtr>(response); IntPtr result; using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input)) { if (RpcApi.Is64BitProcess) { try { result = RpcApi.NdrClientCall2x64( pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response ); } catch (SEHException ex) { int internalError = ex.ErrorCode; if ((uint)RpcError.RPC_E_FAIL == (uint)internalError) { if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode()) { internalError = Marshal.GetExceptionCode(); } } RpcException.Assert(internalError); throw; } } else { using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[6])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pszResponse.Handle.ToInt32(); pStack32.Data[4] = pResponse.Handle.ToInt32(); pStack32.Data[5] = 0; // reserved try { result = RpcApi.NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { int internalError = ex.ErrorCode; if ((uint)RpcError.RPC_E_FAIL == (uint)internalError) { if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode()) { internalError = Marshal.GetExceptionCode(); } } RpcException.Assert(internalError); throw; } } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); szResponse = pszResponse.Data; response = pResponse.Data; byte[] output = new byte[szResponse]; if (szResponse > 0 && pResponse.Handle != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcApi.Free(response); return(output); }
private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback) { Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr <MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC( handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int cbOutput = 0; var pcbOutput = new Ptr <int>(cbOutput); IntPtr result = IntPtr.Zero; IntPtr output = IntPtr.Zero; var pOutput = new Ptr <IntPtr>(output); using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input)) { AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) => { RPC_ASYNC_STATE async = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE)); var myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback)); RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result); if (RpcError.RPC_S_OK == hr) { if (RpcError.RPC_S_OK == (RpcError)result.ToInt32()) { ExecuteAsyncResponse response; try { cbOutput = pcbOutput.Data; byte[] _output = new byte[cbOutput]; Marshal.Copy(pOutput.Data, _output, 0, cbOutput); response.result = RpcError.RPC_S_OK; response.response = _output; } catch (Exception e) { response.result = RpcError.RPC_E_FAIL; response.response = null; } finally { RpcApi.Free(pOutput.Data); } myCallback(response); } else { ExecuteAsyncResponse response; response.result = (RpcError)result.ToInt32(); response.response = null; myCallback(response); } } else { ExecuteAsyncResponse response; response.result = hr; response.response = null; myCallback(response); } }; var pCallbackRoutine = new FunctionPtr <AsyncCallback>(cbRountine); RPC_ASYNC_STATE asyncState = new RPC_ASYNC_STATE(); var _pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState); RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE))); asyncState.Event = _pAsyncState.Data.Event; asyncState.Flags = _pAsyncState.Data.Flags; asyncState.Lock = _pAsyncState.Data.Lock; asyncState.RuntimeInfo = _pAsyncState.Data.RuntimeInfo; asyncState.Signature = _pAsyncState.Data.Signature; asyncState.Size = _pAsyncState.Data.Size; asyncState.StubInfo = _pAsyncState.Data.StubInfo; asyncState.NotificationType = RpcNotificationTypes.RpcNotificationTypeCallback; asyncState.u.NotificationRoutine = pCallbackRoutine.Handle; var ptrCB = Marshal.GetFunctionPointerForDelegate(callback); asyncState.UserInfo = ptrCB; var pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState); if (RpcApi.Is64BitProcess) { try { result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out cbOutput, out output); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } } else { using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[7])) { pStack32.Data[0] = pAsyncState.Handle.ToInt32(); pStack32.Data[1] = handle.Handle.ToInt32(); pStack32.Data[2] = input.Length; pStack32.Data[3] = pInputBuffer.Handle.ToInt32(); pStack32.Data[4] = pcbOutput.Handle.ToInt32(); pStack32.Data[5] = pOutput.Handle.ToInt32(); pStack32.Data[6] = 0; //reserved try { result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { Log.Verbose("exception on {0}", ex); RpcException.Assert(ex.ErrorCode); throw; } } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle); }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr<MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int szResponse = 0; IntPtr response, result; using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input)) { if (RpcApi.Is64BitProcess) { try { result = NdrClientCall2x64(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } } else { using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[10])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof (int)*6); pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof (int)*8); pStack32.Data[5] = 0; //reserved pStack32.Data[6] = 0; //output: int dwSizeResponse pStack32.Data[8] = 0; //output: byte* lpResponse try { result = NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } szResponse = pStack32.Data[6]; response = new IntPtr(pStack32.Data[8]); } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); Log.Verbose("InvokeRpc.InvokeRpc response on {0}, recieved {1} bytes", handle.Handle, szResponse); byte[] output = new byte[szResponse]; if (szResponse > 0 && response != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcApi.Free(response); return output; }
private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback) { Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr<MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC( handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int cbOutput = 0; var pcbOutput = new Ptr<int>(cbOutput); IntPtr result = IntPtr.Zero; IntPtr output = IntPtr.Zero; var pOutput = new Ptr<IntPtr>(output); using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input)) { AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) => { RPC_ASYNC_STATE async = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE)); var myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback)); RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result); if (RpcError.RPC_S_OK == hr) { if (RpcError.RPC_S_OK == (RpcError) result.ToInt32()) { ExecuteAsyncResponse response; try { cbOutput = pcbOutput.Data; byte[] _output = new byte[cbOutput]; Marshal.Copy(pOutput.Data, _output, 0, cbOutput); response.result = RpcError.RPC_S_OK; response.response = _output; } catch (Exception e) { response.result = RpcError.RPC_E_FAIL; response.response = null; } finally { RpcApi.Free(pOutput.Data); } myCallback(response); } else { ExecuteAsyncResponse response; response.result = (RpcError)result.ToInt32(); response.response = null; myCallback(response); } } else { ExecuteAsyncResponse response; response.result = hr; response.response = null; myCallback(response); } }; var pCallbackRoutine = new FunctionPtr<AsyncCallback>(cbRountine); RPC_ASYNC_STATE asyncState = new RPC_ASYNC_STATE(); var _pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState); RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE))); asyncState.Event = _pAsyncState.Data.Event; asyncState.Flags = _pAsyncState.Data.Flags; asyncState.Lock = _pAsyncState.Data.Lock; asyncState.RuntimeInfo = _pAsyncState.Data.RuntimeInfo; asyncState.Signature = _pAsyncState.Data.Signature; asyncState.Size = _pAsyncState.Data.Size; asyncState.StubInfo = _pAsyncState.Data.StubInfo; asyncState.NotificationType = RpcNotificationTypes.RpcNotificationTypeCallback; asyncState.u.NotificationRoutine = pCallbackRoutine.Handle; var ptrCB = Marshal.GetFunctionPointerForDelegate(callback); asyncState.UserInfo = ptrCB; var pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState); if (RpcApi.Is64BitProcess) { try { result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out cbOutput, out output); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } } else { using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[7])) { pStack32.Data[0] = pAsyncState.Handle.ToInt32(); pStack32.Data[1] = handle.Handle.ToInt32(); pStack32.Data[2] = input.Length; pStack32.Data[3] = pInputBuffer.Handle.ToInt32(); pStack32.Data[4] = pcbOutput.Handle.ToInt32(); pStack32.Data[5] = pOutput.Handle.ToInt32(); pStack32.Data[6] = 0; //reserved try { result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { Log.Verbose("exception on {0}", ex); RpcException.Assert(ex.ErrorCode); throw; } } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle); }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr<MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int szResponse = 0; var pszResponse = new Ptr<int>(szResponse); IntPtr response = IntPtr.Zero; var pResponse = new Ptr<IntPtr>(response); IntPtr result; using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input)) { if (RpcApi.Is64BitProcess) { try { result = RpcApi.NdrClientCall2x64( pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response ); } catch (SEHException ex) { int internalError = ex.ErrorCode; if ((uint)RpcError.RPC_E_FAIL == (uint)internalError) { if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode()) { internalError = Marshal.GetExceptionCode(); } } RpcException.Assert(internalError); throw; } } else { using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[6])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pszResponse.Handle.ToInt32(); pStack32.Data[4] = pResponse.Handle.ToInt32(); pStack32.Data[5] = 0; // reserved try { result = RpcApi.NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { int internalError = ex.ErrorCode; if ((uint)RpcError.RPC_E_FAIL == (uint)internalError) { if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode()) { internalError = Marshal.GetExceptionCode(); } } RpcException.Assert(internalError); throw; } } } GC.KeepAlive(pInputBuffer); } RpcException.Assert(result.ToInt32()); szResponse = pszResponse.Data; response = pResponse.Data; byte[] output = new byte[szResponse]; if (szResponse > 0 && pResponse.Handle != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcApi.Free(response); return output; }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { RpcTrace.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr <MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(ClientInterfaceFactory.CreatExplicitBytesClient(iid)), RpcRuntime.TYPE_FORMAT, false)); } int szResponse = 0; IntPtr response, result; using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input)) { if (RpcRuntime.Is64BitProcess) { try { result = NativeMethods.NdrClientCall2x64(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response); } catch (SEHException ex) { RpcTrace.Error(ex); Guard.Assert(ex.ErrorCode); throw; } } else { using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[10])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof(int) * 6); pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof(int) * 8); pStack32.Data[5] = 0; //reserved pStack32.Data[6] = 0; //output: int dwSizeResponse pStack32.Data[8] = 0; //output: byte* lpResponse try { result = NativeMethods.NdrClientCall2x86(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { RpcTrace.Error(ex); Guard.Assert(ex.ErrorCode); throw; } szResponse = pStack32.Data[6]; response = new IntPtr(pStack32.Data[8]); } } GC.KeepAlive(pInputBuffer); } // on local machine // if 32 bit server just throws Exception in server side handler // then 32 bits client can see "-2147467259" debug view of pointer result // then 64 bits client can see "2147500037" debug view of pointer result // then 32 bits client can do ToInt32 and things work fine // then 64 bits client can do ToInt32 and get arithmetic overflow exception // hence using ToInt64 for 64 bits client if (IntPtr.Size == 8) { Guard.Assert(result.ToInt64()); } else { Guard.Assert(result.ToInt32()); } RpcTrace.Verbose("InvokeRpc.InvokeRpc response on {0}, received {1} bytes", handle.Handle, szResponse); byte[] output = new byte[szResponse]; if (szResponse > 0 && response != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcRuntime.Free(response); return(output); }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { RpcTrace.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr<MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(ClientInterfaceFactory.CreatExplicitBytesClient(iid)), RpcRuntime.TYPE_FORMAT, false)); } int szResponse = 0; IntPtr response, result; using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input)) { if (RpcRuntime.Is64BitProcess) { try { result = NativeMethods.NdrClientCall2x64(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response); } catch (SEHException ex) { RpcTrace.Error(ex); Guard.Assert(ex.ErrorCode); throw; } } else { using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[10])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof (int)*6); pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof (int)*8); pStack32.Data[5] = 0; //reserved pStack32.Data[6] = 0; //output: int dwSizeResponse pStack32.Data[8] = 0; //output: byte* lpResponse try { result = NativeMethods.NdrClientCall2x86(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { RpcTrace.Error(ex); Guard.Assert(ex.ErrorCode); throw; } szResponse = pStack32.Data[6]; response = new IntPtr(pStack32.Data[8]); } } GC.KeepAlive(pInputBuffer); } // on local machine // if 32 bit server just throws Exception in server side handler // then 32 bits client can see "-2147467259" debug view of pointer result // then 64 bits client can see "2147500037" debug view of pointer result // then 32 bits client can do ToInt32 and things work fine // then 64 bits client can do ToInt32 and get arithmetic overflow exception // hence using ToInt64 for 64 bits client if (IntPtr.Size == 8) { Guard.Assert(result.ToInt64()); } else { Guard.Assert(result.ToInt32()); } RpcTrace.Verbose("InvokeRpc.InvokeRpc response on {0}, received {1} bytes", handle.Handle, szResponse); byte[] output = new byte[szResponse]; if (szResponse > 0 && response != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcRuntime.Free(response); return output; }