hdd serial number cu wmi sub vista

Intrebari legate de programarea cu biblioteci precum MFC, ATL, WTL si GDI+.
mesajflaviu
Membru++
Membru++
Posts: 687
Joined: 10 Sep 2008, 21:40
Judet: Cluj

Re: hdd serial number cu wmi sub vista

Post by mesajflaviu » 16 Jun 2010, 21:46

4. mergi la offsetul serial

Cod: Selectaţi tot
STORAGE_DEVICE_DESCRIPTOR *descrip = (STORAGE_DEVICE_DESCRIPTOR *)&pbinfo[0];
char* ssn = (char *)&pbinfo[descrip->SerialNumberOffset];

Aici o sa fie un string ascii hexa, reversed probabil.
La mine e "020202027502d244857593548303b4c493536303".
Din hexa iese " W -DXW9E80KL9560", stringul e reversed, " WD-WXE908LK5906", trim stringa/dreapta si finally iese "WD-WXE908LK5906".
Cum formatez ( intr-un CString ) ssn-ul sa vad corect serialul HDD ?



User avatar
cristianamarie
Membru++
Membru++
Posts: 480
Joined: 12 Mar 2009, 18:47
Judet: Iaşi
Location: Iasi

Re: hdd serial number cu wmi sub vista

Post by cristianamarie » 17 Jun 2010, 11:34

mesajflaviu wrote:
4. mergi la offsetul serial

Cod: Selectaţi tot
STORAGE_DEVICE_DESCRIPTOR *descrip = (STORAGE_DEVICE_DESCRIPTOR *)&pbinfo[0];
char* ssn = (char *)&pbinfo[descrip->SerialNumberOffset];

Aici o sa fie un string ascii hexa, reversed probabil.
La mine e "020202027502d244857593548303b4c493536303".
Din hexa iese " W -DXW9E80KL9560", stringul e reversed, " WD-WXE908LK5906", trim stringa/dreapta si finally iese "WD-WXE908LK5906".
Cum formatez ( intr-un CString ) ssn-ul sa vad corect serialul HDD ?
ssn e char*, una la mina, deci trebuie un CStringA.

Fac paste aici la codul (incomplet) de parsing al serial number:

Code: Select all

	static bool get(int drive, vector_t< pair_t< string_t<wchar_t>, string_t<wchar_t> > >& info) {
		if(drive < 0) {
			return false;
		}
		WCHAR szHDD[60 + 1] = L"";
		swprintf(szHDD, _countof(szHDD) - 1, L"\\\\.\\PHYSICALDRIVE%d", drive);
		HANDLE hDevice = ::CreateFileW(szHDD, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
		if(hDevice == INVALID_HANDLE_VALUE) { 
			return false;
		}
		bool result = false;
		BYTE pbinfo[16384] = {0};
		DWORD unused = 0; 
		STORAGE_PROPERTY_QUERY query;
		memset(&query, 0, sizeof(STORAGE_PROPERTY_QUERY)); 
		query.QueryType = PropertyStandardQuery;
		query.PropertyId = StorageDeviceProperty;
		if(::DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), 
			&pbinfo[0], _countof(pbinfo), &unused, (LPOVERLAPPED) NULL)) {
			STORAGE_DEVICE_DESCRIPTOR *descrip = (STORAGE_DEVICE_DESCRIPTOR *)&pbinfo[0];

			BYTE *pSN = ((BYTE *)(&pbinfo[0])) + descrip->SerialNumberOffset;

			ULONG ul = descrip->SerialNumberOffset; 
			char* ssn = (char *)&pbinfo[ul];
			char sn[128] = "";
			//	ssn -> sn
			int nl = strlen(ssn);// "020202027502d244857593548303b4c493536303" "....u.d244857593548303b4c493536303"
			char srsn[80] = "";
			memset(&srsn[0], 0, _countof(srsn));
			int pos = 0;
			for(int c = 0; c < nl / 2; c++) {
				char ch[3];
				ch[0] = tolower(ssn[c * 2]);
				ch[1] = tolower(ssn[c * 2 + 1]);
				ch[2] = '\0';
				int r0 = 0;
				if(ch[0] >= '0' && ch[0] <= '9') { 
					r0 = ch[0] - '0';
				}
				else {
					r0 = ch[0] - 'a' + 10;
				}
				int r1 = 0;
				if(ch[1] >= '0' && ch[1] <= '9') { 
					r1 = ch[1] - '0';
				}
				else {
					r1 = ch[1] - 'a' + 10;
				}

				int r = r0 * 16 + r1;
				char cr[2] = { (char)(r), '\0' };
				::OutputDebugStringA(cr);
				srsn[pos++] = (char)(r);
			}
			for(int c = 0; c < strlen(srsn); c += 2) {
				char ch;
				ch = srsn[c];
				srsn[c] = srsn[c + 1];
				srsn[c + 1] = ch;
			}

			WCHAR wvi[81] = L"";
			memset(wvi, 0, _countof(wvi) * sizeof(WCHAR));

			char* psrsn = &srsn[0];
			while(*psrsn == ' ')
				psrsn++;
			::MultiByteToWideChar(CP_ACP, 0, psrsn, lstrlenA(psrsn), wvi, _countof(wvi) - 1);

			info += pair_t<string_t<wchar_t>, string_t<wchar_t>>(
				  L"vendor-id"
				, wvi);

			result = true;
		}
		::CloseHandle(hDevice); 
		return true;
	}

Ignora partile cu pair_t, vector_t si string_t<wchar_t>, sint niste clase de-ale mele. Sau inlocuieste-le cu std::pair, std::vector si std::wstring.
Nuclear launch detected

Post Reply