Home rlByte News USBPhysic C# example (source code sample)
formats

USBPhysic C# example (source code sample)

 In order to get the usb storage device (external portable Hard Drives, flash/pen/key drive etc) serial number and other manufacturer information (NOT volume serial, only the unique one, the one that will not change after a format, physical one) using our USBPhysic library and C# (CSharp) you need to do the following 3 easy steps:

(1) Load USBPhysic library and declare the imported library functions :

 namespace USBPhysic_Example
{
    public partial class Form1 : Form
    {
        //load the library
        [DllImport("USBPhysic")]
        public static extern int Init(string sUser, string sRegCode);

        [DllImport("USBPhysic")]
        public static extern int GetUSBPhysicInfo(int diskIndex, int InfoType, StringBuilder pHddInfo);

        public Form1()
        {
            InitializeComponent();
        }

(2) Call the Init function to initialize the user computer usb storage devices

       private void Form1_Load(object sender, EventArgs e)
        {
            int USBDisks = Init("your reg code here", "your reg code here");

            if (USBDisks > 0)
            {
                for (int i = 0; i < USBDisks; i++)
                {
                    comboBox1.Items.Add(i);
                }
                comboBox1.SelectedIndex = 0;
            }
        }

(3) Now that you have the total hard drives count you can query for the manufacturer information passing the usb device index to the GetPhysicInfo function:

       private void button1_Click(object sender, EventArgs e)
        {
            const int nChars = 1024;
            StringBuilder Buff = StringBuilder(nChars);
           
            //0 = Get SerialNumber
            if (GetUSBPhysicInfo(Int32.Parse(comboBox1.Items[comboBox1.SelectedIndex].ToString()), 0, Buff) > 0)
                textBox1.Text = Buff.ToString();
            else
                textBox1.Text = "n/a";

            //1 = Get product name
            if (GetUSBPhysicInfo(Int32.Parse(comboBox1.Items[comboBox1.SelectedIndex].ToString()), 1, Buff) > 0)
                textBox2.Text = Buff.ToString();
            else
                textBox2.Text = "n/a";

            //2 = Get USB Manufacturer
            if (GetUSBPhysicInfo(Int32.Parse(comboBox1.Items[comboBox1.SelectedIndex].ToString()), 2, Buff) > 0)
                textBox3.Text = Buff.ToString();
            else
                textBox3.Text = "n/a";

            //3 = Get drive letter mapped on Windows
            if (GetUSBPhysicInfo(Int32.Parse(comboBox1.Items[comboBox1.SelectedIndex].ToString()), 3, Buff) > 0)
                textBox4.Text = Buff.ToString();
            else
                textBox4.Text = "n/a";
           
        }

screenshot

  A complete project with source code example for using USBPhysic component to get usb device (hard disk, storage device pen etc) physical information with CSharp (C#) is available in the resources area of the product (here).

 
  Share on Reddit
4 Comments  comments 

4 Responses

  1. He Shumeng

    Hello,my dear friend,
    I have tested “USBPhysic. DLL”, in the initialization process ,a registration form will appear , I do not need a form.please help me.could you give me a new DLL.send it to my E_mail.
    E-mail:
    THANKS.

    He Shumeng

    Reply
    • Hello,

      You need to purchase a license for USBPhysic in order to remove the registration form. Get it from here: Buy USBPhysic license

      After you buy a license you will receive instructions on how to use the full library, without any restrictions or pop-up forms.

      Good luck.

      John Becher

      Reply
  2. Roberto Colmegna

    Hi,
    I work for a company which bought USBPhysic some months ago.

    I’m using you DLL with an old BORLAND-C++ 5.5 compiler.
    I have no problem to pass S/N and licence-key, obtain the USB device number and read the USB-S/N.

    My problem raise when I need to re-scan the USB drives after the first time (I need a scan every 10 secs).

    In this case the program crash invoking your DLL after first scan.

    Could you confirm me that at every scan I must:
    1) load dll
    2) detect functions entry-point
    3) call dll init function
    4) call dll getInfo function with incremental index
    5) free dll
    ?

    TIA
    BR
    Roberto Colmegna

    Reply
    • Hello,

      You don’t need to load the dll again if already loaded (just free the dll after your app exists and load it with your app launch), you just need to call the init function every time you need the drives list, in your case every 10 seconds (It crashes because maybe you release the dll handles or something like that, make sure they are valid/public when using them). Also, see the RegisterDeviceNotification function, “Registering for Device Notification” in MSDN, this way you will refresh only when new usb device inserted. Good luck

      Reply
© rlByte Software
credit