0

我对 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;
}
4

1 回答 1

1

谢谢你们的投入。我最终想出了如何通过执行以下操作来解决问题:

使用类型 LPCSTR 作为文件名和 hRawDisk

 LPCSTR volumename = "\\\\.\\G:";
 LPCSTR filename = "c:\\somedirectory\\someimage.img";

我创建了单独的函数来创建句柄,它们被调用使用:

hVolume = getHandleOnVolume(wszVolume, GENERIC_WRITE);
hFile = getHandleOnFile(filename, GENERIC_READ);

正在为文件和卷使用类似的东西创建句柄。

HANDLE getHandleOnFile(LPCTSTR filename, DWORD access){
    HANDLE hFile;
        hFile = CreateFile(filename, access, 0, NULL, (access == GENERIC_READ) ? OPEN_EXISTING:CREATE_ALWAYS, 0, NULL);

        if (hFile == INVALID_HANDLE_VALUE)
        {
            printLastError();
            return 0;

        }
        //delete filelocation;
        //CloseHandle(hFile);
        printf("handle hFile created successfully \n");
        system("PAUSE");
        return hFile;

    }

我对音量重复了同样的操作。之后我调用 DeviceIOControl 来锁定和卸载设备以进行写入。

于 2012-06-04T18:24:53.920 回答