# VBForums CodeBank > CodeBank - Visual Basic .NET >  [VB.NET 2005] A Simple Class for Getting Hardware Info

## Jenner

Another question I see somewhat often is how to retrieve processor serial numbers, etc from specific hardware.  Most likely used in hardware-lock type licensing.  Below are some simple functions for getting hardware specific info such as CPU ID, Motherboard Serial Number, Drive Serial Numbers and MAC address.  

Enjoy!  Rate if you like it!




```
Imports System
Imports System.Management

Public Class clsComputerInfo

    Friend Function GetProcessorId() As String
        Dim strProcessorId As String = String.Empty
        Dim query As New SelectQuery("Win32_processor")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject

        For Each info In search.Get()
            strProcessorId = info("processorId").ToString()
        Next
        Return strProcessorId

    End Function

    Friend Function GetMACAddress() As String

        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim MACAddress As String = String.Empty
        For Each mo As ManagementObject In moc

            If (MACAddress.Equals(String.Empty)) Then
                If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()

                mo.Dispose()
            End If
            MACAddress = MACAddress.Replace(":", String.Empty)

        Next
        Return MACAddress
    End Function

    Friend Function GetVolumeSerial(Optional ByVal strDriveLetter As String = "C") As String

        Dim disk As ManagementObject = New ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", strDriveLetter))
        disk.Get()
        Return disk("VolumeSerialNumber").ToString()
    End Function

    Friend Function GetMotherBoardID() As String

        Dim strMotherBoardID As String = String.Empty
        Dim query As New SelectQuery("Win32_BaseBoard")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        For Each info In search.Get()

            strMotherBoardID = info("SerialNumber").ToString()

        Next
        Return strMotherBoardID

    End Function
   
End Class
```

----------


## Abdullah_Dossari

Thanks dude

that is clear

----------


## razohad

thanks man good stuff

----------


## Abdullah_Dossari

Is there any way to confirm the Mother Board ID??

----------


## razohad

> Is there any way to confirm the Mother Board ID??


confirm as what?

----------


## Jenner

The motherboard ID is just the hardware serial number encoded to the Motherboard.  Now, if you're asking "Is there a way to confirm that this is a Dell board vs an Asus board vs a Gigabyte board?", then no, not really.  

There are manufacturer IDs typically encoded into the BIOS, but not all boards support them; and even the ones that do, they can be altered w/o too much trouble by someone who knows what they're doing.

----------


## dee-u

I just want to confirm it, does all CPU have serial numbers and are they always unique?

----------


## alexjanjic

> I just want to confirm it, does all CPU have serial numbers and are they always unique?



Would also like to know this because then it would be possible to build another nice Software Protection.

A CPU Serial Number Check

----------


## Jenner

Yes.  CPU ID numbers are always unique, like the VIN numbers on Cars.  In fact, ALL four of those examples should always be unique.  

MAC Addresses of network adapters are carefully controlled to be unique to all the network card manufacturers even, and all the hard drive companies have their own serialization method to tag their hard drives.  

The Motherboard ID is a hit or miss though, as some BIOSes may not contain this information.

All of these are poor substitutes for a dongle though which is the ultimate hardware ID device.

----------


## kk247live

Hi...

Ur coding working properly except motherboard. Actualy i have Windows Vista & XP Os on my pc & GetMotherBoardID Function return value is "Not Applicable". How do i get MotherBoard Serial No in Vb.net.

Thanking in Advance.

KK

----------


## Jenner

If you're not getting a motherboard ID then most likely, the BIOS isn't set up to provide that information for that model of motherboard. It's not available.

----------


## astradamasta

I am using VB.NET 2008 and have of course imported everything properly.

There are many not defined as SelectQuery, ManagementObjectSearcher, ManagementObject, ManagementClass, etc...


It almost as as if there is something else that needs to be imported.

This code means a lot to me, What am I missing here?

I am actually not a newbie to vb.net and that is why I am so fustrated with this.

By the way I did post this into a class and made sure to name the class the same and all of that.

Thanks a bunch looking forward to the solution.

----------


## alexjanjic

My Project > References > Add > System.Managment

----------


## Jenner

