tpwynn
tpwynn

Reputation: 60

How to Restrict MFC Application to run only from a known USB flash drive

I want to make a MFC Application that can only run form a know USB flash drive. It should not be run if we copy to the other place. I found question something like this at here But i don't really understand. Please show me a hint.

Upvotes: 1

Views: 682

Answers (1)

Software_Designer
Software_Designer

Reputation: 8587

In MFC: GetFileInformationByHandle

 BY_HANDLE_FILE_INFORMATION info;
DWORD dwSerialNumber = 0;

if(GetFileInformationByHandle(FileHandle, &info) != 0)
{
    dwSerialNumber = info.dwVolumeSerialNumber;
    swprintf(szTemp, L"The Volume Serial Number = %d", info.dwVolumeSerialNumber);
    MessageBox(NULL, szTemp, L"Success", MB_OK);
}
else
{
    swprintf(szTemp, L"GetFileInformationByHandle Error = %d", GetLastError());
    MessageBox(NULL, szTemp, L"Success", MB_OK);
}

In C#/C++.NET: Use WMI the internal serial number of a USB-drive.

Try ths code , if there is no serial number, it is becuse some USB flash drives do have them, some don't.

//import the System.Management namespace at the top in your "using" statement. Then in a method, or on a button click:

ManagementObjectSearch theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject currentObject in theSearcher.Get())
{
   ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
   MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());
}

Upvotes: 2

Related Questions