How to obtain the serial number of an SD card in ce6.0

Source: Internet
Author: User

I often see posts about Software Encryption in the forum. Pure Software Encryption and read hardware serial number encryption are often discussed.

Both methods have their own advantages and disadvantages.

The method of encryption by reading the hardware serial number is limited by hardware.

Generally, the CPU and T-flash may have serial numbers. Today, I have studied how to read the SD card (t-flash) in Windows CE 6.0. Below I will list my implementation process for anyone who needs it.

Function declaration, in the. h file:

# Define sd_part_name l "dsk2 :"
# Define valid_sd_serial_1 l "a7dfb784"

Bool getstorageid (tchar * ptccardname, tchar * ptcmanufactureid, tchar * ptcserialnum );

The source code is as follows:

BOOL GetStorageID(TCHAR *ptcCardName,TCHAR *ptcManufactureID,TCHAR *ptcSerialNum){    DWORD dwSize = 0;    DWORD dwReqSize = 0;    STORAGE_IDENTIFICATION StoreInfo;    STORAGE_IDENTIFICATION StoreInfo2;    HANDLE hVolume = NULL;    BOOL bRet = FALSE;    BYTE *pucSerialNo = NULL;    BYTE *pucManuID = NULL;    int i = 0;    ZeroMemory(&StoreInfo,sizeof(STORAGE_IDENTIFICATION));    hVolume = CreateFile(ptcCardName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);      if(NULL == hVolume || INVALID_HANDLE_VALUE == hVolume)      {        // MessageBox(L"Open Partation failed!");        RETAILMSG(1,(L"Open Partation failed!\r\n"));        return FALSE;    }    bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,        NULL,0,(LPVOID)&StoreInfo,/*sizeof(STORAGE_IDENTIFICATION)*/3000,&dwSize,NULL);    if(!bRet)    {        DWORD dwErr = GetLastError();        // TCHAR tcError[64];        // wsprintf(tcError,L"Device IO 1 failed: %d!",dwErr);        // MessageBox(tcError);        RETAILMSG(1,(L"Device IO 1 failed: %d!\r\n",dwErr));        CloseHandle(hVolume);        return FALSE;    }    dwReqSize = StoreInfo.dwSize;    ASSERT(dwReqSize > 0);    dwSize = 0;    StoreInfo2.dwSize = dwReqSize;    bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,        NULL,0,(LPVOID)&StoreInfo,dwReqSize,&dwSize,NULL);       if(FALSE == bRet)    {        DWORD dwErr = GetLastError();        // TCHAR tcError[64];        // wsprintf(tcError,L"Device IO 2 failed: %d!",dwErr);        // MessageBox(tcError);        RETAILMSG(1,(L"Device IO 2 failed: %d!\r\n",dwErr));        CloseHandle(hVolume);        return FALSE;    }    pucSerialNo = (((BYTE *)&StoreInfo) + StoreInfo.dwSerialNumOffset);    pucManuID = (((BYTE *)&StoreInfo) + StoreInfo.dwManufactureIDOffset);    while(pucSerialNo[i] != 0 && i < 200 && i < (int)(dwSize - StoreInfo.dwSerialNumOffset))    {        ptcSerialNum[i] = pucSerialNo[i];        i++;    }    pucSerialNo[i] = '\0 ';    i = 0;    while(pucManuID[i] != 0 && i < 200 && i < (int)(StoreInfo.dwSerialNumOffset - StoreInfo.dwManufactureIDOffset))    {        ptcManufactureID[i] = pucManuID[i];        i++;    }    pucManuID[i] = '\0 ';    CloseHandle(hVolume);    return TRUE; }

Call example:

Tchar tcsdserial [256]; tchar tcsdmanu [256]; zeromemory (tcsdserial, sizeof (tchar) * 256); zeromemory (tcsdmanu, sizeof (tchar) * 256 ); if (0 = getstorageid (sd_part_name, tcsdmanu, tcsdserial) {MessageBox (tcsdserial); If (0 = wcsncmp (valid_sd_serial_1, tcsdserial, wcslen (callback ))) {} else {} encountered the following error during implementation: (1) when the call code is modified to: If (getstorageid (L "dsk1:", csmanufactureid, csserialid )) (dsk1 exists) produces the following error: Error 50: requests are not supported. (2) When the deviceiocontrol () function in the implementation code is modified to the following: Bret = deviceiocontrol (hvolume, ioctl_disk_get_storageid, null, 0, (lpvoid) & storeinfo, sizeof (storage_identification ), & dwsize, null); error 122: The data area passed to the system call is too small. Therefore, it is recommended that you determine the return value of the function whenever possible during encoding. In the error status, you must call the getlasterror () function to obtain the detailed error code.

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.