Yea, if you don't add a reference to System.Management, it won't work.   I kinda figured that was apparent from the "Imports System.Management" at the top of the class.

----------


## ozoner

Could someone tell me what to do with this class once it's installed, how do you actually use it?  I have the imports correct but I can't figure out what to do next.  Tried to call directly but doesn't show up in the drop down so I know I'm on the wrong track.  I'm using VB.net 2008 with vista64 but, don't think it's a compatibility thing, more like a noob thing.  Thanks.

----------


## Jenner

Just like anything else:



```
Dim hw As New clsComputerInfo

Dim cpu As String

cpu = hw.GetProcessorId()
```

----------


## ardhagp

Hi..
I've tried this code, there is no error, but when i used the 'GetMotherBoardID()' function on a textbox, it filled with '*To be filled by O.E.M.*'. is anybody can explain on this case?

----------


## Arve K.

> Hi..
> I've tried this code, there is no error, but when i used the 'GetMotherBoardID()' function on a textbox, it filled with '*To be filled by O.E.M.*'. is anybody can explain on this case?


I guess that it is exactly what it says; The O.E.M. manufacturer should have put the Motherboard ID there, which they haven't...

----------


## ardhagp

i think its little bit hard if we just depend on motherboard's id. if i using cpu id, is there any possibilities for the CPU doesn't have ID just like the motherboad?

----------


## Arve K.

I'm pretty sure all CPU's has their own unique serial number. Hard drives too...

----------


## Jenner

Network MAC address is an excellent serial number to use as well.  They're guaranteed to be unique.

----------


## el3ashe2

a lot of errors not defined things
like
SelectQuery
ManagementObjectSearcher
ManagementObject

is there is something should i import??
i'm uusing 2008 not 2005

thanks at all  :Smilie:

----------


## el3ashe2

sorry forget to add the system.management great class 
thank you

----------


## ADQUSIT

> Another question I see somewhat often is how to retrieve processor serial numbers, etc from specific hardware.  Most likely used in hardware-lock type licensing.  Below are some simple functions for getting hardware specific info such as CPU ID, Motherboard Serial Number, Drive Serial Numbers and MAC address.  
> 
> Enjoy!  Rate if you like it!
> 
> 
> 
> 
> ```
> Imports System
> ...



hi sir, i developed a project using VB.Net 2005 and sql server 2005 standard edition at back end. now its complete and have to deliver to company now. but i am afraid that the company may reproduce a new copy of this software from the original CD, WHICH I WILL GIVE THEM, and may sell to any other company or in open market. I want to implement some mechanism on my project for its security, which enables it to not to work on other computers or some similar type security. Or if you people have any better idea, (hopefully you will have) so please tell me.     The way you mentioned will it be suitable for my situation?

----------


## Jenner

Yes.  You first use this class to generate some type of hardware ID.  An easy way would be the following:

ChallengeCode = Hash(Encrypt(ProcessorID & MAC Address, key1))

I take my processor ID and append it to my primary MAC address.  Then I encrypt this using some key, and hash it to get a consistent value such as a 10-digit number. 

Next, I report this number as a "challenge code" to whoever I bought the software from.  This "challenge code" is used to make a "license key":

LicenseKey = Hash(Encrypt(ChallengeCode, key2))

As the software maker, you write a small key-generator that converts any given "challenge code" into a "license key".  If they paid their bills, you give them back the "license key".  

The software then does the same computation behind the scenes and compares the "license keys".  If they match, it allows access to the software.  If not, it shuts down because it's an invalid key.

The downside is you need an active "registration" system in place (either via a website, email or a person on the other end of a phone).  A passive registration system doesn't lock the software to a hardware ID and has universal codes.  Compromised codes are eliminated between frequent version updates.

----------


## ADQUSIT

Ok, Sir please tell me now that how do i implement this code? i mean where do i type this lengthy? I mean on a separate form, or somewhere else? please sir guide me step wise. pleaseeee

----------


## Ansl72

to Jenner

Hello I would like to have more information for your algorithm. I think if I understood correctly that in the software must be a function (for example checkLicense) that generates a license key and compares it with the license key provided to me

checkLicense = Hash(Encrypt(ChallengeCode, key2))
if checkLicense = licensekey then 
	softwareActivacted
else software notActivacted

thanks
Angelo

----------


## Jenner

My last explanation is all in psudocode. The functions to encrypt and hash you need to make yourself.  If you look in my signature, you'll see I also have an encryption class with some functions you can use to do this.

License systems are just basic cryptography.  If you want to learn more about the concepts of license systems, authentication systems, and general encryption and hashing, check out this book.  It's absolutely excellent.   I outlined the basics of how a hardware-locked function would work.  It's up to you now to implement the concept into your program.  

You are correct Ansl72, though checkLicense is a variable.  The function looks something like this:



```
'Hash(String) As String: is a hash function.
'Encrypt(String,String) As String: is an encryption function.
'ReadLicenseFromSavedFile() As String: is a function that reads license data from some saved license file.
'SaveLicenseToFile(String): is a function that saves license data to some saved license file.
'FormDialog_PromptForLicenseKey is a custom dialog that asks for the license key.
'EnableSoftwareFullUse(): is a function for enabling your software as a full version.
'key1 and key2 are the keys used by the encrypt function.  They probably shouldn't be stored as plain text like this, but this is a simple example.

