HardwareID.dll is a plain Windows DLL, so C# calls it through DllImport rather than COM. This tutorial declares the functions, reads the machine ID into a buffer and checks for VMware.

The returned ID is a short string that a customer can read over the phone or paste into a registration form.

Before you start

  1. Install the HardwareID DLL trial so the DLL is available to the application.
  2. Add the DllImport declarations shown below.
  3. Call SetLicenseKey() once at startup.
  4. Read the ID into a StringBuilder and send it to your licensing service.

Complete example

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;  //DllImport namespace

namespace Project
{
    public partial class Form1 : Form
    {
        // Declare Statement, Import HardwareID.dll
        [DllImport("HardwareID.dll")]
        public static extern void SetLicenseKey(string LicenseKey);

        [DllImport("HardwareID.dll")]
        public static extern void SetAppName(string AppName);

        [DllImport("HardwareID.dll", EntryPoint = "GetHardwareId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern int GetHardwareId(bool HDD, bool NIC, bool CPU, bool BIOS, StringBuilder lpHWID, int nMaxCount);

        [DllImport("HardwareID.dll", EntryPoint = "IsInsideVMWare", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern bool IsInsideVMWare();

        [DllImport("HardwareID.dll", EntryPoint = "IsInsideVirtualPC", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern bool IsInsideVirtualPC();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetAppName("");

            //Marshal.PtrToStringAnsi();
            StringBuilder lpHWID = new StringBuilder(255);
            GetHardwareId(cb_HDD.Checked, cb_NIC.Checked, cb_CPU.Checked, cb_BIOS.Checked, lpHWID, 255);
            textBox1.Text = lpHWID.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetAppName(textBox2.Text);

            StringBuilder lpHWID = new StringBuilder(255);
            GetHardwareId(cb_HDD.Checked, cb_NIC.Checked, cb_CPU.Checked, cb_BIOS.Checked, lpHWID, 255);
            textBox3.Text = lpHWID.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetLicenseKey("Your-License-Key");

            if (IsInsideVMWare())
            {
                labVMWare.Text = "Yes";
            }
        }
    }
}

How it works

  • Keep the DLL next to the executable or in a directory on the DLL search path.
  • Use the same platform target (x86/x64) as the DLL build.
  • Choose hardware sources carefully: including too many components can make the ID change after a hardware upgrade.
During evaluation, leave the placeholder license key in place. After purchase, replace it with the key from your email.