Вы находитесь на странице: 1из 10

using

using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Diagnostics;
System.Linq;
System.Management;
System.Runtime.InteropServices;
System.Security.Cryptography;
System.Text;
System.Windows;
SlarkStuff.DllHelpers;
SlarkStuff.ErrorReporting;

namespace SlarkStuff.HardwareIdExtractor
{
public static class MyHardwareIdExtractor
{
#region HardwareIDExtractorC.dll
public enum OsMemType : byte // OSMemType used by SystemMemStatus
{
MMemoryLoad = 1, // total memory used in percents (%)
MTotalPhys = 2, // total physical memory in bytes
MAvailPhys = 3, // available physical memory (bytes)
MTotalPageFile = 4, // total page file in (bytes)
MAvailPageFile = 5, // available page file (bytes)
MTotalVirtual = 6, // total virtual memory in bytes
MAvailVirtual = 7 // available virtual memory (bytes)
}
public enum ProcMemType : byte // ProcMemType used by ProcessMemStatus
{
PWorkingSetSize = 1, // the current working set size, in bytes
PPageFaultCount = 2, // the number of page faults
PPeakWorkingSetSize = 3, // the peak working set size, in bytes
PQuotaPeakPagedPoolUsage = 4, // the peak paged pool usage, in bytes
PQuotaPagedPoolUsage = 5, // the current paged pool usage, in bytes
PQuotaPeakNonPagedPool = 6, // the peak nonpaged pool usage, in byte
s
PQuotaNonPagedPoolUsg = 7, // the current nonpaged pool usage, in by
tes
PPageFileUsage = 8, // the current space allocated for the pagefile,
in bytes; those pages may or may not be in memory
PPeakPagefileUsage = 9, // the peak space allocated for the pagefile
, in bytes
}
private static class Native //! NATIVE
{
private const string DllFolder = "libs\\libHardwareIdExtractor\\";
private const string DllName = "HardwareIDExtractorC.dll";
private const string DllPath = /*DllFolder +*/ DllName;
//- SOURCE: http://stackoverflow.com/questions/28117178/how-to-fix-n
tdll-dll-appcrash-with-hardware-id-extractor/28118829?noredirect=1#28118829
//- SOURCE: http://www.soft.tahionic.com/download-hdd_id/hardware%20
id%20programming%20source%20code/exported%20functions%20for%20non-Delphi.html
//- SOURCE: http://www.soft.tahionic.com/download-hdd_id/hardware%20

id%20programming%20source%20code/exported%20functions%20for%20Delphi.html
//- SOURCE: http://www.soft.tahionic.com/download-hdd_id/articles/De
lphi%20to%20C%20type%20conversion.html
//- SOURCE: http://www.soft.tahionic.com/download-hdd_id/hardware%20
id%20programming%20source%20code/CPU_mask.html
//! CPU
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "CPUFamily")]
internal static extern IntPtr CPUFamily(); // get CPUU identifier fr
om the windows registry
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "GetCPUVendor")]
internal static extern IntPtr GetCPUVendor(); // new GetCPUVendor fu
nction, reported to work with D7 and D2009
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetCPUSpeed")] //! free
internal static extern double GetCPUSpeed(int speed = 200); // the h
igher the delay, the accurate the result (default = 200ms)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "IsIntel64BitCPU")] //! free
internal static extern bool IsIntel64BitCPU(); // detects IA64 proce
ssors
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetCpuTheoreticSpeed")]
internal static extern int GetCpuTheoreticSpeed(); // get CPU speed
(in MHz)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "IsCPUIDAvailable")] //! free
internal static extern bool IsCPUIDAvailable();
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "GetCPUID")]
internal static extern IntPtr GetCPUID(ushort coreMask); // get the
ID of the specified logical CPU, max coreMask = GetCPUCount()
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "GetCpuIdNow")]
internal static extern IntPtr GetCpuIdNow(); // get the ID of the fi
rst available logical CPU
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetCPUCount")] //! free
internal static extern int GetCPUCount(); // the number of LOGICAL p
rocessors in the current group
//! SYSTEM RAM
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "SystemMemStatus")] //! free
internal static extern uint SystemMemStatus(OsMemType osMemType); //
in Bytes, Limited by the capacity of the OS (32bits OSs will report max 2GB)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C

