Obtain the hard disk serial number or other information Through WMI

Source: Internet
Author: User

The engineer reported today that our program cannot obtain the machine code (hard disk serial number) under Vista ). After searching for information from Google and the help of msdn, we finally got it done.

 

There are many examples of VB. NET code on the Internet.

 

C #

 

String info = "";
Managementclass MC = new managementclass ("win32_physicalmedia ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
Info = Mo. properties ["serialnumber"]. value. tostring ();
}
MoC = NULL;
MC = NULL;

Textbox1.text = Info;

 

Note: add the system. Management reference. Add using system. management before the Code;

Add reference? No, not possible? His methods are described as follows:

In vs.net 2003 (my favorite version, of course, is too slow for my machine to use a higher version)

Project-> Add reference->. net search-> system. Management-> click the "select" button-> click the "OK" button.

 

C ++ method, this method has the least information to search for. It can be done all at once through msdn, all of which are the above Code. I want to modify it to get the hard drive serial number.

Hresult hres;

// Step 1 :--------------------------------------------------
// Initialize com .------------------------------------------

Hres = coinitializeex (0, coinit_multithreaded );
If (failed (hres ))
{
Cout <"failed to initialize com library. Error code = 0x"
<Hex Return; // program has failed.
}

// Step 2 :--------------------------------------------------
// Set general com security levels --------------------------
// Note: if you are using Windows 2000, you need to specify-
// The default authentication credentials for a user by using
// A sole_authentication_list structure in the pauthlist ----
// Parameter of coinitializesecurity ------------------------

Hres = coinitializesecurity (
Null,
-1, // com Authentication
Null, // authentication services
Null, // Reserved
Rpc_c_authn_level_default, // Default Authentication
Rpc_c_imp_level_impersonate, // default impersonation
Null, // authentication info
Eoac_none, // additional capabilities
Null // Reserved
);


If (failed (hres ))
{
Cout <"failed to initialize security. Error code = 0x"
<Hex Couninitialize ();
Return; // program has failed.
}

// Step 3 :---------------------------------------------------
// Obtain the initial locator to WMI -------------------------

Iwbemlocator * ploc = NULL;

Hres = cocreateinstance (
Clsid_wbemlocator,
0,
Clsctx_inproc_server,
Iid_iwbemlocator, (lpvoid *) & ploc );
 
If (failed (hres ))
{
Cout <"failed to create iwbemlocator object ."
<"Err code = 0x"
<Hex Couninitialize ();
Return; // program has failed.
}

// Step 4 :-----------------------------------------------------
// Connect to WMI through the iwbemlocator: connectserver Method

Iwbemservices * psvc = NULL;
 
// Connect to the root/cimv2 namespace
// The current user and obtain pointer psvc
// To make iwbemservices CILS.
Hres = ploc-> connectserver (
_ Bstr_t (L "root // cimv2"), // Object Path of WMI namespace
Null, // user name. null = current user
Null, // user password. null = Current
0, // locale. null indicates current
Null, // Security flags.
0, // Authority (e.g. Kerberos)
0, // context object
& Psvc // pointer to iwbemservices proxy
);

If (failed (hres ))
{
Cout <"cocould not connect. Error code = 0x"
<Hex Ploc-> release ();
Couninitialize ();
Return; // program has failed.
}

Cout <"connected to root // cimv2 WMI namespace" <Endl;

// Step 5 :--------------------------------------------------
// Set security levels on the proxy -------------------------

Hres = cosetproxyblanket (
Psvc, // indicates the proxy to set
Rpc_c_authn_winnt, // rpc_c_authn_xxx
Rpc_c_authz_none, // rpc_c_authz_xxx
Null, // server principal name
Rpc_c_authn_level_call, // rpc_c_authn_level_xxx
Rpc_c_imp_level_impersonate, // rpc_c_imp_level_xxx
Null, // client identity
Eoac_none // proxy capabilities
);

If (failed (hres ))
{
Cout <"cocould not set proxy blanket. Error code = 0x"
<Hex Psvc-> release ();
Ploc-> release ();
Couninitialize ();
Return; // program has failed.
}

// Step 6 :--------------------------------------------------
// Use the iwbemservices pointer to make requests of WMI ----

// For example, get the name of the Operating System
Ienumwbemclassobject * penumerator = NULL;
Hres = psvc-> execquery (
Bstr_t ("WQL "),
Bstr_t ("select * From win32_physicalmedia "),
Wbem_flag_forward_only | wbem_flag_return_immediately,
Null,
& Penumerator );

If (failed (hres ))
{
Cout <"query for operating system name failed ."
<"Error code = 0x"
<Hex Psvc-> release ();
Ploc-> release ();
Couninitialize ();
Return; // program has failed.
}

// Step 7 :-------------------------------------------------
// Get the data from the query in Step 6 -------------------
 
Iwbemclassobject * pclsobj;
Ulong ureturn = 0;

While (penumerator)
{
Hresult hR = penumerator-> next (wbem_infinite, 1,
& Pclsobj, & ureturn );

If (0 = ureturn)
{
Break;
}

Variant vtprop;
Variantinit (& vtprop );

// Get the value of the Name property
HR = pclsobj-> get (L "serialnumber", 0, & vtprop, 0, 0 );
Wcout <"OS name:" <vtprop. bstrval <Endl;
Variantclear (& vtprop );
}

// Cleanup
// ==========

Psvc-> release ();
Ploc-> release ();
Penumerator-> release ();
Pclsobj-> release ();
Couninitialize ();

 

I hope everyone will take actions to show their achievements. Let's take a look at the snow forum to learn about our ignorance. That day, the hero wrote his own operating system and tried to see the hero as soon as possible!

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.