Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
// Mac Address.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <string>
#include<iostream>
#include <fstream>
#include <lm.h>
#include <assert.h>
#include <winsock.h>
#define _WIN32_DCOM
#include <iostream>
#include <comdef.h>
#include <wbemidl.h>
#include <comutil.h>

#pragma comment(lib, "Netapi32.lib")
#pragma comment(lib, "wsock32.lib")
# pragma comment(lib, "wbemuuid.lib")

using namespace std;


void filewrite(string a,int e)
{
	ofstream fout;
	if(e==1)
	fout.open("System Configuration.txt");
	else
	{
		fout.open("System Configuration.txt", ios::app);
		fout<<a;
		fout<<endl;
	}
	
	
	fout.close();
	
}

void getmac()
{
	unsigned char MACData[8];
	WKSTA_TRANSPORT_INFO_0 *pwkti;
	DWORD dwEntriesRead;
	DWORD dwTotalEntries;
	BYTE *pbBuffer;

	NET_API_STATUS dwStatus = NetWkstaTransportEnum(
		NULL,						// [in]  server name
		0,							// [in]  data structure to return
		&pbBuffer,					// [out] pointer to buffer
		MAX_PREFERRED_LENGTH,		// [in]  maximum length
		&dwEntriesRead,				// [out] counter of elements actually enumerated
		&dwTotalEntries,			// [out] total number of elements that could be enumerated
		NULL);						// [in/out] resume handle

	assert(dwStatus == NERR_Success);

	pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer;		// type cast the buffer

	for(DWORD i=1; i< dwEntriesRead; i++)			// first address is 00000000, skip it
	{	char dd[33];	
		// enumerate MACs and print
		swscanf((wchar_t *)pwkti[i].wkti0_transport_address, L"%2hx%2hx%2hx%2hx%2hx%2hx", 
			&MACData[0], &MACData[1], &MACData[2], &MACData[3], &MACData[4], &MACData[5]);
		//PrintMACaddress(MACData);
		sprintf(dd," %02x-%02x-%02x-%02x-%02x-%02x\n",MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
		string u="MAC Address		:";
		u+=dd;
		filewrite(u,0);
	}
	
	// Release pbBuffer allocated by above function
	dwStatus = NetApiBufferFree(pbBuffer);
	assert(dwStatus == NERR_Success);
}

void getip()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	char name[255];
	PHOSTENT hostinfo;
	wVersionRequested = MAKEWORD( 1, 1 );
	char *ip;

	if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
		if( gethostname ( name, sizeof(name)) == 0)
		{
			//printf("Host name: %s\n", name);
			string name1="Computer Name: ";
			name1+=name;
			filewrite(name1,0);

			if((hostinfo = gethostbyname(name)) != NULL)
			{
				int nCount = 0;
				while(hostinfo->h_addr_list[nCount])
				{
					ip = inet_ntoa (*(struct in_addr *)hostinfo->h_addr_list[nCount]);
					++nCount;
					//printf("IP #%d: %s\n", ++nCount, ip);
					
					
					string s="IP Adress			: ";
					s+=ip;
					filewrite(s,0);
				}
			}
		}
}
void getsn_no()
{
	 CoInitializeEx(0, COINIT_MULTITHREADED);

	 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
        );

	  IWbemLocator *pLoc = NULL;

	  CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);

	  IWbemServices *pSvc = NULL;
	   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
         );
	

		IEnumWbemClassObject* pEnumerator = NULL;
		ULONG uReturn;
		int i=1,x=0;
		LPCWSTR lp3;
		_bstr_t win32;
		string data;

while(i<=7)
{

	switch(i)
	{
	case 1:
		filewrite("MOTHER BOARD",0);
		win32="SELECT * FROM Win32_BaseBoard";
		lp3=L"SerialNumber";
		data="SerialNumber		:";
		break;
	case 2:
		win32="SELECT * FROM Win32_BaseBoard";
		lp3=L"Product";
		data="Product ID			:";
		break;
	case 3:
		win32="SELECT * FROM Win32_Processor";
		lp3=L"ProcessorId";
		data="Processor Id		:";
		x=1;
		cout<<"Configuring Software....."<<endl<<endl;
		break;
	case 4:
		 filewrite("",0);
		 filewrite("HARD-DISK & FLAS DRIVE",0);
		 win32="SELECT * FROM Win32_DiskDrive";
		 lp3=L"Model";
		 data="Model NO			: ";
		 break;
	case 5:
		 win32="SELECT * FROM Win32_DiskDrive";
		 lp3=L"InterfaceType";
		 data="InterfaceType		: ";
		 break;
	case 6:
		filewrite("",0);
		filewrite("LOCAL DISK's		SERIAL No.",0);
		win32="SELECT * FROM Win32_LogicalDisk";
		lp3=L"Name";
		x=3;
		break;
	case 7:
		filewrite("",0);
		win32="SELECT * FROM Win32_OperatingSystem";
		lp3=L"Name";
		filewrite("OS Name |PATH|  |PARTITION|",0);
		x=0;
		break;
	}
	i++;

	    pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t(win32),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

		IWbemClassObject *pclsObj;
     uReturn = 0;
	//filewrite("HD&FLASH DRIVE's           ",0);
   
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }


		VARIANT vtProp;

        // Get the value of the Name property
			
		pclsObj->Get(lp3, 0, &vtProp, 0, 0);
		data+=(const char*)_bstr_t(vtProp.bstrVal);

		if(x==3)
		{
			lp3=L"VolumeSerialNumber";
			string n;
			n=data;
			n+="			";
			pclsObj->Get(lp3, 0, &vtProp, 0, 0);
			n+=(const char*)_bstr_t(vtProp.bstrVal);
			data=n;
			lp3=L"Name";
			
		}

		filewrite(data,0);
		
        VariantClear(&vtProp);
		pclsObj->Release();
		data.clear();
		if(x==1)
		{
			x=0;
			break;
		}
	}
}
	pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();
}

int _tmain(int argc, _TCHAR* argv[])

{
	filewrite("",1);
	cout<<"Configuring Hardware....."<<endl;
	getip();
	getmac();
	getsn_no();
	cout<<endl<<"	Configuration Completed";
	cout<<endl<<endl<<endl<<"Press any key to continue...";
	getch();
	return 0;

}


every thing works fine, butt when i try to get HDD serial no by
win32_physicalmedia
get(L"serialNumber,0,&varient,0,0)
it returns NULL varinet... please help
Posted
Updated 22-Jul-12 20:53pm
v2
Comments
Richard MacCutchan 23-Jul-12 4:28am    
You have obviously dumped the wrong bit of code, there is no call to win32_physicalmedia() in the above.
Praveen S 27-Jul-12 1:43am    
@Richard...
if i try like this

case 7:

win32="SELECT * FROM Win32_DiskDrive";
lp3=L"SerialNumber";
data="Serial Number : ";
break;


it returns NULL varient
Praveen S 27-Jul-12 1:47am    
@Richard... sorry if i try like this case 7: win32="SELECT * FROM Win32_physicalMedia"; lp3=L"SerialNumber"; data="Serial Number : "; break; it returns NULL varient

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900