public InputMapper() { bool exists = false; if (_readwriteMutex == null) { _readwriteMutex = new System.Threading.Mutex(false, @"F3DTouchMutex", out exists); } _touchInfo = new ClientWinTouchInfo(); }
public ClientWinTouchInfo GetProTouchInfo() { try { _readwriteMutex.WaitOne(); int structLength = Marshal.SizeOf(_touchInfo); IntPtr temp_mapHandle = OpenFileMapping(FILE_MAP_READ, 0, "F3DTouchShared"); if (temp_mapHandle == IntPtr.Zero) { _readwriteMutex.ReleaseMutex(); return(_touchInfo); } // 映射对象的一个视图,得到指向共享内存的指针,获取里面的数据 IntPtr pBuf = MapViewOfFile(temp_mapHandle, FILE_MAP_READ, 0, 0, (uint)structLength); if (pBuf == IntPtr.Zero) { _readwriteMutex.ReleaseMutex(); return(_touchInfo); } IntPtr structPointer = Marshal.AllocHGlobal(structLength); Marshal.StructureToPtr(_touchInfo, structPointer, true); CopyMemory(structPointer, pBuf, structLength); _touchInfo = (ClientWinTouchInfo)Marshal.PtrToStructure(structPointer, typeof(ClientWinTouchInfo)); CloseHandle(temp_mapHandle); _readwriteMutex.ReleaseMutex(); } catch (System.Exception e) { _readwriteMutex.ReleaseMutex(); UnityEngine.Debug.LogError("InputMapper.GetProTouchInfo error" + e); throw; } return(_touchInfo); }