Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify a PC 3

Status
Not open for further replies.

pererm

Programmer
Feb 11, 2002
27
0
0
ES
How can an aplication get a code or a number that says me which PC is using it? I try with GetCurrentHwProfile(), but the profile, i think, is not exactly "one for each PC"? Ineed help, thanks
 
It's IP address is always unique in any given network.
Code:
$ ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.0.1
There will be an API to get this somewhere, but that would need a bit of digging in the socket API.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Unfortunately, IP address is not permanent PC attribute in any given network (remember DHCP).
Get computer name or network card MAC address (it's unique for every card in the Universe;).

Alas, I don't know (don't remeber;) the simple method(s) to obtain these attributes. May be you try to adopt these two extractions from my old application:

To obtain computer name (change Info::xxx calls to save obtained computer name):
Code:
namespace {
const char compname[] = "System\\CurrentControlSet\\Control\\ComputerName\\ComputerName";
}

bool GetCompName()
{
	HKEY	hk = 0;
	char name[MAX_COMPUTERNAME_LENGTH + 1];
	DWORD	psz = sizeof name;

	if (RegOpenKeyEx(
		HKEY_LOCAL_MACHINE,
		compname,
		0,
		KEY_READ,
		&hk) == ERROR_SUCCESS)
	{
		if (RegQueryValueEx(
			hk,
			"ComputerName",
			0, 0,
			(LPBYTE)name,
			&psz
			) == ERROR_SUCCESS)
		{
			Info::printf("Computer Name",name);
		}
		else
		{
			Info::printf(0,"*** Can\'t get %s value.",compname);
			return false;
		}
		
		if (RegCloseKey(hk) != ERROR_SUCCESS)
		{
			Info::printf(0,"*** Can\t close %s Registry key.",compname);
//			Info::print();
//			exit(3);
		}
	}
	else
	{
		Info::printf("Computer Name","");	// Unknown computer name.
	}
/*
	if (GetComputerName(name,&psz))
		printf("=== GetComputername %.40s\n",name);
	else
		printf("*** GetComputerName failed.\n");
*/
return true;
}

Get (the last) network adapter MAC addr:
Code:
namespace {	// Locals

typedef struct _ASTAT_
{

  ADAPTER_STATUS adapt;
  NAME_BUFFER    NameBuff [30];

}ASTAT, * PASTAT;

ASTAT Adapter;

char MAC_addr[16];	// static MAC addr string (12 hexas).

int	errCnt = 0;		// call errors count.

}	// End of locals.

int	errMAC()
{
	return errCnt;
}

/**
 *	Using internal static storage area.
 */
const char* GetMAC()
{
	NCB Ncb;
	UCHAR uRetCode;
	unsigned char NetName[50];
	LANA_ENUM	lenum;
	int		i;

	errCnt = 0;
	memset(NetName,0,sizeof NetName);

	memset( &Ncb, 0, sizeof(Ncb) );
	Ncb.ncb_command = NCBENUM;
	Ncb.ncb_buffer = (UCHAR *)&lenum;
	Ncb.ncb_length = sizeof(lenum);
	uRetCode = Netbios( &Ncb );
	if (uRetCode)
	{
		++errCnt;
		return MAC_addr;
		//printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
	}

	for (i = 0; i < lenum.length ;i++)
	{
		memset( &Ncb, 0, sizeof(Ncb) );
		Ncb.ncb_command = NCBRESET;
		Ncb.ncb_lana_num = lenum.lana[i];

		uRetCode = Netbios( &Ncb );
		if (uRetCode)
		{
		//	printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
		//		  lenum.lana[i], uRetCode );
			++errCnt;
			continue;
		}
		memset( &Ncb, 0, sizeof (Ncb) );
		Ncb.ncb_command = NCBASTAT;
		Ncb.ncb_lana_num = lenum.lana[i];

		strcpy( (char*)Ncb.ncb_callname,  "*               " );
		Ncb.ncb_buffer = (BYTE *) &Adapter;
		Ncb.ncb_length = sizeof(Adapter);

		uRetCode = Netbios( &Ncb );
		if (uRetCode)
		{
			++errCnt;
			continue;
		//	printf("The NCBASTAT on LANA %d return code is: 0x%x \n",
		//		  lenum.lana[i], uRetCode );
		}
		else// ( uRetCode == 0 )
		{
			if (i && memcmp(NetName,Adapter.adapt.adapter_address,6) == 0)
				continue;
			else
				memcpy(NetName,Adapter.adapt.adapter_address,6);
		/*
			 printf("The Ethernet Number on LANA %d is:"
					"%02x%02x%02x%02x%02x%02x\n",
					lenum.lana[i],
		*/
		  sprintf(MAC_addr,"%02X%02X%02X%02X%02X%02X",
			  NetName[0],
			  NetName[1],
			  NetName[2],
			  NetName[3],
			  NetName[4],
			  NetName[5]);
		}
	}
	return MAC_addr;
}
Add netapi32.lib to the project.
See MSDN - codes above was written (many years ago;) with MSDN help...
May be it helps.
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top