Take the 2-minute tour ×

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.

share|improve this question
Putting such a constraint on a program seems weird to me. Could we know what you are trying to achieve ? –  ereOn May 3 '12 at 6:34
I am trying to put the application to an usb flash drive. It is just like plug and play application. No need to install. But it should only be run form certain usb drive. Just need to protect the application copying form the usb and run form the everywhere. –  tpw May 3 '12 at 6:37
possible duplicate of Preventing copy protection circumvention –  Bo Persson May 3 '12 at 6:42
@BoPersson, did you copy the wrong link? That question is not a duplicate at all. –  Mark Ransom May 3 '12 at 16:03

1 Answer 1

up vote 2 down vote accepted

In MFC: GetFileInformationByHandle

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);
    swprintf(szTemp, L"GetFileInformationByHandle Error = %d", GetLastError());

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"] + "'");
share|improve this answer
How can I get FileHandle? I have tried something like this. ` 'LPCSTR szBuf = "F:\\"; HANDLE FileHandle = CreateFile(szBuf, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);' I got error 6.` –  tpw May 4 '12 at 2:02

Your Answer


