0

I am pretty new to C++. I am working a Java application that makes a Jni Call to C++ to write raw file (.img) to an attached Compact Flash card writer. Below is my c++ code, it is suppose to locate a connected usb drive, create a Handle using createFile and write a raw image (.img) to the device. Eventually the application would called using Java JNI.

The issue am having now is that, am able to list the attached drives but having issues creating a Handle to them using createFile(). I am getting below errors :

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

Any help is appreciated.

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;
}
vyegorov
  • 21,787
  • 7
  • 59
  • 73
stdapsy
  • 154
  • 1
  • 1
  • 8
  • Why not to use a pure Java approach? It has a perfectly capable File IO support, especially since Java 7. – 01es Apr 23 '12 at 19:17
  • Thanks. from the little research i've done, Java doesnt seem to provide support for such low level calls except by using JUSB or Java-USB APIs which are outdated and not compatible with windows 7.However, I'll apprecite if you could point me in the pure java direction, if possible. – stdapsy Apr 23 '12 at 20:09

1 Answers1

1

Thank you guys for your inputs. I eventually figured out how to resolve the issue by doing the following:

using type LPCSTR for the filename and hRawDisk

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

I created separate functions to create the handles, they are called using:

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

Handles are being created using something like this for both file and volume.

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;

    }

I repeated same for the volume. After which I called DeviceIOControl to lock and unmount the device for writing.

stdapsy
  • 154
  • 1
  • 1
  • 8