public unsafe static uint Request( SafeFileHandle hPort, uint tenantId, uint flowId, string accountName, SecurityIdentifier sid, string shareName) { uint HRESULT = 0; uint lpBytesReturned = 0; UnicodeEncoding enc = new UnicodeEncoding(); OKTOPUS_CREATE_RAP_REQUEST Request = new OKTOPUS_CREATE_RAP_REQUEST(); OKTOPUS_CREATE_RAP_REPLY Reply = new OKTOPUS_CREATE_RAP_REPLY(); if (accountName.Length + 1 > OKTO_VM_NAME_MAX_CHARS) { throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_VM_NAME_TOO_LONG)); } if (shareName.Length + 1 > OKTO_SHARE_NAME_MAX_CHARS) { throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_SHARE_NAME_TOO_LONG)); } if (sid.BinaryLength > OKTO_SID_BUFF_BYTE_LEN) { throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_VM_SID_TOO_LONG)); } // // Format request buffer. // Request.OpCode = (ulong)IOFLOWUSER_OPCODE.OpCodeCreateRap; Request.TenantId = tenantId; Request.FlowId = flowId; Request.MaxStatsArraySize = MAX_STATS_ARRAY_SIZE; Request.Result = 0; Request.VmNameWCharLen = ((uint)enc.GetByteCount(accountName) / 2) + 1; byte[] encVmNameBuff = new byte[OKTO_VM_NAME_BUFF_WCZ_MAX * 2]; enc.GetBytes(accountName, 0, accountName.Length, encVmNameBuff, 0); for (int i = 0; i < OKTO_VM_NAME_BUFF_WCZ_MAX * 2; i++) { Request.VmNameBuff[i] = encVmNameBuff[i]; } Request.ShareNameWCharZLen = ((uint)enc.GetByteCount(shareName) / 2) + 1; byte[] encShareNameBuff = new byte[OKTO_SHARE_NAME_BUFF_WCZ_MAX * 2]; enc.GetBytes(shareName, 0, shareName.Length, encShareNameBuff, 0); for (int i = 0; i < OKTO_SHARE_NAME_BUFF_WCZ_MAX * 2; i++) { Request.ShareNameBuff[i] = encShareNameBuff[i]; } Request.SidLength = (uint)sid.BinaryLength; byte[] sidBuff = new byte[sid.BinaryLength]; sid.GetBinaryForm(sidBuff, 0); for (int i = 0; i < sid.BinaryLength; i++) { Request.Sid[i] = sidBuff[i]; } Request.PID = 0; Request.i_index = 0; Request.j_index = 0; Reply.Result = 0; // Pin buffers for duration of synchronous call to FilterSendMessage. GCHandle gchRequest = GCHandle.Alloc(Request, GCHandleType.Pinned); GCHandle gchReply = GCHandle.Alloc(Reply, GCHandleType.Pinned); // FilterSendMessage() ref http://msdn.microsoft.com/en-us/library/windows/hardware/ff541513(v=vs.85).aspx HRESULT = FilterSendMessage(hPort, // HANDLE hPort, gchRequest.AddrOfPinnedObject(), // LPVOID lpInBuffer, (uint)Marshal.SizeOf(Request), // DWORD dwInBufferSize, gchReply.AddrOfPinnedObject(), // LPVOID lpOutBuffer, (uint)Marshal.SizeOf(Reply), // DWORD dwOutBufferSize, out lpBytesReturned); // LPDWORD lpBytesReturned if (HRESULT != S_OK) { Marshal.ThrowExceptionForHR((int)HRESULT); } if (Reply.Result != (uint)RESULTCODES.OKTO_RESULT_SUCCESS) { throw new ExceptionIoFlow(DecodeOktoResult(Reply.Result)); } gchRequest.Free(); gchReply.Free(); return(HRESULT); }
const int OKTO_VM_NAME_MAX_CHARS = (OKTO_VM_NAME_BUFF_WCZ_MAX - 1); // Allow NULL terminator. #endregion Fields #region Methods public static unsafe uint Request( SafeFileHandle hPort, uint tenantId, uint flowId, string accountName, SecurityIdentifier sid, string shareName) { uint HRESULT = 0; uint lpBytesReturned = 0; UnicodeEncoding enc = new UnicodeEncoding(); OKTOPUS_CREATE_RAP_REQUEST Request = new OKTOPUS_CREATE_RAP_REQUEST(); OKTOPUS_CREATE_RAP_REPLY Reply = new OKTOPUS_CREATE_RAP_REPLY(); if (accountName.Length + 1 > OKTO_VM_NAME_MAX_CHARS) throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_VM_NAME_TOO_LONG)); if (shareName.Length + 1 > OKTO_SHARE_NAME_MAX_CHARS) throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_SHARE_NAME_TOO_LONG)); if (sid.BinaryLength > OKTO_SID_BUFF_BYTE_LEN) throw new ExceptionIoFlow(DecodeOktoResult((uint)RESULTCODES.OKTO_VM_SID_TOO_LONG)); // // Format request buffer. // Request.OpCode = (ulong)IOFLOWUSER_OPCODE.OpCodeCreateRap; Request.TenantId = tenantId; Request.FlowId = flowId; Request.MaxStatsArraySize = MAX_STATS_ARRAY_SIZE; Request.Result = 0; Request.VmNameWCharLen = ((uint)enc.GetByteCount(accountName) / 2) + 1; byte[] encVmNameBuff = new byte[OKTO_VM_NAME_BUFF_WCZ_MAX * 2]; enc.GetBytes(accountName, 0, accountName.Length, encVmNameBuff, 0); for (int i = 0; i < OKTO_VM_NAME_BUFF_WCZ_MAX * 2; i++) Request.VmNameBuff[i] = encVmNameBuff[i]; Request.ShareNameWCharZLen = ((uint)enc.GetByteCount(shareName) / 2) + 1; byte[] encShareNameBuff = new byte[OKTO_SHARE_NAME_BUFF_WCZ_MAX * 2]; enc.GetBytes(shareName, 0, shareName.Length, encShareNameBuff, 0); for (int i = 0; i < OKTO_SHARE_NAME_BUFF_WCZ_MAX * 2; i++) Request.ShareNameBuff[i] = encShareNameBuff[i]; Request.SidLength = (uint)sid.BinaryLength; byte[] sidBuff = new byte[sid.BinaryLength]; sid.GetBinaryForm(sidBuff, 0); for (int i = 0; i < sid.BinaryLength; i++) Request.Sid[i] = sidBuff[i]; Request.PID = 0; Request.i_index = 0; Request.j_index = 0; Reply.Result = 0; // Pin buffers for duration of synchronous call to FilterSendMessage. GCHandle gchRequest = GCHandle.Alloc(Request, GCHandleType.Pinned); GCHandle gchReply = GCHandle.Alloc(Reply, GCHandleType.Pinned); // FilterSendMessage() ref http://msdn.microsoft.com/en-us/library/windows/hardware/ff541513(v=vs.85).aspx HRESULT = FilterSendMessage(hPort, // HANDLE hPort, gchRequest.AddrOfPinnedObject(), // LPVOID lpInBuffer, (uint)Marshal.SizeOf(Request), // DWORD dwInBufferSize, gchReply.AddrOfPinnedObject(), // LPVOID lpOutBuffer, (uint)Marshal.SizeOf(Reply), // DWORD dwOutBufferSize, out lpBytesReturned); // LPDWORD lpBytesReturned if (HRESULT != S_OK) { Marshal.ThrowExceptionForHR((int)HRESULT); } if (Reply.Result != (uint)RESULTCODES.OKTO_RESULT_SUCCESS) { throw new ExceptionIoFlow(DecodeOktoResult(Reply.Result)); } gchRequest.Free(); gchReply.Free(); return HRESULT; }