【发布时间】:2012-06-05 02:24:53
【问题描述】:
我对 C++ 很陌生。我正在开发一个 Java 应用程序,该应用程序对 C++ 进行 Jni 调用以将原始文件 (.img) 写入附加的紧凑型闪存卡写入器。下面是我的 c++ 代码,假设找到一个连接的 USB 驱动器,使用 createFile 创建一个句柄并将原始图像(.img)写入设备。最终应用程序将使用 Java JNI 调用。
现在遇到的问题是,能够列出连接的驱动器但有问题 使用 createFile() 为它们创建一个句柄。我收到以下错误:
Error 1 error C2660: 'getHandleOnVolume' : function does not take 2 arguments c:\users\omisorem.nov\documents\visual studio 2010\projects\diskrunner\diskrunner.cpp 71 1 DiskRunner
2 IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR" c:\users\omisorem.nov\documents\visual studio 2010\projects\diskrunner\diskrunner.cpp 86 23 DiskRunner
感谢任何帮助。
int main() {
// GetLogicalDrives returns 0 on failure, or a bitmask representing
// the drives available on the system (bit 0 = A:, bit 1 = B:, etc)
unsigned long driveMask = GetLogicalDrives();
int i = 0;
ULONG pID;
//cboxDevice->clear();
while (driveMask != 0)
{
if (driveMask & 1)
{
// the "A" in drivename will get incremented by the # of bits
// we've shifted
char drivename[] = "\\\\.\\A:\\";
drivename[4] += i;
cout << pID << "[%1:\\]" << drivename << endl;
}
driveMask >>= 1;
// cboxDevice->setCurrentIndex(0);
++i;
}
LPCTSTR volumeID = TEXT("D:");
int volumeID1 = int(volumeID);
hVolume = getHandleOnVolume(volumeID1, GENERIC_WRITE);
system("PAUSE");
return 0;
}
HANDLE getHandleOnVolume(int volume1, DWORD access)
{
HANDLE hVolume;
char volumename[] = "\\\\.\\A:";
volumename[4] += volume1;
hVolume = CreateFile("\\\\.\\F", access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hVolume == INVALID_HANDLE_VALUE)
{
cout <<"Partition does not exist or you don\'t have rights to access it"<<endl; // tesing
}
else {
cout << "Partition exists " << endl;
}
cout << volume1;
return hVolume;
}
【问题讨论】:
-
为什么不使用纯 Java 方法?它具有完美的文件 IO 支持,尤其是从 Java 7 开始。
-
谢谢。从我所做的小研究来看,Java 似乎不提供对此类低级调用的支持,除非使用已过时且与 Windows 7 不兼容的 JUSB 或 Java-USB API。但是,如果你能指出我,我将不胜感激如果可能的话,在纯 java 方向。
标签: java c++ handle createfile