Complete code for obtaining the hard disk physical serial number

Source: Internet
Author: User

The physical serial number of a hard disk cannot be read in some cases, such as a SCSI hard disk or a user who uses a virtual machine. It is said that a popular SATA hard disk cannot be read now.
My method is: if the physical serial number can be obtained, the physical serial number is used. If the physical serial number cannot be obtained, the volume serial number is used.

Pseudocode:

-------------------------------
Obtain the physical serial number.
If then
Use physical serial number
Else
Volume serial number
End if
--------------------------------

To determine whether the physical serial number is successfully obtained, it must be judged by three factors.
First: If the 8 bytes obtained are all 0, it indicates that it has failed.
The second way: Get the first time and then get it again. If the two results are different, it means that the physical serial number you get is a random value and cannot be used.
Third, an API exception occurs, indicating that the API fails. You must use APIs for exception replacement to intercept API exceptions to ensure that they are handled in VB.

As for how to obtain the volume serial number, it is relatively simple. You can use the API function getvolumeinformation, you can refer to msdn, no need to go into details.

I have a complete code for getting the physical serial number of the hard disk. I have done all the above operations, which are relatively stable and can be used for use. The DLL file is downloaded from applevb: (thanks to www.applevb.com ).

Source code and DLL file is: http://lqweb.crcoo.com/mycode/Hard_Disk_Physical_ID.zip

Bytes ---------------------------------------------------------------------------------------------------------------------------

Let's explain in detail how to intercept DLL exceptions in the above Code: To intercept DLL exceptions, you need to use an API function setunhandledexceptionfilter.

The setunhandledexceptionfilter function replaces the default exception handling function of the system. All exceptions that cannot be processed during the running of the program must be processed by the global exception processing function. This function can be used to replace the default global exception handling function with a common VB Function, all unhandled exceptions that occur during the program running must be handled by the function in VB. For more information, see msdn.

The byval lptoplevelexceptionfilter as long parameter is the address of the function to be replaced. If this parameter is set to 0, the system restores the default global exception handling function.
You can try to define a common VB Function in a standard module that replaces the global exception handling function. The definition is as follows:

'New Exception Handling Function
Public Function newexceptionhandler (_
Byref lpexceptionpointers as prediction_pointers) as long

Msgbox "system exceptions intercepted"
End Function

Then, in the form_load event, write the following code:

Setunhandledexceptionfilter addressof newexceptionhandler

In form_unload, write the following code:
Setunhandledexceptionfilter 0 &

Next, run the program to intercept exceptions, including exceptions caused by external DLL, which greatly enhances the debugging mechanism of the vbprogram.
I will post all the code for reference.

Module1.bas ----------------------------------------------------------------
Option explicit

'Replaces the system's default error handler function.
Public declare function setunhandledexceptionfilter _
Lib "Kernel32" (byval lptoplevelexceptionfilter as long )_
As long

Private type exception_pointers
Pexceptionrecord as long
Contextrecord as long
End type

'New Exception Handling Function
Public Function newexceptionhandler (_
Byref lpexceptionpointers as prediction_pointers) as long

Msgbox intercepts system exceptions! "
End Function

--------------------------------------------------------------------------
Form1.frm -----------------------------------------------------------------
Option explicit
Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)

Private sub commandementclick ()
Copymemory byval 0 &, 8 &, 4
End sub

Private sub form_load ()
Setunhandledexceptionfilter addressof newexceptionhandler

End sub

Private sub form_unload (cancel as integer)
Setunhandledexceptionfilter 0 &
End sub

--------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.