示例#1
0
        internal static IntPtr Alloc(uint size)
        {
            IntPtr ptr = NativeMethods.LocalAlloc(LPTR, size);

            RpcTrace.Verbose("{0} = LocalAlloc({1})", ptr, size);
            return(ptr);
        }
示例#2
0
        public void Dispose(bool disposing)
        {
            try
            {
                RpcTrace.Verbose("RpcHandle.Dispose on {0}", Handle);

                if (Handle != IntPtr.Zero)
                {
                    DisposeHandle(ref Handle);
                }
                lock (_pinnedAddresses)
                {
                    for (int i = _pinnedAddresses.Count - 1; i >= 0; i--)
                    {
                        _pinnedAddresses[i].Dispose();
                    }
                    _pinnedAddresses.Clear();
                }
            }
            finally
            {
                Handle = IntPtr.Zero;
            }
            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
示例#3
0
 internal static void Free(IntPtr ptr)
 {
     if (ptr != IntPtr.Zero)
     {
         RpcTrace.Verbose("LocalFree({0})", ptr);
         NativeMethods.LocalFree(ptr);
     }
 }
 /// <summary>
 /// Sends a message as an array of bytes and retrieves the response from the server, if
 /// AuthenticateAs() has not been called, the client will authenticate as Anonymous.
 /// </summary>
 public byte[] Execute(byte[] input)
 {
     if (!_authenticated)
     {
         RpcTrace.Warning("AuthenticateAs was not called, assuming Anonymous.");
         AuthenticateAs(Anonymous);
     }
     RpcTrace.Verbose("ExplicitBytesExecute(byte[{0}])", input.Length);
     return(InvokeRpc(_handle, IID, input));
 }
示例#5
0
 public ExplicitBytesServer(Guid iid, int maxCalls, int maxRequestBytes, bool allowAnonymousCallbacks)
 {
     IID = iid;
     RpcTrace.Verbose("ServerRegisterInterface({0})", iid);
     // Guid.Empty to avoid registration of any interface allowing access to AddProtocol/AddAuthentication
     if (Guid.Empty != iid)
     {
         Ptr <RPC_SERVER_INTERFACE> sIf = ServerInterfaceFactory.Create(_handle, iid, RpcRuntime.TYPE_FORMAT, RpcRuntime.FUNC_FORMAT,
                                                                        RpcEntryPoint);
         this.serverRegisterInterface(sIf, maxCalls, maxRequestBytes, allowAnonymousCallbacks);
     }
 }
示例#6
0
        static RpcRuntime()
        {
            Is64BitProcess = (IntPtr.Size == 8);
            RpcTrace.Verbose("Is64BitProcess = {0}", Is64BitProcess);

            if (Is64BitProcess)
            {
                //Same as 32-bit except: [8] = 8; [32] = 24;
                TYPE_FORMAT = new byte[39]
                {
                    0x00, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x28, 0x00, 0x08, 0x00,
                    0x01, 0x00, 0x01, 0x5b, 0x11, 0x0c, 0x08, 0x5c, 0x11, 0x14,
                    0x02, 0x00, 0x12, 0x00, 0x02, 0x00, 0x1b, 0x00, 0x01, 0x00,
                    0x28, 0x54, 0x18, 0x00, 0x01, 0x00, 0x01, 0x5b, 0x00
                };
                //Very different from 32-bit:
                FUNC_FORMAT = new byte[61]
                {
                    0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00,
                    0x32, 0x00, 0x00, 0x00, 0x08, 0x00, 0x24, 0x00, 0x47, 0x05,
                    0x0a, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x48, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b, 0x00, 0x10, 0x00,
                    0x02, 0x00, 0x50, 0x21, 0x18, 0x00, 0x08, 0x00, 0x13, 0x20,
                    0x20, 0x00, 0x12, 0x00, 0x70, 0x00, 0x28, 0x00, 0x10, 0x00,
                    0x00
                };
            }
            else
            {
                TYPE_FORMAT = new byte[39]
                {
                    0x00, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x28, 0x00, 0x04, 0x00,
                    0x01, 0x00, 0x01, 0x5b, 0x11, 0x0c, 0x08, 0x5c, 0x11, 0x14,
                    0x02, 0x00, 0x12, 0x00, 0x02, 0x00, 0x1b, 0x00, 0x01, 0x00,
                    0x28, 0x54, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x5b, 0x00
                };
                FUNC_FORMAT = new byte[59]
                {
                    0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00,
                    0x32, 0x00, 0x00, 0x00, 0x08, 0x00, 0x24, 0x00, 0x47, 0x05,
                    0x08, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x48, 0x00,
                    0x04, 0x00, 0x08, 0x00, 0x0b, 0x00, 0x08, 0x00, 0x02, 0x00,
                    0x50, 0x21, 0x0c, 0x00, 0x08, 0x00, 0x13, 0x20, 0x10, 0x00,
                    0x12, 0x00, 0x70, 0x00, 0x14, 0x00, 0x10, 0x00, 0x00
                };
            }
            FUNC_FORMAT_PTR = new Ptr <byte[]>(FUNC_FORMAT);
        }
        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);
        }