我想要获取唯一不可更改的机器ID,例如计算机的处理器序列号,以便分发软件并避免复制。
我曾尝试使用处理器序列号和硬盘序列号,但这些都会在格式化和重新安装Windows后发生变化。
您有什么想法如何获取计算机的不可更改的序列号吗?
using System.Management;
using System.Windows.Forms;
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_Processor instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Architecture: {0}", queryObj["Architecture"]);
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
Console.WriteLine("Family: {0}", queryObj["Family"]);
Console.WriteLine("ProcessorId: {0}", queryObj["ProcessorId"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
使用WMI在C#中检索硬件标识符, 作者Peter Bromberg
我建议远离使用MAC地址。在某些硬件上,重新启动后MAC地址可能会更改。在我们的研究早期就已经明确不要依赖它。
请查看文章《软件保护和许可证开发》,其中提供了一些指导,说明如何设计和实现应用程序以减少盗版。
强制性免责声明和推广:我共同创立的公司生产OffByZero Cobalt许可解决方案。因此,你可能不会感到意外,我建议外包你的许可证,并专注于你的核心竞争力。
using System.Management;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
foreach (ManagementObject share in searcher.Get()) {
// Some Codes ...
}
上述代码中的Key是一个变量,将被适当的数据替换。例如,要获取CPU信息,您需要用Win32_Processor替换Key。
是的,我们可以获得一个由物理地址、独特驱动器ID、硬盘ID(卷序列号)、CPU ID和BIOS ID组成的代码。
例子(完整示例)://Main physical hard drive ID
private static string diskId()
{
return identifier("Win32_DiskDrive", "Model")
+ identifier("Win32_DiskDrive", "Manufacturer")
+ identifier("Win32_DiskDrive", "Signature")
+ identifier("Win32_DiskDrive", "TotalHeads");
}
//Motherboard ID
private static string baseId()
{
return identifier("Win32_BaseBoard", "Model")
+ identifier("Win32_BaseBoard", "Manufacturer")
+ identifier("Win32_BaseBoard", "Name")
+ identifier("Win32_BaseBoard", "SerialNumber");
}
您不应该使用 MAC 地址,因为一些操作系统每天都会更改它。我的建议是:使用 Tools.CpuID.ProcessorId() + volumeSerial。
string volumeSerial = "";
try {
ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""C:""");
dsk.Get();
volumeSerial = dsk["VolumeSerialNumber"].ToString();
} catch {
try {
ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""D:""");
dsk.Get();
volumeSerial = dsk["VolumeSerialNumber"].ToString();
} catch { File.WriteAllText("disk.mising","need C or D"); Environment.Exit(0); }
}
public class CpuID
{
[DllImport("user32", EntryPoint = "CallWindowProcW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CallWindowProcW([In] byte[] bytes, IntPtr hWnd, int msg, [In, Out] byte[] wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool VirtualProtect([In] byte[] bytes, IntPtr size, int newProtect, out int oldProtect);
const int PAGE_EXECUTE_READWRITE = 0x40;
public static string ProcessorId()
{
byte[] sn = new byte[8];
if (!ExecuteCode(ref sn))
return "ND";
return string.Format("{0}{1}", BitConverter.ToUInt32(sn, 4).ToString("X8"), BitConverter.ToUInt32(sn, 0).ToString("X8"));
}
private static bool ExecuteCode(ref byte[] result)
{
int num;
/* The opcodes below implement a C function with the signature:
* __stdcall CpuIdWindowProc(hWnd, Msg, wParam, lParam);
* with wParam interpreted as an 8 byte unsigned character buffer.
* */
byte[] code_x86 = new byte[] {
0x55, /* push ebp */
0x89, 0xe5, /* mov ebp, esp */
0x57, /* push edi */
0x8b, 0x7d, 0x10, /* mov edi, [ebp+0x10] */
0x6a, 0x01, /* push 0x1 */
0x58, /* pop eax */
0x53, /* push ebx */
0x0f, 0xa2, /* cpuid */
0x89, 0x07, /* mov [edi], eax */
0x89, 0x57, 0x04, /* mov [edi+0x4], edx */
0x5b, /* pop ebx */
0x5f, /* pop edi */
0x89, 0xec, /* mov esp, ebp */
0x5d, /* pop ebp */
0xc2, 0x10, 0x00, /* ret 0x10 */
};
byte[] code_x64 = new byte[] {
0x53, /* push rbx */
0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, /* mov rax, 0x1 */
0x0f, 0xa2, /* cpuid */
0x41, 0x89, 0x00, /* mov [r8], eax */
0x41, 0x89, 0x50, 0x04, /* mov [r8+0x4], edx */
0x5b, /* pop rbx */
0xc3, /* ret */
};
byte[] code;
if (IsX64Process())
code = code_x64;
else
code = code_x86;
IntPtr ptr = new IntPtr(code.Length);
if (!VirtualProtect(code, ptr, PAGE_EXECUTE_READWRITE, out num))
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
ptr = new IntPtr(result.Length);
try {
return (CallWindowProcW(code, IntPtr.Zero, 0, result, ptr) != IntPtr.Zero);
} catch { System.Windows.Forms.MessageBox.Show("Память повреждена"); return false; }
}
private static bool IsX64Process()
{
return IntPtr.Size == 8;
}
}
编辑:我刚刚看到你是指用c#。这里有一种更好的使用非托管代码的方法:
ManagementClass oMClass = new ManagementClass ("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection colMObj = oMCLass.GetInstances();
foreach(ManagementObject objMO in colMObj)
Console.WriteLine(objMO["MacAddress"].ToString());
我知道有两种可能的方法:
获取系统的处理器ID:
public string getCPUId()
{
string cpuInfo = string.Empty;
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (cpuInfo == "")
{
//仅获取第一个CPU的ID
cpuInfo = mo.Properties["processorID"].Value.ToString();
break;
}
}
return cpuInfo;
}
获取系统的UUID:
public string getUUID()
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "CMD.exe";
startInfo.Arguments = "/C wmic csproduct get UUID";
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
return output;
}
UUID
是不好的。 - Gray Programmerz