Dim key1 As String = "blahblah"
Dim key2 As String = "wibblewibble"
Dim hw As New clsComputerInfo 
Dim challengeCode As String = Hash(Encrypt(hw.GetProcessorID() & hw.GetMACAddress(), key1))
Dim licenseKey As String = Hash(Encrypt(challengeCode, key2))
Dim compareKey as String = ReadLicenseFromSavedFile()

If licenseKey = compareKey Then
    EnableSoftwareFullUse()
Else
    Dim f As New FormDialog_PromptForLicenseKey
    If f.ShowDialog = DialogResult.OK Then
        SaveLicenseToFile(f.LicenseKeyEntered)
        EnableSoftwareFullUse()
    Else
        Me.Close 'Shut down the software, they are not authorized
    End If
End If
```

----------


## obicd

Thanks for the benevolence. I have tried to run this on VB 2008. Despite importing the system management I still have errors such as SelectQuery not defined, ManagementObjectSearcher not defined, ManagementObject not defined. I wish to add this code to a button that users could click to have these Ids displayed, but it appears the function can run under method. Any idea how to get rid of this error n circumvent the issue?

----------


## Jenner

Have you tried:   


```
Dim query As New System.Management.SelectQuery("Win32_processor")
```

?

If it's telling you those are not defined, you still have a reference problem somewhere.   Did you add the reference to System.Management to your project?

----------


## levanduyet

Hi,
How can I convert this class to Visual Basic 6.0?

Thanks,

LVD

----------


## Nightwalker83

> Hi,
> How can I convert this class to Visual Basic 6.0?
> 
> Thanks,
> 
> LVD


You will have to rewrite it! Try searching for "Get Hardware Info" + "VB6" in google.

----------


## Jenner

Yup, you'll most likely have to use Windows API calls to access the WMI system.  I don't even know if WMI even existed when VB6 was written.

----------


## pavlosnet

hello.what i want is to get the serial number of my cpu.
i have used the code you give at your website,that refers at the function GetProcessorId,
at 3 different pc's,where all these pc's were using cpu's of the company INTEL.
 so my problem is that i took the same ID from the 3 pc's i used.
is there any way that will make me able to get a real serial number 
from a cpu,that this serial number is unique?i am using visual studio 2008.
do you have something to suggest?
Thank you and i am looking forward for your answer.

----------


## Jenner

The CPU ID is not a unique serial number.  As far as I know, there is no way to retrieve that from the CPU reliably. In some CPUs it's totally disabled, in others, it's not reported, and in a virtual environment, it's not present.  

Your best bet is to use the MAC ID which MUST be unique in order for networking to work. If you want another level, add this to the CPUID which identifies the CPU make, model and features supported.

----------


## Hjc218

Hey, I am new to VB and would like your help...

I am making a software and want to restrict it to a particular machine, i.e the software cannot be pirated.
I have decided to do the following

=> Get User HDD Id or MAC Id
=> Save that id to my site
=> Whenever the program is executed it'll automatically check if that user's HDD id or MAC id is available on my site, if it does the program will load or else it'll exit.

I'm currently having problem with the "Get Id" code  :Frown: 
I want that as soon as the program runs(loads), the HDD id or MAC id is displayed in a textbox.

Kindly help me with it  :Smilie: 
Thankyou

----------


## vinsoft

> Another question I see somewhat often is how to retrieve processor serial numbers, etc from specific hardware.  Most likely used in hardware-lock type licensing.  Below are some simple functions for getting hardware specific info such as CPU ID, Motherboard Serial Number, Drive Serial Numbers and MAC address.  
> 
> Enjoy!  Rate if you like it!
> 
> 
> 
> 
> ```
> Imports System
> ...



