C++创建文件(紧凑型闪存卡写入器)

C++ createFile (Compact Flash Card Writer)

本文关键字:闪存卡 紧凑型 创建 文件 C++      更新时间:2023-10-16

我对C++很陌生。我正在开发一个 Java 应用程序,该应用程序会调用 Jni 来C++将原始文件 (.img) 写入连接的紧凑型闪存卡写入器。下面是我的 c++ 代码,假设找到连接的 USB 驱动器,使用 createFile 创建一个句柄并将原始图像 (.img) 写入设备。最终,应用程序将使用Java JNI调用。

我现在遇到的问题是,我能够列出连接的驱动器但有问题使用 createFile() 为它们创建一个句柄。我收到以下错误:

Error 1 error C2660: 'getHandleOnVolume' : function does not take 2 arguments   c:usersomisorem.novdocumentsvisual studio 2010projectsdiskrunnerdiskrunner.cpp   71  1   DiskRunner
      2 IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"    c:usersomisorem.novdocumentsvisual studio 2010projectsdiskrunnerdiskrunner.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;
}

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

使用类型 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来锁定和卸载设备以进行写入。