Advertisement
BaSs_HaXoR

[4.3]PS3Lib.CCAPI.cs SOURCE

Sep 16th, 2014
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.22 KB | None | 0 0
  1. // ************************************************* //
  2. //    --- Copyright (c) 2014 iMCS Productions ---    //
  3. // ************************************************* //
  4. //              PS3Lib v4 By FM|T iMCSx              //
  5. //                                                   //
  6. // Features v4.3 :                                   //
  7. // - Support CCAPI v2.5 C# by iMCSx                  //
  8. // - Popup better form with icon                     //
  9. // - CCAPI Consoles List Popup French/English        //
  10. // - CCAPI Get Console Info                          //
  11. // - CCAPI Get Console List                          //
  12. // - CCAPI Get Number Of Consoles                    //
  13. // - Get Console Name TMAPI/CCAPI                    //
  14. //                                                   //
  15. // Credits : FM|T Enstone , Buc-ShoTz                //
  16. //                                                   //
  17. // Follow me :                                       //
  18. //                                                   //
  19. // FrenchModdingTeam.com                             //
  20. // Youtube.com/iMCSx                                 //
  21. // Twitter.com/iMCSx                                 //
  22. // Facebook.com/iMCSx                                //
  23. //                                                   //
  24. // ************************************************* //
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Runtime.InteropServices;
  30. using System.Text;
  31. using System.Reflection;
  32. using System.Threading.Tasks;
  33. using System.Windows.Forms;
  34. using System.IO;
  35.  
  36. namespace PS3Lib
  37. {
  38.     public class CCAPI
  39.     {
  40.         public CCAPI()
  41.         {
  42.             if (GetDllVersion() < 250)
  43.             {
  44.                 MessageBox.Show("Invalid CCAPI version.\n\nPlease install the version 2.50.", "Re-install CCAPI v2.50", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45.             }
  46.         }
  47.  
  48.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi15initTargetCommsEv", CallingConvention = CallingConvention.Cdecl)]
  49.         private static extern int initTargetComms();
  50.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16closeTargetCommsEv", CallingConvention = CallingConvention.Cdecl)]
  51.         private static extern int closeTargetComms();
  52.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14connectConsoleEPKc", CallingConvention = CallingConvention.Cdecl)]
  53.         private static extern int connectConsole(string targetIP);
  54.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi17disconnectConsoleEi", CallingConvention = CallingConvention.Cdecl)]
  55.         private static extern int disconnectConsole(int connectionID);
  56.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getProcessListEiPjS0_", CallingConvention = CallingConvention.Cdecl)]
  57.         private static extern int getProcessList(int connectionID, ref uint numberProcesses, IntPtr processIdPtr);
  58.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getProcessNameEijPc", CallingConvention = CallingConvention.Cdecl)]
  59.         private static extern int getProcessName(int connectionID, uint processID, IntPtr strPtr);
  60.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16getProcessMemoryEijyjPh", CallingConvention = CallingConvention.Cdecl)]
  61.         private static extern int getProcessMemory(int connectionID, uint processID, ulong offset, uint size, byte[] buffOut);
  62.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16setProcessMemoryEijyjPh", CallingConvention = CallingConvention.Cdecl)]
  63.         private static extern int setProcessMemory(int connectionID, uint processID, ulong offset, uint size, byte[] buffIn);
  64.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi8shutdownEii", CallingConvention = CallingConvention.Cdecl)]
  65.         private static extern int shutdown(int connectionId, int mode);
  66.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi10ringBuzzerEii", CallingConvention = CallingConvention.Cdecl)]
  67.         private static extern int ringBuzzer(int connectionId, int type);
  68.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi13setConsoleLedEiii", CallingConvention = CallingConvention.Cdecl)]
  69.         private static extern int setConsoleLed(int connectionId, int color, int status);
  70.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi15getFirmwareInfoEiPiS0_PyS0_", CallingConvention = CallingConvention.Cdecl)]
  71.         private static extern int getFirmwareInfo(int connectionId, ref int firmware, ref int ccapi, ref ulong systable, ref int consoleType);
  72.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getTemperatureEiPj", CallingConvention = CallingConvention.Cdecl)]
  73.         private static extern int getTemperature(int connectionId, IntPtr tempPtr);
  74.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi12setConsoleIDEiPh", CallingConvention = CallingConvention.Cdecl)]
  75.         private static extern int setConsoleID(int connectionId, byte[] consoleID);
  76.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi6notifyEiiPw", CallingConvention = CallingConvention.Cdecl)]
  77.         private static extern int notify(int connectionId, int mode, [MarshalAsAttribute(UnmanagedType.LPWStr)] string msgWChar);
  78.         [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi13getDllVersionEv", CallingConvention = CallingConvention.Cdecl)]
  79.         private static extern int getDllVersion();
  80.  
  81.         [DllImport("CCAPI.dll", EntryPoint = "_ZN8Settings19getNumberOfConsolesEv", CallingConvention = CallingConvention.Cdecl)]
  82.         private static extern int getNumberOfConsoles();
  83.         [DllImport("CCAPI.dll", EntryPoint = "_ZN8Settings14getConsoleInfoEiPcS0_", CallingConvention = CallingConvention.Cdecl)]
  84.         private static extern int getConsoleInfo(int index, IntPtr ptrN, IntPtr ptrI);
  85.  
  86.         public enum NotifyIcon
  87.         {
  88.             INFO,
  89.             CAUTION,
  90.             FRIEND,
  91.             SLIDER,
  92.             WRONGWAY,
  93.             DIALOG,
  94.             DIALOGSHADOW,
  95.             TEXT,
  96.             POINTER,
  97.             GRAB,
  98.             HAND,
  99.             PEN,
  100.             FINGER,
  101.             ARROW,
  102.             ARROWRIGHT,
  103.             PROGRESS
  104.         }
  105.  
  106.         public enum ConsoleType
  107.         {
  108.             CEX = 1,
  109.             DEX = 2,
  110.             TOOL = 3
  111.         }
  112.  
  113.         public enum ProcessType
  114.         {
  115.             VSH,
  116.             SYS_AGENT,
  117.             CURRENTGAME
  118.         }
  119.  
  120.         public enum RebootFlags
  121.         {
  122.             ShutDown = 1,
  123.             SoftReboot = 2,
  124.             HardReboot = 3
  125.         }
  126.  
  127.         public enum BuzzerMode
  128.         {
  129.             Continuous,
  130.             Single,
  131.             Double
  132.         }
  133.  
  134.         public enum LedColor
  135.         {
  136.             Green = 1,
  137.             Red = 2
  138.         }
  139.  
  140.         public enum LedMode
  141.         {
  142.             Off,
  143.             On,
  144.             Blink
  145.         }
  146.  
  147.         private TargetInfo pInfo = new TargetInfo();
  148.  
  149.  
  150.         private class CCAPIGlobalPointer
  151.         {
  152.             private IntPtr _intPtr = IntPtr.Zero;
  153.             public CCAPIGlobalPointer(IntPtr intPtr)
  154.             {
  155.                 _intPtr = intPtr;
  156.             }
  157.  
  158.             ~CCAPIGlobalPointer()
  159.             { if (_intPtr != IntPtr.Zero)
  160.                     Marshal.FreeHGlobal(_intPtr); }
  161.  
  162.             public IntPtr GetPtr()
  163.             {
  164.                 return _intPtr;
  165.             }
  166.         }
  167.  
  168.         private IntPtr ReadDataFromUnBufPtr<T>(IntPtr unBuf, ref T storage)
  169.         {
  170.             storage = (T)Marshal.PtrToStructure(unBuf, typeof(T));
  171.             return new IntPtr(unBuf.ToInt64() + Marshal.SizeOf((T)storage));
  172.         }
  173.  
  174.  
  175.         private class System
  176.         {
  177.             public static int
  178.                 connectionID = -1;
  179.             public static uint
  180.                 processID = 0;
  181.             public static uint[]
  182.                 processIDs;
  183.         }
  184.  
  185.         /// <summary>Get informations from your target.</summary>
  186.         public class TargetInfo
  187.         {
  188.             public int
  189.                 Firmware = 0,
  190.                 CCAPI = 0,
  191.                 ConsoleType = 0,
  192.                 TempCell = 0,
  193.                 TempRSX = 0;
  194.             public ulong
  195.                 SysTable = 0;
  196.         }
  197.  
  198.         /// <summary>Get Info for targets.</summary>
  199.         public class ConsoleInfo
  200.         {
  201.             public string
  202.                 Name,
  203.                 Ip;
  204.         }
  205.  
  206.         public Extension Extension
  207.         {
  208.             get { return new Extension(SelectAPI.ControlConsole); }
  209.         }
  210.  
  211.         private void CompleteInfo(ref TargetInfo Info, int fw, int ccapi, ulong sysTable, int consoleType, int tempCELL, int tempRSX)
  212.         {
  213.             Info.Firmware = fw;
  214.             Info.CCAPI = ccapi;
  215.             Info.SysTable = sysTable;
  216.             Info.ConsoleType = consoleType;
  217.             Info.TempCell = tempCELL;
  218.             Info.TempRSX = tempRSX;
  219.         }
  220.  
  221.         /// <summary>Return true if a ccapi function return a good integer.</summary>
  222.         public bool SUCCESS(int Void)
  223.         {
  224.             if (Void >= 0)
  225.                 return true;
  226.             else return false;
  227.         }
  228.  
  229.         /// <summary>Connect your console by console list.</summary>
  230.         public bool ConnectTarget()
  231.         {
  232.             return new PS3API.ConsoleList(new PS3API(SelectAPI.ControlConsole)).Show();
  233.         }
  234.  
  235.         /// <summary>Connect your console by ip address.</summary>
  236.         public int ConnectTarget(string targetIP)
  237.         {
  238.             if (SUCCESS(System.connectionID))
  239.                 disconnectConsole(System.connectionID);
  240.             initTargetComms();
  241.             System.connectionID = connectConsole(targetIP);
  242.             if (System.connectionID == -7)
  243.                 MessageBox.Show("Invalid version between CCAPI.dll and the version installed on your ps3.\n\nPlease re-install the same version.", "Invalid Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
  244.             return System.connectionID;
  245.         }
  246.  
  247.         /// <summary>Disconnect your console.</summary>
  248.         public int DisconnectTarget()
  249.         {
  250.             return disconnectConsole(System.connectionID);
  251.         }
  252.  
  253.         /// <summary>Attach the default process (Current Game).</summary>
  254.         public int AttachProcess()
  255.         {
  256.             int result = -1; System.processID = 0;
  257.             result = GetProcessList(out System.processIDs);
  258.             if (SUCCESS(result) && System.processIDs.Length > 0)
  259.             {
  260.                 for (int i = 0; i < System.processIDs.Length; i++)
  261.                 {
  262.                     string name = String.Empty;
  263.                     result = GetProcessName(System.processIDs[i], out name);
  264.                     if (!SUCCESS(result))
  265.                         break;
  266.                     if (!name.Contains("flash"))
  267.                     {
  268.                         System.processID = System.processIDs[i];
  269.                         break;
  270.                     }
  271.                     else result = -1;
  272.                 }
  273.                 if(System.processID == 0)
  274.                     System.processID = System.processIDs[System.processIDs.Length-1];
  275.             }
  276.             else result = -1;
  277.             return result;
  278.         }
  279.  
  280.         /// <summary>Attach your desired process.</summary>
  281.         public int AttachProcess(ProcessType procType)
  282.         {
  283.             int result = -1; System.processID = 0;
  284.             result = GetProcessList(out System.processIDs);
  285.             if (result >= 0 && System.processIDs.Length > 0)
  286.             {
  287.                 for (int i = 0; i < System.processIDs.Length; i++)
  288.                 {
  289.                     string name = String.Empty;
  290.                     result = GetProcessName(System.processIDs[i], out name);
  291.                     if (result < 0)
  292.                         break;
  293.                     if(procType == ProcessType.VSH && name.Contains("vsh"))
  294.                     {
  295.                         System.processID = System.processIDs[i]; break;
  296.                     }
  297.                     else if (procType == ProcessType.SYS_AGENT && name.Contains("agent"))
  298.                     {
  299.                         System.processID = System.processIDs[i]; break;
  300.                     }
  301.                     else if (procType == ProcessType.CURRENTGAME && !name.Contains("flash"))
  302.                     {
  303.                         System.processID = System.processIDs[i]; break;
  304.                     }
  305.                 }
  306.                 if (System.processID == 0)
  307.                     System.processID = System.processIDs[System.processIDs.Length - 1];
  308.             }
  309.             else result = -1;
  310.             return result;
  311.         }
  312.  
  313.          /// <summary>Attach your desired process.</summary>
  314.         public int AttachProcess(uint process)
  315.         {
  316.             int result = -1;
  317.             uint[] procs = new uint[64];
  318.             result = GetProcessList(out procs);
  319.             if(SUCCESS(result))
  320.             {
  321.                 for (int i = 0; i < procs.Length; i++)
  322.                 {
  323.                     if (procs[i] == process)
  324.                     {
  325.                         result = 0;
  326.                         System.processID = process;
  327.                         break;
  328.                     }
  329.                     else result = -1;
  330.                 }
  331.             }
  332.             return result;
  333.         }
  334.  
  335.         /// <summary>Get a list of all processes available.</summary>
  336.         public int GetProcessList(out uint[] processIds)
  337.         {
  338.             uint numOfProcs = 0; int result = -1;
  339.             CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int) (4*0x40)));
  340.             result = getProcessList(System.connectionID, ref numOfProcs, uPtr.GetPtr());
  341.             processIds = new uint[numOfProcs];
  342.             if (SUCCESS(result))
  343.             {
  344.                 IntPtr unBuf = uPtr.GetPtr();
  345.                 for (uint i = 0; i < numOfProcs; i++)
  346.                     unBuf = ReadDataFromUnBufPtr<uint>(unBuf, ref processIds[i]);
  347.             }
  348.             return result;
  349.         }
  350.  
  351.         /// <summary>Get the process name of your choice.</summary>
  352.         public int GetProcessName(uint processId, out string name)
  353.         {
  354.             IntPtr ptr = Marshal.AllocHGlobal((int)(0x211)); int result = -1;
  355.             result = getProcessName(System.connectionID, processId, ptr);
  356.             name = String.Empty;
  357.             if(SUCCESS(result))
  358.                 name = Marshal.PtrToStringAnsi(ptr);
  359.             return result;
  360.         }
  361.  
  362.         /// <summary>Return the current process attached. Use this function only if you called AttachProcess before.</summary>
  363.         public uint GetAttachedProcess()
  364.         {
  365.             return System.processID;
  366.         }
  367.  
  368.         /// <summary>Set memory to offset (uint).</summary>
  369.         public int SetMemory(uint offset, byte[] buffer)
  370.         {
  371.             return setProcessMemory(System.connectionID, System.processID, (ulong)offset, (uint)buffer.Length, buffer);
  372.         }
  373.  
  374.         /// <summary>Set memory to offset (ulong).</summary>
  375.         public int SetMemory(ulong offset, byte[] buffer)
  376.         {
  377.             return setProcessMemory(System.connectionID, System.processID, offset, (uint)buffer.Length, buffer);
  378.         }
  379.  
  380.         /// <summary>Set memory to offset (string hex).</summary>
  381.         public int SetMemory(ulong offset, string hexadecimal, EndianType Type = EndianType.BigEndian)
  382.         {
  383.             byte[] Entry = StringToByteArray(hexadecimal);
  384.             if (Type == EndianType.LittleEndian)
  385.                 Array.Reverse(Entry);
  386.             return setProcessMemory(System.connectionID, System.processID, offset, (uint)Entry.Length, Entry);
  387.         }
  388.  
  389.         /// <summary>Get memory from offset (uint).</summary>
  390.         public int GetMemory(uint offset, byte[] buffer)
  391.         {
  392.             return getProcessMemory(System.connectionID, System.processID, (ulong)offset, (uint)buffer.Length, buffer);
  393.         }
  394.  
  395.         /// <summary>Get memory from offset (ulong).</summary>
  396.         public int GetMemory(ulong offset, byte[] buffer)
  397.         {
  398.             return getProcessMemory(System.connectionID, System.processID, offset, (uint)buffer.Length, buffer);
  399.         }
  400.  
  401.         /// <summary>Like Get memory but this function return directly the buffer from the offset (uint).</summary>
  402.         public byte[] GetBytes(uint offset, uint length)
  403.         {
  404.             byte[] buffer = new byte[length];
  405.             GetMemory(offset, buffer);
  406.             return buffer;
  407.         }
  408.  
  409.         /// <summary>Like Get memory but this function return directly the buffer from the offset (ulong).</summary>
  410.         public byte[] GetBytes(ulong offset, uint length)
  411.         {
  412.             byte[] buffer = new byte[length];
  413.             GetMemory(offset, buffer);
  414.             return buffer;
  415.         }
  416.  
  417.         /// <summary>Display the notify message on your PS3.</summary>
  418.         public int Notify(NotifyIcon icon, string message)
  419.         {
  420.             return notify(System.connectionID, (int)icon, message);
  421.         }
  422.  
  423.         /// <summary>Display the notify message on your PS3.</summary>
  424.         public int Notify(int icon, string message)
  425.         {
  426.             return notify(System.connectionID, icon, message);
  427.         }
  428.  
  429.         /// <summary>You can shutdown the console or just reboot her according the flag selected.</summary>
  430.         public int ShutDown(RebootFlags flag)
  431.         {
  432.             return shutdown(System.connectionID, (int)flag);
  433.         }
  434.  
  435.         /// <summary>Your console will emit a song.</summary>
  436.         public int RingBuzzer(BuzzerMode flag)
  437.         {
  438.             return ringBuzzer(System.connectionID, (int)flag);
  439.         }
  440.  
  441.         /// <summary>Change leds for your console.</summary>
  442.         public int SetConsoleLed(LedColor color, LedMode mode)
  443.         {
  444.             return setConsoleLed(System.connectionID, (int)color, (int)mode);
  445.         }
  446.  
  447.         private int GetTargetInfo()
  448.         {
  449.             int result = -1; int[] sysTemp = new int[2];
  450.             int fw = 0, ccapi = 0, consoleType = 0; ulong sysTable = 0;
  451.             result = getFirmwareInfo(System.connectionID, ref fw, ref ccapi, ref sysTable, ref consoleType);
  452.             if (result >= 0)
  453.             {
  454.                 CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int)(2 * 4)));
  455.                 result = getTemperature(System.connectionID, uPtr.GetPtr());
  456.                 if (result >= 0)
  457.                 {
  458.                     IntPtr unBuf = uPtr.GetPtr();
  459.                     for (uint i = 0; i < 2; i++)
  460.                         unBuf = ReadDataFromUnBufPtr<int>(unBuf, ref sysTemp[i]);
  461.                     CompleteInfo(ref pInfo, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
  462.                 }
  463.             }
  464.             return result;
  465.         }
  466.  
  467.         /// <summary>Get informations of your console and store them into TargetInfo class.</summary>
  468.         public int GetTargetInfo(out TargetInfo Info)
  469.         {
  470.             Info = new TargetInfo();
  471.             int result = -1; int[] sysTemp = new int[2];
  472.             int fw = 0, ccapi = 0, consoleType = 0; ulong sysTable = 0;
  473.             result = getFirmwareInfo(System.connectionID, ref fw, ref ccapi, ref sysTable, ref consoleType);
  474.             if (result >= 0)
  475.             {
  476.                 CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int)(2 * 4)));
  477.                 result = getTemperature(System.connectionID, uPtr.GetPtr());
  478.                 if (result >= 0)
  479.                 {
  480.                     IntPtr unBuf = uPtr.GetPtr();
  481.                     for (uint i = 0; i < 2; i++)
  482.                         unBuf = ReadDataFromUnBufPtr<int>(unBuf, ref sysTemp[i]);
  483.                     CompleteInfo(ref Info, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
  484.                     CompleteInfo(ref pInfo, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
  485.                 }
  486.             }
  487.             return result;
  488.         }
  489.  
  490.         /// <summary>Return the current firmware of your console in string format.</summary>
  491.         public string GetFirmwareVersion()
  492.         {
  493.             if (pInfo.Firmware == 0)
  494.                 GetTargetInfo();
  495.  
  496.             string ver = pInfo.Firmware.ToString("X8");
  497.             string char1 = ver.Substring(1, 1) + ".";
  498.             string char2 = ver.Substring(3, 1);
  499.             string char3 = ver.Substring(4, 1);
  500.             return char1 + char2 + char3;
  501.         }
  502.  
  503.         /// <summary>Return the current temperature of your system in string.</summary>
  504.         public string GetTemperatureCELL()
  505.         {
  506.             if (pInfo.TempCell == 0)
  507.                 GetTargetInfo(out pInfo);
  508.  
  509.             return pInfo.TempCell.ToString() + " C";
  510.         }
  511.  
  512.         /// <summary>Return the current temperature of your system in string.</summary>
  513.         public string GetTemperatureRSX()
  514.         {
  515.             if (pInfo.TempRSX == 0)
  516.                 GetTargetInfo(out pInfo);
  517.             return pInfo.TempRSX.ToString() + " C";
  518.         }
  519.  
  520.         /// <summary>Return the type of your firmware in string format.</summary>
  521.         public string GetFirmwareType()
  522.         {
  523.             if (pInfo.ConsoleType.ToString() == "")
  524.                 GetTargetInfo(out pInfo);
  525.             string type = String.Empty;
  526.             if (pInfo.ConsoleType == (int)ConsoleType.CEX)
  527.                 type = "CEX";
  528.             else if (pInfo.ConsoleType == (int)ConsoleType.DEX)
  529.                 type = "DEX";
  530.             else if (pInfo.ConsoleType == (int)ConsoleType.TOOL)
  531.                 type = "TOOL";
  532.             return type;
  533.         }
  534.  
  535.         /// <summary>Clear informations into the DLL (PS3Lib).</summary>
  536.         public void ClearTargetInfo()
  537.         {
  538.             pInfo = new TargetInfo();
  539.         }
  540.  
  541.         /// <summary>Set a new ConsoleID in real time. (string)</summary>
  542.         public int SetConsoleID(string consoleID)
  543.         {
  544.             string newCID = String.Empty;
  545.             if (consoleID.Length >= 32)
  546.                 newCID = consoleID.Substring(0, 32);
  547.             return setConsoleID(System.connectionID, StringToByteArray(newCID));
  548.         }
  549.  
  550.         /// <summary>Set a new ConsoleID in real time. (bytes)</summary>
  551.         public int SetConsoleID(byte[] consoleID)
  552.         {
  553.             return setConsoleID(System.connectionID, consoleID);
  554.         }
  555.  
  556.         /// <summary>Return CCAPI Version.</summary>
  557.         public int GetDllVersion()
  558.         {
  559.             return getDllVersion();
  560.         }
  561.  
  562.         /// <summary>Return a list of informations for each console available.</summary>
  563.         public List<ConsoleInfo> GetConsoleList()
  564.         {
  565.             List<ConsoleInfo> data = new List<ConsoleInfo>();
  566.             int targetCount = getNumberOfConsoles();
  567.             IntPtr name = Marshal.AllocHGlobal((int)(256)),
  568.                        ip = Marshal.AllocHGlobal((int)(256));
  569.             for (int i = 0; i < targetCount; i++)
  570.             {
  571.                 ConsoleInfo Info = new ConsoleInfo();
  572.                 getConsoleInfo(i, name, ip);
  573.                 Info.Name = Marshal.PtrToStringAnsi(name);
  574.                 Info.Ip = Marshal.PtrToStringAnsi(ip);
  575.                 data.Add(Info);
  576.             }
  577.             Marshal.FreeHGlobal(name);
  578.             Marshal.FreeHGlobal(ip);
  579.             return data;
  580.         }
  581.  
  582.         internal static byte[] StringToByteArray(string hex)
  583.         {
  584.             string replace = hex.Replace("0x", "");
  585.             string Stringz = replace.Insert(replace.Length - 1, "0");
  586.  
  587.             int Odd = replace.Length;
  588.             bool Nombre;
  589.             if (Odd % 2 == 0)
  590.                 Nombre = true;
  591.             else
  592.                 Nombre = false;
  593.             try
  594.             {
  595.                 if (Nombre == true)
  596.                 {
  597.                     return Enumerable.Range(0, replace.Length)
  598.                  .Where(x => x % 2 == 0)
  599.                  .Select(x => Convert.ToByte(replace.Substring(x, 2), 16))
  600.                  .ToArray();
  601.                 }
  602.                 else
  603.                 {
  604.                     return Enumerable.Range(0, replace.Length)
  605.                  .Where(x => x % 2 == 0)
  606.                  .Select(x => Convert.ToByte(Stringz.Substring(x, 2), 16))
  607.                  .ToArray();
  608.                 }
  609.             }
  610.             catch { throw new ArgumentException("Value not possible.", "Byte Array"); }
  611.         }
  612.      }
  613. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement