网上的代码大同小异(见一楼回复),获取的cpuid有相同的,自然不可以用,不过有人用 delphi写的dll可以获取到真正的cpuid ,dll下载
http://d.download.csdn.net/down/945504/skniceDLL中只包含了一个函数
procedure GetCPUID(CPUID: PChar); stdcall; external 'CPUID_Util';调用它很简单,只要传入一个字符串指针,返回时就可以得到CPU的序列号了,是一个字符串DELPHI的调用举例如下
procedure TForm1.Button1Click(Sender: TObject);
var
pc: Array[0..255] of Char;
begin
GetCPUID(pc);
Edit1.Text := StrPas(pc);
end;dll详细介绍 http://www.zjda07.cn/blog/article.asp?id=3
问题是我想在C#程序里调用这个dll来获取到cpuid,
我是这样用的
//定义DLL文件名,此文件路径要加到系统Path中
private const string _fileDll = @"CPUID_Util.dll";
//GetCPUID是 dll里面的一个方法
private const string _FountionName = @"GetCPUID";
[DllImport(_fileDll, EntryPoint = _FountionName, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//C#中的申明
public static extern string GetCPUID(string id);
但是调用此方法,没有返回任何值,请高人指点,应该如何调用才能返回正确的cpuid
http://d.download.csdn.net/down/945504/skniceDLL中只包含了一个函数
procedure GetCPUID(CPUID: PChar); stdcall; external 'CPUID_Util';调用它很简单,只要传入一个字符串指针,返回时就可以得到CPU的序列号了,是一个字符串DELPHI的调用举例如下
procedure TForm1.Button1Click(Sender: TObject);
var
pc: Array[0..255] of Char;
begin
GetCPUID(pc);
Edit1.Text := StrPas(pc);
end;dll详细介绍 http://www.zjda07.cn/blog/article.asp?id=3
问题是我想在C#程序里调用这个dll来获取到cpuid,
我是这样用的
//定义DLL文件名,此文件路径要加到系统Path中
private const string _fileDll = @"CPUID_Util.dll";
//GetCPUID是 dll里面的一个方法
private const string _FountionName = @"GetCPUID";
[DllImport(_fileDll, EntryPoint = _FountionName, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//C#中的申明
public static extern string GetCPUID(string id);
但是调用此方法,没有返回任何值,请高人指点,应该如何调用才能返回正确的cpuid
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Management;
namespace ComLib.Win32.Hardware
{
public class Common
{
/// <summary>
/// cpu序列号
/// </summary>
/// <returns></returns>
public static string GetProcessorId()
{
string cpuInfo = "";
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
break; }
return cpuInfo;
}
} }
//C#中的申明
public static extern string GetCPUID(StringBuilder id);
/// 获取系统信息
/// </summary>
/// <param name="cpuinfo"></param>
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo); [StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId; //CPU的OEM-ID
public uint dwPageSize; //CPU中的页面大小
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors; //多少个CPU
public uint dwProcessorType; //CPU类型
public uint dwAllocationGranularity;
public uint dwProcessorLevel; //CPU等级
public uint dwProcessorRevision;
} 用API呢?
public static extern string CPUIDfromDll(StringBuilder _CPUID);
arbond
private const string _fileDll = @"CPUID_Util.dll";
//GetCPUID是 dll里面的一个方法
private const string _FountionName = @"GetCPUID";
[DllImport(_fileDll, EntryPoint = _FountionName, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//C#中的申明
public static extern string GetCPUID(string id);
该dll需要的是一个StringBuilder(一个可变的字符串类型。并不是一个string类型)
解决办法:
//声明一个stringBuilder类型字符串
StringBuilder strcpu=new StringBuilder();
//将strcpu带入函数中
GetCPUID(strcpu);
//此时的strcpu已经是cpuid了
Console.writeLine(strcpu);