Question by
wannesMonchy · Oct 09, 2017 at 12:45 PM ·
c#scripting problem.netpython.net-assemblies
sHandle is Invalid
Hi all!
I'm trying to establish a connection between python and unity, using c# & a memory mapped file (mmf). The python project uses the mmap import & writes it on "Interface.bin".
To interpret the changes happening in Interface.bin I followed the following example:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public class PlaneController: MonoBehaviour {
//Shared Memory
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern SafeFileHandle OpenFileMapping(
uint dwDesiredAccess,
bool bInheritHandle,
string lpName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr MapViewOfFile(
SafeFileHandle hFileMappingObject,
UInt32 dwDesiredAccess,
UInt32 dwFileOffsetHigh,
UInt32 dwFileOffsetLow,
UIntPtr dwNumberOfBytesToMap);
/*
[DllImport("kernel32.dll", SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
*/
string szMapName = "UnityFileMappingObject";
const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
const UInt32 SECTION_QUERY = 0x0001;
const UInt32 SECTION_MAP_WRITE = 0x0002;
const UInt32 SECTION_MAP_READ = 0x0004;
const UInt32 SECTION_MAP_EXECUTE = 0x0008;
const UInt32 SECTION_EXTEND_SIZE = 0x0010;
const UInt32 SECTION_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
SECTION_MAP_WRITE |
SECTION_MAP_READ |
SECTION_MAP_EXECUTE |
SECTION_EXTEND_SIZE);
const UInt32 FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;
private SafeFileHandle sHandle;
private IntPtr hHandle;
private IntPtr pBuffer;
private int sharedInputCount;
bool attachSuccessful;
//
int count;
int X0;
int Y0;
int X1 = -1;
int Y1 = -1;
int SQ0 = -1;
int SQ1 = -1;
void Start (){
sHandle = new SafeFileHandle(hHandle, true);
sharedInputCount = 0;
attachSuccessful = Attach(szMapName, 256);
}
unsafe public bool Attach(string SharedMemoryName, UInt32 NumBytes)
{
if (!sHandle.IsInvalid) return false;
sHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, SharedMemoryName);
Debug.Log("Shared mem open: ");
if (sHandle.IsInvalid) return false;
Debug.Log("Shared mem open SUCCESS: ");
pBuffer = MapViewOfFile(sHandle, FILE_MAP_ALL_ACCESS, 0, 0, new UIntPtr(NumBytes));
Debug.Log("Shared mem mapped: ");
return true;
}
unsafe public void Detach()
{
if (!sHandle.IsInvalid && !sHandle.IsClosed )
{
//CloseHandle(hHandle); //fair to leak if can't close
sHandle.Close();
}
pBuffer = IntPtr.Zero;
//lBufferSize = 0;
}
void Update()
{
if (!attachSuccessful)
{
attachSuccessful = Attach(szMapName, 256);
return;
}
}
void OnApplicationQuit()
{
if (attachSuccessful)
{
Detach();
}
}
void FixedUpdate ()
{
//get Shared memory Input
if (!attachSuccessful)
{
return;
}
count = Marshal.ReadInt32(pBuffer, 0);
X0 = Marshal.ReadInt32(pBuffer, 4);
Y0 = Marshal.ReadInt32(pBuffer, 8);
X1 = Marshal.ReadInt32(pBuffer, 12);
Y1 = Marshal.ReadInt32(pBuffer, 16);
SQ0 = Marshal.ReadInt32(pBuffer, 20);
SQ1 = Marshal.ReadInt32(pBuffer, 24);
}
}
(as seen on: this unity3D forum page)
In unity I get the "trying to attach" & "Shared mem open" but afterwards the sHandle seems to be invalid. My project settings have already been set to .NET 2.0 (instead of the subset)
The python files, as well as the Interface.bin are situated inside the scripts folder. After inspecting the mmap with a windows process tool I can verify it exists.
Any idea as to why my sHandle is invalid?
Comment