harSet = CharSet.Ansi, EntryPoint = "SystemMemStatus_KB")]


internal static extern IntPtr SystemMemStatus_KB(OsMemType osMemType
); // in KB
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "SystemMemStatus_MB")]
internal static extern IntPtr SystemMemStatus_MB(OsMemType osMemType
); // in MB
//! PROCESS RAM
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "ProcessMemStatus")]
internal static extern uint ProcessMemStatus(ProcMemType procMemType
= ProcMemType.PWorkingSetSize); // returns data about the memory used of the cu
rrent process
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "ProcessPeakMem")] //! free
internal static extern IntPtr ProcessPeakMem(); // shows the highest
amount of memory this program ever occupied
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "ProcessCurrentMem")] //! free
internal static extern IntPtr ProcessCurrentMem();
//! VIRTUAL RAM
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetPageSize")] //! free
internal static extern uint GetPageSize(); // the page size and the
granularity of page protection and commitment; this is the page size used by the
VirtualAlloc function
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetMemGranularity")] //! free
internal static extern int GetMemGranularity(); // granularity with
which virtual memory is allocated (in KB)
//! RAM - Advanced stuff
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetLowAddr")] //! free
internal static extern uint GetLowAddr(); // lowest RAM memory addre
ss accessible to applications (this is the RAM address, not virtual memory addre
ss)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "GetHiAddr")] //! free
internal static extern uint GetHiAddr(); // lowest RAM memory addres
s accessible to applications
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "TrimWorkingSet")]
internal static extern void TrimWorkingSet(); // minimizes the amoun
t to RAM used by application by swapping the unused pages back to disk
//! HDD
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C

harSet = CharSet.Ansi, EntryPoint = "GetPartitionID")]


internal static extern IntPtr GetPartitionID(string partition); // g
et the ID of the specified partition; example of parameter: 'C:\\'
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "GetIDESerialNumber")]
internal static extern IntPtr GetIDESerialNumber(byte driveNumber);
// driveNumber is from 0 to 4
//! BIOS (NEW!)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "BiosDate")]
internal static extern IntPtr BiosDate();
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "BiosVersion")]
internal static extern IntPtr BiosVersion(); // could be something l
ike: TOSQCI - 6040000 Ver 1.00PARTTBL. TOS is from Toshiba, Q is comming from pr
oduct series (Qosmio)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "BiosProductID")]
internal static extern IntPtr BiosProductID(); // manufacturer produ
ct (laptop, PC) ID - Could be something like: Toshiba_PQX33U-01G00H
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "BiosVideo")]
internal static extern IntPtr BiosVideo();
//! UTILS
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "BiosVideo")]
internal static extern IntPtr GenerateHardwareReport(); // before ca
lling this I need to enter a valid key into CubicHardID
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "FormatBytes")]
internal static extern IntPtr FormatBytes(Int64 size, byte decimals)
; // format bytes to KB, MB, GB, TB
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "BinToInt")]
internal static extern uint BinToInt(string binaryString);
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "IntToBin")] //! free
internal static extern IntPtr IntToBin(uint value, byte digits);
//- SOURCE: http://www.soft.tahionic.com/download-hdd_id/hardware%20
id%20programming%20source%20code/CPU_mask.html
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "CoreNumber2CoreMask")] //! free
internal static extern ushort CoreNumber2CoreMask(ushort cpuCore); /
/ steps 1, 2, 4, 8, 16, 32, 64, 128 (for 8 logical cores)
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "ReleaseMemory")] //! free
internal static extern void ReleaseMemory(IntPtr p);

