Skip to content

Commit

Permalink
Merge pull request #167 from K-Society/experimental
Browse files Browse the repository at this point in the history
Fix for char*
  • Loading branch information
maniglia authored Nov 28, 2024
2 parents 2ab2bc2 + 15d501d commit a29028a
Showing 1 changed file with 115 additions and 25 deletions.
140 changes: 115 additions & 25 deletions src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SharpCubeProgrammer.Native
{
using System;
using System.Runtime.InteropServices;
using System.Text;
using Enum;
using Struct;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -952,19 +979,23 @@ internal static int MassErase(string sFlashMemName)
{
throw new Exception("K-Society CubeProgrammer operation not found.", ex);
}
finally
{
Marshal.FreeHGlobal(sFlashMemNamePtr);
}
}

#endregion

#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)
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -1500,19 +1556,23 @@ 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

#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)
{
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -1582,23 +1657,34 @@ 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);
}

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)
{
Expand All @@ -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
Expand Down

0 comments on commit a29028a

Please sign in to comment.