本文整理汇总了C#中SafeFileHandle.DangerousGetHandle方法的典型用法代码示例。如果您正苦于以下问题:C# SafeFileHandle.DangerousGetHandle方法的具体用法?C# SafeFileHandle.DangerousGetHandle怎么用?C# SafeFileHandle.DangerousGetHandle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SafeFileHandle
的用法示例。
在下文中一共展示了SafeFileHandle.DangerousGetHandle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Poll
/// <summary>
/// Polls a File Descriptor for the passed in flags.
/// </summary>
/// <param name="fd">The descriptor to poll</param>
/// <param name="events">The events to poll for</param>
/// <param name="timeout">The amount of time to wait; -1 for infinite, 0 for immediate return, and a positive number is the number of milliseconds</param>
/// <param name="triggered">The events that were returned by the poll call. May be PollEvents.POLLNONE in the case of a timeout.</param>
/// <returns>An error or Error.SUCCESS.</returns>
internal static unsafe Error Poll(SafeFileHandle fd, PollEvents events, int timeout, out PollEvents triggered)
{
bool gotRef = false;
try
{
fd.DangerousAddRef(ref gotRef);
var pollEvent = new PollEvent
{
FileDescriptor = fd.DangerousGetHandle().ToInt32(),
Events = events,
};
uint unused;
Error err = Poll(&pollEvent, 1, timeout, &unused);
triggered = pollEvent.TriggeredEvents;
return err;
}
finally
{
if (gotRef)
{
fd.DangerousRelease();
}
}
}
示例2: PrintScoresMatrix
public void PrintScoresMatrix(SafeFileHandle handle)
{
if (this.nativeDesignScorer != IntPtr.Zero)
{
NadirHelper.PrintScoresMatrix(this.nativeDesignScorer, handle.DangerousGetHandle());
}
}
示例3: CreateFileMapping
public static IntPtr CreateFileMapping(SafeFileHandle handle,
FileMapProtection flProtect, long ddMaxSize, string lpName)
{
var Hi = (int) (ddMaxSize/int.MaxValue);
var Lo = (int) (ddMaxSize%int.MaxValue);
return CreateFileMapping(handle.DangerousGetHandle(), IntPtr.Zero, flProtect, Hi, Lo, lpName);
}
示例4: FSync
private static void FSync(SafeFileHandle handle)
{
#if !__MonoCS__ && !USE_UNIX_IO
WinNative.FlushFileBuffers(handle);
#else
Syscall.fsync(handle.DangerousGetHandle().ToInt32());
#endif
}
示例5: Module
internal Module(ForeignPtr @base, SafeFileHandle handle)
{
Contract.Assert(@base.Address> 0);
[email protected] = @base;
this.handle = handle;
if (!handle.IsInvalid)
this.imagePath = Kernel32.GetFinalPathNameByHandle(handle.DangerousGetHandle(), 0);
}
示例6: CreateConsole
public static bool CreateConsole() {
if (AllocConsole()) {
//vs debugger may redirect console out into debug out, so we need to redirect it back
if (!hConOut.IsInvalid) {
hConOut.Dispose();
}
hConOut = CreateFile("CONOUT$", 0x40000000, FileShare.Write, /*null*/ IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (!hConOut.IsInvalid) {
SetStdHandle(-11, hConOut.DangerousGetHandle());
}
var cw = new StreamWriter(Console.OpenStandardOutput());
cw.AutoFlush = true;
Console.SetOut(cw);
return true;
}
return false;
}
示例7: Main
static void Main(string[] args)
{
const string pipeName = @"\\.\pipe\SamplePipe";
const int inputBufferSize = 4096;
const int outputBufferSize = 4096;
IntPtr hPipe = NativeMethods.CreateNamedPipe(
pipeName,
NativeMethods.PIPE_ACCESS_DUPLEX, // read/write access
NativeMethods.PIPE_TYPE_BYTE | // message type pipe
NativeMethods.PIPE_READMODE_BYTE | // message-read mode
NativeMethods.PIPE_WAIT, // blocking mode
NativeMethods.PIPE_UNLIMITED_INSTANCES, // max. instances
outputBufferSize, // output buffer size
inputBufferSize, // input buffer size
0, // default client time-out for WaitNamePipe Method
IntPtr.Zero); // no security attribute
SafeFileHandle safeHandle = new SafeFileHandle(hPipe, true);
if (safeHandle.IsInvalid)
throw new IOException("Could not open pipe " + pipeName + ".");
Console.WriteLine("Waiting for client to connect.");
bool connected = NativeMethods.ConnectNamedPipe(hPipe, IntPtr.Zero) ||
Marshal.GetLastWin32Error() == NativeMethods.ERROR_PIPE_CONNECTED;
if (!connected)
throw new Win32Exception(Marshal.GetLastWin32Error());
Console.WriteLine("Connected to client.");
Stream stream = new FileStream(safeHandle, FileAccess.ReadWrite, 4096);
try
{
byte[] bytes = Encoding.UTF8.GetBytes("Hello World\n");
while (true)
{
stream.Write(bytes, 0, bytes.Length);
stream.Flush();//important to send immediately
Thread.Sleep(1000);
}
}
finally
{
NativeMethods.DisconnectNamedPipe(safeHandle.DangerousGetHandle()); //proper disconnect before closing handle
stream.Dispose();
}
}
示例8: SetFileSize
public static void SetFileSize(SafeFileHandle handle, long count)
{
#if !__MonoCS__ && !USE_UNIX_IO
var low = (int)(count & 0xffffffff);
var high = (int)(count >> 32);
WinNative.SetFilePointer(handle, low, out high, WinNative.EMoveMethod.Begin);
if (!WinNative.SetEndOfFile(handle))
{
throw new Win32Exception();
}
#else
int r;
do {
r = Syscall.ftruncate (handle.DangerousGetHandle().ToInt32(), count);
} while (UnixMarshal.ShouldRetrySyscall (r));
UnixMarshal.ThrowExceptionForLastErrorIf (r);
#endif
FSync(handle);
}
示例9: GetDeviceAttributes
private static HidDeviceAttributes GetDeviceAttributes(SafeFileHandle hidHandle)
{
var deviceAttributes = default(NativeMethods.HIDD_ATTRIBUTES);
deviceAttributes.Size = Marshal.SizeOf(deviceAttributes);
NativeMethods.HidD_GetAttributes(hidHandle.DangerousGetHandle(), ref deviceAttributes);
return new HidDeviceAttributes(deviceAttributes);
}
示例10: FileStream
public FileStream (SafeFileHandle handle, FileAccess access,
int bufferSize, bool isAsync)
:this (handle.DangerousGetHandle (), access, false, bufferSize, isAsync)
{
this.safeHandle = handle;
}
示例11: CreateFileMapping
internal static SafeMemoryMappedFileHandle CreateFileMapping(SafeFileHandle hFile, SECURITY_ATTRIBUTES lpAttributes, int fProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName)
{
var handle = new SafeMemoryMappedFileHandle(hFile.DangerousGetHandle (), false);
return handle;
}
示例12: FindEv3
internal void FindEv3()
{
int index = 0;
bool found = false;
Guid guid;
// get the GUID of the HID class
HidImports.HidD_GetHidGuid(out guid);
// get a handle to all devices that are part of the HID class
IntPtr hDevInfo = HidImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HidImports.DIGCF_DEVICEINTERFACE | HidImports.DIGCF_PRESENT);
// create a new interface data struct and initialize its size
HidImports.SP_DEVICE_INTERFACE_DATA diData = new HidImports.SP_DEVICE_INTERFACE_DATA();
diData.cbSize = Marshal.SizeOf(diData);
// get a device interface to a single device (enumerate all devices)
while(HidImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
{
UInt32 size;
// get the buffer size for this device detail instance (returned in the size parameter)
HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);
// create a detail struct and set its size
HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
// yeah, yeah...well, see, on Win x86, cbSize must be 5 for some reason. On x64, apparently 8 is what it wants.
// someday I should figure this out. Thanks to Paul Miller on this...
diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);
// actually get the detail struct
if(HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
{
Debug.WriteLine("{0}: {1} - {2}", index, diDetail.DevicePath, Marshal.GetLastWin32Error());
// open a read/write handle to our device using the DevicePath returned
_handle = HidImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HidImports.EFileAttributes.Overlapped, IntPtr.Zero);
// create an attributes struct and initialize the size
HidImports.HIDD_ATTRIBUTES attrib = new HidImports.HIDD_ATTRIBUTES();
attrib.Size = Marshal.SizeOf(attrib);
// get the attributes of the current device
if(HidImports.HidD_GetAttributes(_handle.DangerousGetHandle(), ref attrib))
{
// if the vendor and product IDs match up
if(attrib.VendorID == VID && attrib.ProductID == PID)
{
// it's a Ev3
Debug.WriteLine("Found one!");
found = true;
IntPtr preparsedData;
if(!HidImports.HidD_GetPreparsedData(_handle.DangerousGetHandle(), out preparsedData))
throw new Exception("Could not get preparsed data for HID device");
HidImports.HIDP_CAPS caps;
if(HidImports.HidP_GetCaps(preparsedData, out caps) != HidImports.HIDP_STATUS_SUCCESS)
throw new Exception("Could not get CAPS for HID device");
HidImports.HidD_FreePreparsedData(ref preparsedData);
_inputReport = new byte[caps.InputReportByteLength];
_outputReport = new byte[caps.OutputReportByteLength];
// create a nice .NET FileStream wrapping the handle above
_stream = new FileStream(_handle, FileAccess.ReadWrite, _inputReport.Length, true);
break;
}
_handle.Close();
}
}
else
{
// failed to get the detail struct
throw new Exception("SetupDiGetDeviceInterfaceDetail failed on index " + index);
}
// move to the next device
index++;
}
// clean up our list
HidImports.SetupDiDestroyDeviceInfoList(hDevInfo);
// if we didn't find a EV3, throw an exception
if(!found)
throw new Exception("No LEGO EV3s found in HID device list.");
}
示例13: InternalGetTarget
private static string InternalGetTarget(SafeFileHandle handle)
{
int outBufferSize = Marshal.SizeOf(typeof(REPARSE_DATA_BUFFER));
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
try
{
int bytesReturned;
bool result = DeviceIoControl(handle.DangerousGetHandle(), FsctlGetReparsePoint, IntPtr.Zero, 0, outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);
if (!result)
{
int error = Marshal.GetLastWin32Error();
if (error == ErrorNotAReparsePoint)
{
return null;
}
ThrowLastWin32Error(Strings.UnableToGetJunctionInformation);
}
REPARSE_DATA_BUFFER reparseDataBuffer = (REPARSE_DATA_BUFFER)
Marshal.PtrToStructure(outBuffer, typeof(REPARSE_DATA_BUFFER));
if (reparseDataBuffer.ReparseTag != IoReparseTagMountPoint)
{
return null;
}
string targetDir = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer, reparseDataBuffer.SubstituteNameOffset, reparseDataBuffer.SubstituteNameLength);
if (targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.Ordinal))
{
targetDir = targetDir.Substring(NonInterpretedPathPrefix.Length);
}
return targetDir;
}
finally
{
Marshal.FreeHGlobal(outBuffer);
}
}
示例14: FindWiimote
internal static void FindWiimote(WiimoteFoundDelegate wiimoteFound)
{
int index = 0;
bool found = false;
Guid guid;
SafeFileHandle mHandle;
// get the GUID of the HID class
HIDImports.HidD_GetHidGuid(out guid);
// get a handle to all devices that are part of the HID class
// Fun fact: DIGCF_PRESENT worked on my machine just fine. I reinstalled Vista, and now it no longer finds the Wiimote with that parameter enabled...
IntPtr hDevInfo = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HIDImports.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);
// create a new interface data struct and initialize its size
HIDImports.SP_DEVICE_INTERFACE_DATA diData = new HIDImports.SP_DEVICE_INTERFACE_DATA();
diData.cbSize = Marshal.SizeOf(diData);
// get a device interface to a single device (enumerate all devices)
while(HIDImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
{
UInt32 size;
// get the buffer size for this device detail instance (returned in the size parameter)
HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);
// create a detail struct and set its size
HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
// yeah, yeah...well, see, on Win x86, cbSize must be 5 for some reason. On x64, apparently 8 is what it wants.
// someday I should figure this out. Thanks to Paul Miller on this...
diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);
// actually get the detail struct
if(HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
{
Debug.WriteLine(string.Format("{0}: {1} - {2}", index, diDetail.DevicePath, Marshal.GetLastWin32Error()));
// open a read/write handle to our device using the DevicePath returned
mHandle = HIDImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);
// create an attributes struct and initialize the size
HIDImports.HIDD_ATTRIBUTES attrib = new HIDImports.HIDD_ATTRIBUTES();
attrib.Size = Marshal.SizeOf(attrib);
// get the attributes of the current device
if(HIDImports.HidD_GetAttributes(mHandle.DangerousGetHandle(), ref attrib))
{
// if the vendor and product IDs match up
if(attrib.VendorID == VID && attrib.ProductID == PID)
{
// it's a Wiimote
Debug.WriteLine("Found one!");
found = true;
// fire the callback function...if the callee doesn't care about more Wiimotes, break out
if(!wiimoteFound(diDetail.DevicePath))
break;
}
}
mHandle.Close();
}
else
{
// failed to get the detail struct
throw new WiimoteException("SetupDiGetDeviceInterfaceDetail failed on index " + index);
}
// move to the next device
index++;
}
// clean up our list
HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo);
// if we didn't find a Wiimote, throw an exception
if(!found)
throw new WiimoteNotFoundException("No Wiimotes found in HID device list.");
}
示例15: Connect
/// <summary>
/// Connect to a Wiimote paired to the PC via Bluetooth
/// </summary>
public void Connect()
{
int index = 0;
bool found = false;
Guid guid;
// get the GUID of the HID class
HIDImports.HidD_GetHidGuid(out guid);
// get a handle to all devices that are part of the HID class
// Fun fact: DIGCF_PRESENT worked on my machine just fine. I reinstalled Vista, and now it no longer finds the Wiimote with that parameter enabled...
IntPtr hDevInfo = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HIDImports.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);
// create a new interface data struct and initialize its size
HIDImports.SP_DEVICE_INTERFACE_DATA diData = new HIDImports.SP_DEVICE_INTERFACE_DATA();
diData.cbSize = Marshal.SizeOf(diData);
// get a device interface to a single device (enumerate all devices)
while(HIDImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
{
UInt32 size;
// get the buffer size for this device detail instance (returned in the size parameter)
HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);
// create a detail struct and set its size
HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
// yeah, yeah...well, see, on Win x86, cbSize must be 5 for some reason. On x64, apparently 8 is what it wants.
// someday I should figure this out. Thanks to Paul Miller on this...
diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);
// actually get the detail struct
if(HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
{
Debug.WriteLine(index + " " + diDetail.DevicePath + " " + Marshal.GetLastWin32Error());
// open a read/write handle to our device using the DevicePath returned
mHandle = HIDImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);
// create an attributes struct and initialize the size
HIDImports.HIDD_ATTRIBUTES attrib = new HIDImports.HIDD_ATTRIBUTES();
attrib.Size = Marshal.SizeOf(attrib);
// get the attributes of the current device
if(HIDImports.HidD_GetAttributes(mHandle.DangerousGetHandle(), ref attrib))
{
// if the vendor and product IDs match up
if(attrib.VendorID == VID && attrib.ProductID == PID)
{
Debug.WriteLine("Found it!");
found = true;
// create a nice .NET FileStream wrapping the handle above
mStream = new FileStream(mHandle, FileAccess.ReadWrite, REPORT_LENGTH, true);
// start an async read operation on it
BeginAsyncRead();
// read the calibration info from the controller
try
{
ReadCalibration();
}
catch
{
// if we fail above, try the alternate HID writes
mAltWriteMethod = true;
ReadCalibration();
}
// force a status check to get the state of any extensions plugged in at startup
GetStatus();
break;
}
else
{
// otherwise this isn't the controller, so close up the file handle
mHandle.Close();
}
}
}
else
{
// failed to get the detail struct
throw new WiimoteException("SetupDiGetDeviceInterfaceDetail failed on index " + index);
}
// move to the next device
index++;
}
// clean up our list
HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo);
// if we didn't find a Wiimote, throw an exception
//.........这里部分代码省略.........