private void WriteHandle(string key, IntPtr handler) { System.IO.MemoryMappedFiles.MemoryMappedFile mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(key, 1024000); using (System.IO.MemoryMappedFiles.MemoryMappedViewStream stream = mmf.CreateViewStream()) { using (System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor()) { accessor.Write(0, ref handler); } } }
void RefreshMmf() { while (true) { Thread.Sleep(5); try { if (_rawFormat?.ToString() != _fileFormat) { _rawFormat = new StringBuilder(_fileFormat ?? ""); } Regex varPattern = new Regex(@"\$\{[^,\{\}]*(:[\w\.]*)?\}"); // Regex boolPattern = new Regex(@"\$if\{[^\{\}]*[\w><=!&\|\+\-\*/\(\)]*,[^{}]*,[^{}]*\}"); MatchCollection varMatches = varPattern.Matches(_rawFormat.ToString()); MatchCollection boolMatches = boolPattern.Matches(_rawFormat.ToString()); if (varMatches.Count > 0) { foreach (Match match in varMatches) { try { _matchedValue = match.Value; VariableExpression expression = new VariableExpression(_matchedValue, _ortdpWrapper); _replaceStr = ExpressionTools.CalcRpnExpression( ExpressionTools.ConvertToRpnExpression(expression.NoFormatExpression), _ortdpWrapper); if (expression.HasFormat) { if (double.TryParse(_replaceStr, out double val)) { _replaceStr = val.ToString(expression.Format); } } if (_rawFormat.ToString().Contains("\n") || _rawFormat.ToString().Contains("\r")) { _rawFormat.Replace("\n", "\n"); _rawFormat.Replace("\r", "\r"); } _rawFormat.Replace(match.Value, _replaceStr); } catch (NullReferenceException) { } catch (FormatException) { _rawFormat.Append("Invalid Format"); } catch (ArgumentOutOfRangeException) { } _rawFormat.Append("\0\0"); } } if (boolMatches.Count > 0) { foreach (Match match in boolMatches) { try { _matchedValue = match.Value; IfExpression expression = new IfExpression(_matchedValue, _ortdpWrapper); _replaceStr = expression.GetProcessedValue().ToString(); if (_rawFormat.ToString().Contains("\n") || _rawFormat.ToString().Contains("\r")) { _rawFormat.Replace("\n", "\n"); _rawFormat.Replace("\r", "\r"); } _rawFormat.Replace(match.Value, _replaceStr); } catch (NullReferenceException) { _rawFormat.Append(""); } _rawFormat.Append("\0\0"); } } } catch (Exception x) { System.IO.File.AppendAllText("Ex.txt", x.ToString()); } byte[] bytes = Encoding.GetEncoding(Setting.Encoding).GetBytes(_rawFormat.ToString() + "\0\0"); _outputMapStream = _outputInfoMap.CreateViewStream(); _outputMapStream.Write(bytes, 0, bytes.Length); } }
public override Stream GetRawData(int Offset, int Count = -1) { //TODO: TEST return(MMF.CreateViewStream(Offset, Count)); }
internal bool Run(string uuid, string mode) { bool run = false; XcDiagProcessInfo xcdiagProcessInfo = GetXcDiagProcessInfo(mode); if (xcdiagProcessInfo != null) // If got xcdiag process info { this.log.Info("Received xcdiag request"); xcdiagProcessInfo.processStartInfo.CreateNoWindow = true; xcdiagProcessInfo.processStartInfo.ErrorDialog = false; xcdiagProcessInfo.processStartInfo.RedirectStandardError = true; xcdiagProcessInfo.processStartInfo.RedirectStandardOutput = true; xcdiagProcessInfo.processStartInfo.UseShellExecute = false; xcdiagProcessInfo.processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process xcdiagProcess = null; try { xcdiagProcess = System.Diagnostics.Process.Start(xcdiagProcessInfo.processStartInfo); if (xcdiagProcess == null) { log.Info("xcdiag process already running in: \"{0}\"", xcdiagProcessInfo.processStartInfo.FileName); } } catch (System.IO.FileNotFoundException filenotfoundEx) { log.Exception("Cannot launch xcdiag (file not found)", filenotfoundEx); } catch (System.ObjectDisposedException objdisposedEx) { log.Exception("Cannot launch xcdiag (object disposed)", objdisposedEx); } catch (System.InvalidOperationException invalidopEx) { log.Exception("Cannot launch xcdiag (invalid operation)", invalidopEx); } catch (System.ComponentModel.Win32Exception win32Ex) { log.Exception("Cannot launch xcdiag (win32 error)", win32Ex); } catch (System.Exception ex) { log.Exception("Cannot launch xcdiag", ex); } if (xcdiagProcess != null) // If launched xcdiag { // Tends to block until process finishes. string xcdiagError = xcdiagProcess.StandardError.ReadToEnd(); if (!string.IsNullOrEmpty(xcdiagError)) { log.Error("xcdiag produced error output: {0}", xcdiagError); } xcdiagProcess.WaitForExit(); // Oooooh. if (xcdiagProcess.ExitCode != 0) // If xcdiag reported an error { log.Error("xcdiag exited with error code '{0}'", xcdiagProcess.ExitCode); } // Ends if xcdiag reported an error else // else xcdiag ran ok { System.Text.StringBuilder dataBuffer = null; // Make a big ol' string containing the data, encoding each byte as 2-digit ASCII hex. #if MEMORYMAPFILE_SUPPORTED using (System.IO.MemoryMappedFiles.MemoryMappedFile memmapXcDiag = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(xcdiagProcessInfo.xcDiagOutputPath)) #else // !MEMORYMAPFILE_SUPPORTED //// Create a memory mapped view of the zip file //hFile = CreateFileA(szXcDiagOut, // GENERIC_READ | GENERIC_WRITE, // 0, // NULL, // OPEN_EXISTING, // FILE_ATTRIBUTE_NORMAL, // NULL); using (Microsoft.Win32.SafeHandles.SafeFileHandle mapHandle = wcf.Native.FileFunctions.CreateFile(xcdiagProcessInfo.xcDiagOutputPath , global::XenClientGuestService.Native.EFileAccess.FILE_GENERIC_READ | global::XenClientGuestService.Native.EFileAccess.FILE_GENERIC_WRITE , System.IO.FileShare.None , IntPtr.Zero , System.IO.FileMode.Open , System.IO.FileAttributes.Normal , IntPtr.Zero)) #endif // MEMORYMAPFILE_SUPPORTED { // Finish creating the native mapping, and then work out how to read bytes... #if MEMORYMAPFILE_SUPPORTED using (System.IO.MemoryMappedFiles.MemoryMappedViewStream streamXcDiag = memmapXcDiag.CreateViewStream()) #else // !MEMORYMAPFILE_SUPPORTED long fileSize = new System.IO.FileInfo(xcdiagProcessInfo.xcDiagOutputPath).Length; //hMapFile = CreateFileMapping( // hFile, // current file handle // NULL, // default security // PAGE_READWRITE, // read/write permission // 0, // size of mapping object, high // filesize, // size of mapping object, low // NULL); // name of mapping object using (Microsoft.Win32.SafeHandles.SafeFileHandle xcDiagOutputMappingHandle = wcf.Native.MappingFunctions.CreateFileMapping(mapHandle , IntPtr.Zero , global::XenClientGuestService.Native.FileMapProtection.PageReadWrite , 0 , checked ((uint)fileSize) , null)) #endif // MEMORYMAPFILE_SUPPORTED { #if MEMORYMAPFILE_SUPPORTED long fileSize = streamXcDiag.Length; #else // !MEMORYMAPFILE_SUPPORTED //// Map the view //lpMapAddress = (unsigned char *)MapViewOfFile( // hMapFile, // handle to mapping object // FILE_MAP_ALL_ACCESS, // read/write // 0, // high-order 32 bits of file offset // 0, // low-order 32 bits of file offset // filesize); // number of bytes to map using (wcf.Native.FileMappingViewHandle mappingViewHandler = wcf.Native.MappingFunctions.MapViewOfFile(xcDiagOutputMappingHandle , global::XenClientGuestService.Native.FileMapAccess.FileMapAllAccess , 0 , 0 , (checked ((UInt32)fileSize)))) #endif // !MEMORYMAPFILE_SUPPORTED { #if MEMORYMAPFILE_SUPPORTED System.IO.BinaryReader reader = new System.IO.BinaryReader(streamXcDiag); #else // !MEMORYMAPFILE_SUPPORTED wcf.Native.FileMappingViewHandle.ViewReader reader = mappingViewHandler.CreateViewReader(); #endif // !MEMORYMAPFILE_SUPPORTED int resultSize = checked ((int)(fileSize * 2)); dataBuffer = new StringBuilder(resultSize, resultSize); for (int byteIter = 0; byteIter < fileSize; ++byteIter) { byte b = reader.ReadByte(); dataBuffer.Append(b.ToString("x2")); } // Ends loop over bytes } // Ends using map view handle } // Ends using Memory Map view } // Ends using Memory Map file string data = dataBuffer.ToString(); byte[] stringBytes = System.Text.Encoding.Default.GetBytes(data); byte[] asciiBytes = System.Text.ASCIIEncoding.Convert(Encoding.Default, Encoding.ASCII, stringBytes); data = System.Text.Encoding.ASCII.GetString(asciiBytes); Gather(uuid, data); } // Ends else xcdiag ran ok } // Ends if launched xcdiag } // Ends if got xcdiag process info return(run); }
public static string ZZReadMappedMemoryData() { string Data = string.Empty; try { using (System.IO.MemoryMappedFiles.MemoryMappedFile mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting("NICSQLToolsUpdateMap")) { System.Threading.Mutex mutex = System.Threading.Mutex.OpenExisting("NICSQLToolsUpdateMapMutex"); using (System.IO.MemoryMappedFiles.MemoryMappedViewStream stream = mmf.CreateViewStream()) { System.IO.BinaryReader reader = new System.IO.BinaryReader(stream); Data = reader.ReadString(); } mutex.WaitOne(); } if (Data == string.Empty) { System.Diagnostics.Process.GetCurrentProcess().Kill(); } Properties.Settings.Default["DatabaseConnectionString"] = Data.Split(Convert.ToChar("||"))[0]; } catch (System.IO.FileNotFoundException) { Console.WriteLine("Memory-mapped file does not exist. Run Process A first."); } return(Data.Split(Convert.ToChar("||"))[1]); }