Skip to content

Commit

Permalink
Merge pull request #351 from MediaPortal/MP1-5229-MiniDisplayLibrary_…
Browse files Browse the repository at this point in the history
…Fix_x64_compatibilty_for_HID_VFD_Control

MP1-5229: MiniDisplayLibrary: Fix x64 compatibilty for HID VFD Control
  • Loading branch information
andrewjswan authored Dec 2, 2024
2 parents 54fab88 + b60880e commit d844b9a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ internal static bool FindDeviceFromGuid(Guid myGuid, ref string[] devicePathName
// no more devices are available.

// The cbSize element of the MyDeviceInterfaceData structure must be set to
// the structure's size in bytes. The size is 28 bytes.
MyDeviceInterfaceData.cbSize = 28;
//Marshal.SizeOf(MyDeviceInterfaceData);
// the structure's size in bytes.
MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData);

// ***
// API function:
Expand Down Expand Up @@ -184,7 +183,11 @@ internal static bool FindDeviceFromGuid(Guid myGuid, ref string[] devicePathName

if (Result == 0)
{
LastDevice = true;
int iLastError = Marshal.GetLastWin32Error();
if (iLastError == DeviceManagementApiDeclarations.ERROR_NO_MORE_ITEMS)
LastDevice = true;
else
throw new System.ComponentModel.Win32Exception(iLastError);
}
else
{
Expand Down Expand Up @@ -234,7 +237,7 @@ internal static bool FindDeviceFromGuid(Guid myGuid, ref string[] devicePathName
IntPtr DetailDataBuffer = Marshal.AllocHGlobal(BufferSize);

// Store cbSize in the first 4 bytes of the array
Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize);
Marshal.WriteInt32(DetailDataBuffer, IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 8);
Debug.WriteLine("cbsize = " + MyDeviceInterfaceDetailData.cbSize);

// Call SetupDiGetDeviceInterfaceDetail again.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal sealed class DeviceManagementApiDeclarations
public const short DIGCF_PRESENT = 0x00000002;
public const short DIGCF_DEVICEINTERFACE = 0x00000010;

public const int ERROR_NO_MORE_ITEMS = 259;

// ******************************************************************************
// Structures and classes for API calls, listed alphabetically
// ******************************************************************************
Expand All @@ -59,31 +61,31 @@ internal sealed class DeviceManagementApiDeclarations
// API functions, listed alphabetically
// ******************************************************************************

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, int Flags);

[DllImport("setupapi.dll")]
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int SetupDiCreateDeviceInfoList(ref Guid ClassGuid, int hwndParent);

[DllImport("setupapi.dll")]
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll")]
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData,
ref Guid InterfaceClassGuid, int MemberIndex,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, string Enumerator, int hwndParent, int Flags);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
IntPtr DeviceInterfaceDetailData,
int DeviceInterfaceDetailDataSize, ref int RequiredSize,
IntPtr DeviceInfoData);

[DllImport("user32.dll")]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterDeviceNotification(IntPtr Handle);

[StructLayout(LayoutKind.Sequential)]
Expand All @@ -110,10 +112,11 @@ public class DEV_BROADCAST_DEVICEINTERFACE_1
public int dbcc_devicetype;
public int dbcc_reserved;

[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 16)] public byte[]
dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 16)]
public byte[] dbcc_classguid;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)] public char[] dbcc_name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
public char[] dbcc_name;

#endregion
}
Expand All @@ -126,9 +129,12 @@ public class DEV_BROADCAST_HANDLE
public int dbch_size;
public int dbch_devicetype;
public int dbch_reserved;
public int dbch_handle;
public int dbch_hdevnotify;

public IntPtr dbch_handle;
public IntPtr dbch_hdevnotify;
public Guid dbch_eventguid;
public int dbch_nameoffset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1, ArraySubType = UnmanagedType.I1)]
public byte[] dbch_data;
#endregion
}

Expand All @@ -152,7 +158,7 @@ public struct SP_DEVICE_INTERFACE_DATA
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public int Reserved;
public IntPtr Reserved;

#endregion
}
Expand All @@ -176,7 +182,7 @@ public struct SP_DEVINFO_DATA
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public int Reserved;
public IntPtr Reserved;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#endregion

using System;
using System.Runtime.InteropServices;

namespace MediaPortal.ProcessPlugins.MiniDisplayPlugin.VFD_Control
Expand All @@ -35,7 +36,7 @@ internal class FileIOApiDeclarations
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
public const int INVALID_HANDLE_VALUE = -1;
public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public const short OPEN_EXISTING = 3;
public const int WAIT_TIMEOUT = 0x102;
public const short WAIT_OBJECT_0 = 0;
Expand All @@ -48,56 +49,60 @@ internal class FileIOApiDeclarations
// API functions, listed alphabetically
// ******************************************************************************

[DllImport("kernel32.dll")]
public static extern int CancelIo(int hFile);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int CancelIo(IntPtr hFile);

[DllImport("kernel32.dll")]
public static extern int CloseHandle(int hObject);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int CreateEvent(ref SECURITY_ATTRIBUTES SecurityAttributes, int bManualReset, int bInitialState,
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateEvent(ref SECURITY_ATTRIBUTES SecurityAttributes, int bManualReset, int bInitialState,
string lpName);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr
CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition, uint dwFlagsAndAttributes, int hTemplateFile);
int dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

[DllImport("kernel32.dll")]
public static extern int ReadFile(int hFile, ref byte lpBuffer, int nNumberOfBytesToRead,
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int ReadFile(IntPtr hFile, ref byte lpBuffer, int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped);

[DllImport("kernel32.dll")]
public static extern int WaitForSingleObject(int hHandle, int dwMilliseconds);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);

[DllImport("kernel32.dll")]
public static extern int WriteFile(int hFile, ref byte lpBuffer, int nNumberOfBytesToWrite,
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int WriteFile(IntPtr hFile, ref byte lpBuffer, int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten, int lpOverlapped);

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Explicit, Size = 20)]
public struct OVERLAPPED
{
#region Fields
[FieldOffset(0)]
public uint Internal;

[FieldOffset(4)]
public uint InternalHigh;

[FieldOffset(8)]
public uint Offset;

public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
[FieldOffset(12)]
public uint OffsetHigh;

#endregion
[FieldOffset(8)]
public IntPtr Pointer;

[FieldOffset(16)]
public IntPtr hEvent;
}

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
#region Fields

public int nLength;
public int lpSecurityDescriptor;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;

#endregion
}
}
}
Loading

0 comments on commit d844b9a

Please sign in to comment.