Hi Jenner,

I tried your code on vb.net 2010. It works perfect.I have only one doubt in this that is how can i make the software secure so that the tamping happens nowdays with any product it can't happen?

Wht's the best suggestion you can give in this regard.I don't want the user to crack the code using reassembled,hash editor etc..

Thanks & Regards.

----------


## Jenner

There's no way to make it "totally" uncrackable, but how difficult it'll be depends on how far you're willing to do and how much you're willing to spend. 

First off, just by understanding the basics of encryption and the protocols for using it, you'll put your program way ahead of the pack.  Most license systems fail because of poor implementation or reliance on obfuscation.  If you add in a code obfuscation tool, you'll be even better.  

Then, you make an automated online licensing system that checks the license code entered into the program against a known list of sold keys (and # of times activated within T timeframe) and you're at the pro level of "uncrackable".

If you need to go the extra mile, then you'll need either a continuous online system (like many games these days) that checks each time the user wants to use the program, and/or a hardware solution like a dongle-key; a USB plug with needed licensing / program data on it to get your program to run.  You can also look into professional licensing systems such as FLEXnet.

----------


## vinsoft

> There's no way to make it "totally" uncrackable, but how difficult it'll be depends on how far you're willing to do and how much you're willing to spend. 
> 
> First off, just by understanding the basics of encryption and the protocols for using it, you'll put your program way ahead of the pack.  Most license systems fail because of poor implementation or reliance on obfuscation.  If you add in a code obfuscation tool, you'll be even better.  
> 
> Then, you make an automated online licensing system that checks the license code entered into the program against a known list of sold keys (and # of times activated within T timeframe) and you're at the pro level of "uncrackable".
> 
> If you need to go the extra mile, then you'll need either a continuous online system (like many games these days) that checks each time the user wants to use the program, and/or a hardware solution like a dongle-key; a USB plug with needed licensing / program data on it to get your program to run.  You can also look into professional licensing systems such as FLEXnet.


Hi,

Thanks for quick reply..Please can you give me some sites.As I am very much interested in this.

----------


## Jenner

Just search this site and Google for "License System" and "String Encryption"  

If you want a cookie-cutter solution, try these guys: http://www.ssware.com/cryptolicensin...ensing_net.htm

----------


## djbrian

Hi, how can i get the system information of the other computers connected to my Network?
Thanks.

----------


## Jenner

Interesting question.  I believe there is a way to do remote management calls for getting that information but honestly, I haven't even explored how to do that.  Poke around MSDN and see what you can turn up.  If you find anything, I'd love to hear about it.

----------


## TheThinker

Is it possible to get more information other than the processor and Mainboard details.. perhaps Drivers etc.. using this method?

Perhaps something like select * from Win32_[another item's name here]

----------


## Jenner

Absolutely.  It's all WMI queries.  If you Google a little bit on the WMI system, you'll turn up all kinds of references.  For example, suppose I wanted to know the Video Card's driver:


```
    Function ShowVideoControllerInfo()
        Dim str As String = ""
        Dim query As New System.Management.SelectQuery("Win32_VideoController")
        Dim search As New System.Management.ManagementObjectSearcher(query)
        Dim info As System.Management.ManagementObject

        For Each info In search.Get()
            str = info("DriverVersion").ToString()
        Next
        Return str
       
    End Function
```

----------


## TheThinker

Cool...
I'll give it a shot, as I'd like to create an app that displays Specs of PC

----------

