From 15d501d37512925131cef56c20485bae6d698d4c Mon Sep 17 00:00:00 2001 From: maniglia Date: Thu, 28 Nov 2024 17:27:17 +0100 Subject: [PATCH] Fix for char* --- .../Native/ProgrammerApi.cs | 140 ++++++++++++++---- 1 file changed, 115 insertions(+), 25 deletions(-) diff --git a/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs index bc52e2e..b5e3c3c 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs @@ -4,6 +4,7 @@ namespace SharpCubeProgrammer.Native { using System; using System.Runtime.InteropServices; + using System.Text; using Enum; using Struct; @@ -318,12 +319,12 @@ internal static int GetDfuDeviceList(ref IntPtr dfuList, int iPID, int iVID) #region [ConnectDfuBootloader] [DllImport(ProgrammerDll32, EntryPoint = "ConnectDfuBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int ConnectDfuBootloader32(string usbIndex); + private static extern int ConnectDfuBootloader32(IntPtr usbIndex); [DllImport(ProgrammerDll64, EntryPoint = "ConnectDfuBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int ConnectDfuBootloader64(string usbIndex); + private static extern int ConnectDfuBootloader64(IntPtr usbIndex); - private static int ConnectDfuBootloaderNative(string usbIndex) + private static int ConnectDfuBootloaderNative(IntPtr usbIndex) { return !Environment.Is64BitProcess ? ConnectDfuBootloader32(usbIndex) @@ -332,9 +333,20 @@ private static int ConnectDfuBootloaderNative(string usbIndex) internal static int ConnectDfuBootloader(string usbIndex) { + var usbIndexPtr = IntPtr.Zero; + try { - return ConnectDfuBootloaderNative(usbIndex); + usbIndexPtr = Marshal.StringToHGlobalAnsi(usbIndex); + return ConnectDfuBootloaderNative(usbIndexPtr); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -344,6 +356,10 @@ internal static int ConnectDfuBootloader(string usbIndex) { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(usbIndexPtr); + } } #endregion @@ -926,12 +942,12 @@ internal static int Execute(uint address) #region [MassErase] [DllImport(ProgrammerDll32, EntryPoint = "MassErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int MassErase32(string sFlashMemName); + private static extern int MassErase32(IntPtr sFlashMemName); [DllImport(ProgrammerDll64, EntryPoint = "MassErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int MassErase64(string sFlashMemName); + private static extern int MassErase64(IntPtr sFlashMemName); - private static int MassEraseNative(string sFlashMemName) + private static int MassEraseNative(IntPtr sFlashMemName) { return !Environment.Is64BitProcess ? MassErase32(sFlashMemName) @@ -940,9 +956,20 @@ private static int MassEraseNative(string sFlashMemName) internal static int MassErase(string sFlashMemName) { + var sFlashMemNamePtr = IntPtr.Zero; + try { - return MassEraseNative(sFlashMemName); + sFlashMemNamePtr = Marshal.StringToHGlobalAnsi(sFlashMemName); + return MassEraseNative(sFlashMemNamePtr); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -952,6 +979,10 @@ internal static int MassErase(string sFlashMemName) { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(sFlashMemNamePtr); + } } #endregion @@ -959,12 +990,12 @@ internal static int MassErase(string sFlashMemName) #region [SectorErase] [DllImport(ProgrammerDll32, EntryPoint = "SectorErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int SectorErase32(uint[] sectors, uint sectorNbr, string sFlashMemName); + private static extern int SectorErase32(uint[] sectors, uint sectorNbr, IntPtr sFlashMemName); [DllImport(ProgrammerDll64, EntryPoint = "SectorErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int SectorErase64(uint[] sectors, uint sectorNbr, string sFlashMemName); + private static extern int SectorErase64(uint[] sectors, uint sectorNbr, IntPtr sFlashMemName); - private static int SectorEraseNative(uint[] sectors, uint sectorNbr, string sFlashMemName) + private static int SectorEraseNative(uint[] sectors, uint sectorNbr, IntPtr sFlashMemName) { return !Environment.Is64BitProcess ? SectorErase32(sectors, sectorNbr, sFlashMemName) @@ -973,9 +1004,20 @@ private static int SectorEraseNative(uint[] sectors, uint sectorNbr, string sFla internal static int SectorErase(uint[] sectors, uint sectorNbr, string sFlashMemName) { + var sFlashMemNamePtr = IntPtr.Zero; + try { - return SectorEraseNative(sectors, sectorNbr, sFlashMemName); + sFlashMemNamePtr = Marshal.StringToHGlobalAnsi(sFlashMemName); + return SectorEraseNative(sectors, sectorNbr, sFlashMemNamePtr); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -985,6 +1027,10 @@ internal static int SectorErase(uint[] sectors, uint sectorNbr, string sFlashMem { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(sFlashMemNamePtr); + } } #endregion @@ -1469,12 +1515,12 @@ internal static void DeleteInterfaceList() #region [AutomaticMode] [DllImport(ProgrammerDll32, EntryPoint = "AutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern void AutomaticMode32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run); + private static extern void AutomaticMode32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run); [DllImport(ProgrammerDll64, EntryPoint = "AutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern void AutomaticMode64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run); + private static extern void AutomaticMode64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run); - private static void AutomaticModeNative(string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run) + private static void AutomaticModeNative(string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run) { if (!Environment.Is64BitProcess) { @@ -1488,9 +1534,19 @@ private static void AutomaticModeNative(string filePath, uint address, uint skip internal static void AutomaticMode(string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run) { + var obCommandPtr = IntPtr.Zero; try { - AutomaticModeNative(filePath, address, skipErase, verify, isMassErase, obCommand, run); + obCommandPtr = Marshal.StringToHGlobalAnsi(obCommand); + AutomaticModeNative(filePath, address, skipErase, verify, isMassErase, obCommandPtr, run); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -1500,6 +1556,10 @@ internal static void AutomaticMode(string filePath, uint address, uint skipErase { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(obCommandPtr); + } } #endregion @@ -1507,12 +1567,12 @@ internal static void AutomaticMode(string filePath, uint address, uint skipErase #region [SerialNumberingAutomaticMode] [DllImport(ProgrammerDll32, EntryPoint = "SerialNumberingAutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern void SerialNumberingAutomaticMode32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData); + private static extern void SerialNumberingAutomaticMode32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData); [DllImport(ProgrammerDll64, EntryPoint = "SerialNumberingAutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern void SerialNumberingAutomaticMode64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData); + private static extern void SerialNumberingAutomaticMode64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData); - private static void SerialNumberingAutomaticModeNative(string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData) + private static void SerialNumberingAutomaticModeNative(string filePath, uint address, uint skipErase, uint verify, int isMassErase, IntPtr obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData) { if (!Environment.Is64BitProcess) { @@ -1526,9 +1586,20 @@ private static void SerialNumberingAutomaticModeNative(string filePath, uint add internal static void SerialNumberingAutomaticMode(string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run, int enableSerialNumbering, int serialAddress, int serialSize, string serialInitialData) { + var obCommandPtr = IntPtr.Zero; + try { - SerialNumberingAutomaticModeNative(filePath, address, skipErase, verify, isMassErase, obCommand, run, enableSerialNumbering, serialAddress, serialSize, serialInitialData); + obCommandPtr = Marshal.StringToHGlobalAnsi(obCommand); + SerialNumberingAutomaticModeNative(filePath, address, skipErase, verify, isMassErase, obCommandPtr, run, enableSerialNumbering, serialAddress, serialSize, serialInitialData); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -1538,6 +1609,10 @@ internal static void SerialNumberingAutomaticMode(string filePath, uint address, { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(obCommandPtr); + } } #endregion @@ -1582,13 +1657,13 @@ internal static int GetStorageStructure(ref IntPtr deviceStorageStruct) #region [SendOptionBytesCmd] [DllImport(ProgrammerDll32, EntryPoint = "SendOptionBytesCmd", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int SendOptionBytesCmd32(string command); + private static extern int SendOptionBytesCmd32(IntPtr command); [DllImport(ProgrammerDll64, EntryPoint = "SendOptionBytesCmd", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int SendOptionBytesCmd64(string command); + private static extern int SendOptionBytesCmd64(IntPtr command); - private static int SendOptionBytesCmdNative(string command) - { + private static int SendOptionBytesCmdNative(IntPtr command) + { return !Environment.Is64BitProcess ? SendOptionBytesCmd32(command) : SendOptionBytesCmd64(command); @@ -1596,9 +1671,20 @@ private static int SendOptionBytesCmdNative(string command) internal static int SendOptionBytesCmd(string command) { + var commandPtr = IntPtr.Zero; + try { - return SendOptionBytesCmdNative(command); + commandPtr = Marshal.StringToHGlobalAnsi(command); + return SendOptionBytesCmdNative(commandPtr); + } + catch (OutOfMemoryException ex) + { + throw new Exception("K-Society CubeProgrammer out of memory exception.", ex); + } + catch (ArgumentOutOfRangeException ex) + { + throw new Exception("K-Society CubeProgrammer argument out of range exception.", ex); } catch (DllNotFoundException ex) { @@ -1608,6 +1694,10 @@ internal static int SendOptionBytesCmd(string command) { throw new Exception("K-Society CubeProgrammer operation not found.", ex); } + finally + { + Marshal.FreeHGlobal(commandPtr); + } } #endregion