【问题标题】:How to Restrict MFC Application to run only from a known USB flash drive如何限制 MFC 应用程序仅从已知的 USB 闪存驱动器运行
【发布时间】:2012-05-03 21:59:34
【问题描述】:

我想制作一个只能从已知 USB 闪存驱动器运行的 MFC 应用程序。如果我们复制到其他地方,它不应该运行。 我在here 发现了类似的问题 但我真的不明白。请给我一个提示。

【问题讨论】:

  • 对程序施加这样的约束对我来说似乎很奇怪。我们能知道您想要达到的目标吗?
  • 我正在尝试将应用程序放入 USB 闪存驱动器。它就像即插即用应用程序一样。无需安装。但它只能从特定的 USB 驱动器运行。只需要保护应用程序从 USB 复制并在任何地方运行即可。
  • @BoPersson,您是否复制了错误的链接?这个问题根本不是重复的。

标签: c++ mfc


【解决方案1】:

在 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);
}

在 C#/C++.NET 中: 使用 WMI 作为 USB 驱动器的内部序列号。

试试这个代码,如果没有序列号,那是因为有些U盘有,有些没有。

//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());
}

【讨论】:

  • 如何获取 FileHandle?我试过这样的事情。 ` 'LPCSTR szBuf = "F:\\"; HANDLE FileHandle = CreateFile(szBuf, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);'我收到错误 6。`
猜你喜欢
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
相关资源
最近更新 更多