[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E


ntryPoint = "GetDllVersion")] //! free
internal static extern double GetDllVersion();
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "ChangeByteOrder")] //! free
internal static extern void ChangeByteOrder(UInt64 data, uint size);
//! not sure what it does, and it doesn't exist in this dll
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, C
harSet = CharSet.Ansi, EntryPoint = "WindowsProductID")] //! free
internal static extern IntPtr WindowsProductID(); //! not sure what
it does, and it doesn't exist in this dll
//! INTERNAL
[DllImport(DllPath, CallingConvention = CallingConvention.StdCall, E
ntryPoint = "EnterKey")]
internal static extern bool EnterKey(int key);
}
private static class Internal //! INTERNAL
{
internal static bool EnterKey(int key = 0)
{
var myKey = key; // get key parameter
if (myKey == 0) myKey = 666; // if default key parameter, set va
lid key
if (Native.EnterKey(myKey)) return true;
MyErrorReporting.ReportError(MyErrorReporting.ErrorId.WrongFinge
rKey, true);
return false;
}
}
public static class Cpu //! CPU
{
public static string CpuFamily()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.CPUFamily();
var cpuid = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return cpuid;
}
public static string GetCpuVendor()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.GetCPUVendor();
var cpuid = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return cpuid;
}
public static double GetCpuSpeed(int delay = 200)
{
return !Internal.EnterKey() ? 0 : Native.GetCPUSpeed();
}

public static bool IsIntel64BitCpu()


