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;

namespace UniqueID
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            #if DEBUG
                MinimizeBox = false;
            #else       
                MinimizeBox = true;
            #endif
        }

        [DllImport("coredll.dll")]
        private extern static int GetDeviceUniqueID(
        [In, Out] byte[] appdata,
        int cbApplictionData,
        int dwDeviceIDVersion,
        [In, Out] byte[] deviceIDOuput,
        out uint pcbDeviceIDOutput);

        private byte[] GetDeviceID(string AppString)
        {
            // Вызываем GetDeviceUniqueID
            byte[] AppData = new byte[AppString.Length];
            for (int count = 0; count < AppString.Length; count++)
                AppData[count] = (byte)AppString[count];
            int appDataSize = AppData.Length;
            byte[] DeviceOutput = new byte[20];
            uint SizeOut = 20;
            GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, out SizeOut);
            return DeviceOutput;
        }

        private void butGetDevID_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";

            byte[] buffer = GetDeviceID("MyAppString");
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < buffer.Length; x++)
            {
                sb.Append('{');
                sb.Append(string.Format("{0:x2}", buffer[x]));
                sb.Append("} ");
            }
            textBox1.Text = sb.ToString();
        }
    }
}