1

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.

Community
  • 1
  • 1
tpwynn
  • 60
  • 2
  • 5
  • Putting such a constraint on a program seems weird to me. Could we know what you are trying to achieve ? – ereOn May 03 '12 at 06: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. – tpwynn May 03 '12 at 06:37
  • possible duplicate of [Preventing copy protection circumvention](http://stackoverflow.com/questions/203229/preventing-copy-protection-circumvention) – Bo Persson May 03 '12 at 06:42
  • @BoPersson, did you copy the wrong link? That question is not a duplicate at all. – Mark Ransom May 03 '12 at 16:03

1 Answers1

2

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());
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • 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.` – tpwynn May 04 '12 at 02:02