{
return Internal.EnterKey() && Native.IsIntel64BitCPU();
}
public static int GetCpuTheoreticSpeed()
{
return !Internal.EnterKey() ? 0 : Native.GetCpuTheoreticSpeed();
}
public static bool IsCpuIdAvailable()
{
return Internal.EnterKey() && Native.IsCPUIDAvailable();
}
public static string GetCpuId(ushort cpuCore)
{
if (!Internal.EnterKey()) return null;
if (!IsCpuIdAvailable()) return null; // return null if CPU ID i
s not available
var myCpuCore = Native.CoreNumber2CoreMask(cpuCore);
if (cpuCore <= 0 || cpuCore > GetCpuCount() || myCpuCore == 0)
{
MyErrorReporting.ReportError(MyErrorReporting.ErrorId.CpuCor
eOutOfRange, silentReport: false);
return null; // return null if requested core number is out
of range
}
var ptr = Native.GetCPUID(myCpuCore); //! issues with when logic
al cores > physical cores (hyperthreading)
var cpuId = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return cpuId;
}
public static string GetCpuIdNative(ushort cpuCore)
{
if (!Internal.EnterKey()) return null;
if (!IsCpuIdAvailable()) return null; // return null if CPU ID i
s not available
var ptr = Native.GetCPUID(cpuCore); //! issues with when logical
cores > physical cores (hyperthreading)
var cpuId = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return cpuId;
}
public static string GetCpuIdNow()
{
if (!Internal.EnterKey()) return null;
if (!IsCpuIdAvailable()) return null; // return null if CPU ID i
s not available
var ptr = Native.GetCpuIdNow();
var cpuIdNow = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return cpuIdNow;
}
public static int GetCpuCount()
{

return !Internal.EnterKey() ? 0 : Native.GetCPUCount();


}
}
public static class SystemRam //! SYSTEM RAM
{
public static uint SystemMemStatus(OsMemType osMemType)
{
//! shows only one memory bank
return !Internal.EnterKey() ? 0 : Native.SystemMemStatus(osMemTy
pe);
}
public static string SystemMemStatus_KB(OsMemType osMemType)
{
if (!Internal.EnterKey()) return null;
var ptr = Native.SystemMemStatus_KB(osMemType);
var sysMem = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return sysMem;
}
public static string SystemMemStatus_MB(OsMemType osMemType)
{
if (!Internal.EnterKey()) return null;
var ptr = Native.SystemMemStatus_MB(osMemType);
var sysMem = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return sysMem;
}
}
public static class ProcessRam //! PROCESS RAM
{
public static uint ProcessMemStatus(ProcMemType procMemType = ProcMe
mType.PWorkingSetSize)
{
//! returns uint in bytes
return !Internal.EnterKey() ? 0 : Native.ProcessMemStatus(procMe
mType);
}
public static string ProcessPeakMem()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.ProcessPeakMem();
var sysMem = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns string in MB
return sysMem;
}
public static string ProcessCurrentMem()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.ProcessCurrentMem();
var sysMem = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns string in MB
return sysMem;

}
}
public static class VirtualRam //! VIRTUAL RAM
{
public static uint GetPageSizeNative()
{
//! returns uint in MB
return Native.GetPageSize();
}
public static int GetMemGranularityNative()
{
//! returns int in KB
return Native.GetMemGranularity();
}
}
public static class AdvancedRam //! RAM - Advanced stuff
{
public static uint GetLowAddrNative()
{
return Native.GetLowAddr();
}
public static uint GetHiAddrNative()
{
return Native.GetHiAddr();
}
public static void TrimWorkingSetNative()
{
//! EXTREMELY USEFUL FUNCTION
if (!Internal.EnterKey()) return;
Native.TrimWorkingSet();
}
}
public static class HardDiskDrive //! HDD
{
public static string GetPartitionID(string partition = "C:\\")
{
if (!Internal.EnterKey()) return null;
var ptr = Native.GetPartitionID(partition);
var partId = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns string formatted Win32_LogicalDisk VolumeSerialNumbe
r
return partId;
}
public static string GetIdeSerialNumber(byte driveNumber)
{
if (!Internal.EnterKey()) return null;
var ptr = Native.GetIDESerialNumber(driveNumber);
var ideSerial = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns formatted hard drive unique ID:
return ideSerial;
}

}
public static class Bios //! BIOS (NEW!)
{
public static string BiosDate()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.BiosDate();
var biosDate = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns a formatted string 02/05/10 (mm/dd/yy)
return biosDate;
}
public static string BiosVersion()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.BiosVersion();
var biosVersion = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns a formatted string with lots of info
return biosVersion;
}
public static string BiosProductId()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.BiosProductID();
var biosProductId = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns "System Version"
return biosProductId;
}
public static string BiosVideo()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.BiosVideo();
var biosVideo = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns a formatted string 02/05/10 (mm/dd/yy)
return biosVideo;
}
}
public static class Utils //! UTILS
{
public static string GenerateHardwareReport()
{
if (!Internal.EnterKey()) return null;
var ptr = Native.GenerateHardwareReport();
var hardwareReport = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns a formatted hardware report (just like the free util
ity does)
return hardwareReport;
}
public static string FormatBytes(Int64 size, byte decimals)
{

if (!Internal.EnterKey()) return null;


var ptr = Native.FormatBytes(size, decimals);
var formattedBytes = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
//! returns a formatted string (e.g. 6,85 KB); max available 102
3 MB (1072693248 bytes)
return formattedBytes;
}
public static uint BinToIntNative(string binaryString)
{
return !Internal.EnterKey() ? 0 : Native.BinToInt(binaryString);
}
public static string IntToBinNative(uint value, byte digits)
{
if (!Internal.EnterKey()) return null;
var ptr = Native.IntToBin(value, digits);
var intToBin = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return intToBin;
}
public static ushort CoreNumber2CoreMaskNative(ushort cpuCore)
{
return Native.CoreNumber2CoreMask(cpuCore);
}
public static void ReleaseMemoryNative(IntPtr p)
{
Native.ReleaseMemory(p);
}
public static double GetDllVersionNative()
{
return Native.GetDllVersion();
}
public static void ChangeByteOrderNative(UInt64 data, uint size)
{
//! not sure what it does, and it doesn't exist in this dll
Native.ChangeByteOrder(data, size);
}
public static string WindowsProductIdNative()
{
//! not sure what it does, and it doesn't exist in this dll
var ptr = Native.WindowsProductID();
var productId = Marshal.PtrToStringAnsi(ptr);
Native.ReleaseMemory(ptr);
return productId;
}
}
}
}

Вам также может понравиться