CreateFile() Zeichenproblemme



  • hast du recht es soll "\\\.\" sein.

    Ich bin echt verzweifelt, ich weiss nicht was ich falsch mache. Hier ist mein Code:

    bool USBTest::isEnoughtMemory()
    {
    /*
    HANDLE hDevice = INVALID_HANDLE_VALUE; // handle to the drive to be examined
    BOOL bResult; // results flag
    DWORD junk; // discard results
    */
    //char driveL[14];
    //TCHAR comPrefix[5];
    //TCHAR comPrefix ="\\\.\";
    //LPCSTR driveL;
    //driveL = driveLetter;
    //LPCSTR path = &comPrefix + &driveL;

    // memset(driveLetter,0,4);
    //sprintf(driveLetter,_T("\\\.\")

    //strcpy(driveL,"\\\.\");

    //strncat(driveL,driveLetter,4);

    DISK_GEOMETRY pdg; // disk drive geometry structure
    HANDLE hDevice = INVALID_HANDLE_VALUE; // handle to the drive to be examined
    BOOL bResult; // results flag
    DWORD junk; // discard results

    //Open a USB stick
    hDevice = CreateFile("\\\.\\F:", // pointer to the USB stick //pszDrive
    GENERIC_READ, // no access to the drive
    FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode
    NULL, // default security attributes
    OPEN_EXISTING, // disposition
    0, // file attributes
    0); // do not copy file attributes

    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    {
    printf("CreateFile() faild, error %d\n",GetLastError());
    return (FALSE);
    }

    bResult = DeviceIoControl(hDevice, // device to be queried
    IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
    NULL, 0, // no input buffer
    &pdg, sizeof(&pdg), // output buffer
    &junk, // # bytes returned
    (LPOVERLAPPED) NULL); // synchronous I/O

    CloseHandle(hDevice);

    return (bResult);
    }

    ich definiere in meiner Klasse eine char driveLetter[4] variable die den Laufwerkbuchstabe enthält("F:"). Ich will die der Methode CreateFile übergeben, dafür aber müssen noch die "\\\.\" davor. Ich habe gerade direkt mit einem string ausprobiert "\\\.\\F:" ging aber auch nicht,ich weiss nicht was ich falsch mache.



  • Wo hast du denn nachgelesen, dass du es so (" \\\\.\\F: ") machen sollst?



  • eigentlich steht das niergendwo. Diese Methode hat bei mir aber früher funktioniert und durch ausprobieren, habe ich rausgefunden, dass davor noch "\\\.\" stehen soll.



  • my Fehler hängt irgendwie von unicode oder multibyte programmieren.

    Mein Programm funktioniert wenn ich schreibe:

    #define pszDrive L"\\\\.\\E:"
    
    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes
    	//char driveL[9] = "\\\\\\\\.\\\\";
    
    	//Get a path to a USB stick
    	//strcpy(driveL,"\\\\\\\\.\\\\");
    	//strncat(driveL,driveLetter,4);
    
    	//Open  a USB stick
    	hDevice = CreateFile((LPCWSTR)pszDrive,  // pointer to the USB stick //pszDrive
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	return (bResult);
    }
    

    Wenn ich aber pszDrive auf driveL wechsele und die Kommentare wegnehme bei:

    strcpy(driveL,"\\\\\\\\.\\\\");
    strncat(driveL,driveLetter,4);
    

    funktioniert es nicht.

    Kann mir jemand helfen?



  • Schalte dein Programm einfachheitshalber auf MBCS um. Du benutzt vermutlich Visual Studio? In dem Fall öffne die Projekteinstellungen und stelle unter Allgemein=>Zeichensatz "Multi-Byte-Zeichensatz" ein.

    strcpy(driveL,"\\\\\\\\.\\\\");
    

    Wieso denn jetzt plötzlich doppelt so viele Backslashes?? Wenn schon, dann \\\\.\\ , wie bisher auch.

    P.S.: wenn du bei Unicode bleiben wollen würdest, wäre der richtige Typ wchar_t* statt char* (oder gleich TCHAR*) und die L-Notation (oder gleich _T()-Makro).



  • wie can ich einen:

    char driveletter[4];
    TCHAR letter[4];

    in TCHAR konvertieren. Ich habe mir die Methode rausgefunden:

    MultiByteToWideChar(CP_ACPO,0,driveLetter,-1,letter,sizeof(letter));

    anscheind benutze ich die falsch. Was mache ich falsch? Danke erstmal für die Hilfe.



  • Wieso konvertieren? Benutze direkt TCHAR!

    Wie gesagt, wenn du dir das Leben erst mal einfach machen willst, verzichte auf TCHAR und schalte dein Projekt von Unicode auf MBCS um.



  • Dieser Thread wurde von Moderator/in rüdiger aus dem Forum Rund um die Programmierung in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • na ja, ich muss in meinem Projekt Unicode benutzen. Ich habe schon das Program an Unicode angepasst. Ich habe keinen Fehler wenn ich mit define Path auslese:

    #define pszDrive "\\\\.\\E:"
    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes
    	//TCHAR driveL[9] = _T("\\\\\\\\.\\\\");
        char driveL[10] = "\\\\\\\\.\\\\";
    	TCHAR path[10];
    //	MultiByteToWideChar(CP_ACP,0,driveLetter,-1,letter,sizeof(letter));
    
    	//Get a path to a USB stick
    	strcpy(driveL,"\\\\\\\\.\\\\");
    	strncat(driveL,driveLetter,4);
    	// _tcsncpy(letter,driveL,4);
    
    	MultiByteToWideChar(CP_ACP,0,driveL,-1,path,sizeof(path));
    
    	//Open  a USB stick
    	hDevice = CreateFileA((LPCSTR)pszDrive,  // pointer to the USB stick //pszDrive//(LPCWSTR)pszDrive
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	return (bResult);
    }
    

    sobald ich direkt TCHAR benutze, kriege ich Fehler:

    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes
    	//TCHAR driveL[9] = _T("\\\\\\\\.\\\\");
        char driveL[10] = "\\\\\\\\.\\\\";
    	TCHAR path[10];
    //	MultiByteToWideChar(CP_ACP,0,driveLetter,-1,letter,sizeof(letter));
    
    	//Get a path to a USB stick
    	strcpy(driveL,"\\\\\\\\.\\\\");
    	strncat(driveL,driveLetter,4);
    	// _tcsncpy(letter,driveL,4);
    
    	MultiByteToWideChar(CP_ACP,0,driveL,-1,path,sizeof(path));
    
    	//Open  a USB stick
    	hDevice = CreateFileA((LPCSTR)path,  // pointer to the USB stick //pszDrive//(LPCWSTR)pszDrive
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	return (bResult);
    }
    

    weiss jemand warum?



  • ok, ich habe das Problem gelöst. Richtig wäre

    bool USBStick::checkMemory()
    {
    	DISK_GEOMETRY pdg;            // disk drive geometry structure
    	HANDLE hDevice = INVALID_HANDLE_VALUE;               // handle to the drive to be examined 
    	BOOL bResult;                 // results flag
    	DWORD junk;                   // discard results
    	ULONGLONG DiskSize;           // size of the drive, in bytes
    	//TCHAR driveL[9] = _T("\\\\\\\\.\\\\");
        char driveL[10] = "\\\\.\\";
    	TCHAR path[10] ;//= _T("\\\\.\\E:");
    //	MultiByteToWideChar(CP_ACP,0,driveLetter,-1,letter,sizeof(letter));
    
    	//Get a path to a USB stick
    	//strcpy(driveL,"\\\\\\\\.\\\\");
    	strncat(driveL,driveLetter,4);
    	// _tcsncpy(letter,driveL,4);
    
    	MultiByteToWideChar(CP_ACP,0,driveL,-1,path,sizeof(path));
    
    	//Open  a USB stick
    	hDevice = CreateFile(path,  // pointer to the USB stick //pszDrive//(LPCWSTR)pszDrive
    		GENERIC_READ,                // no access to the drive
    		FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode		
    		NULL,             // default security attributes
    		OPEN_EXISTING,    // disposition
    		0,                // file attributes
    		0);            // do not copy file attributes
    
    	if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
    	{
    		printf("CreateFile() faild, error %d\n",GetLastError());
    		return (FALSE);
    	}
    
    	bResult = DeviceIoControl(hDevice,  // device to be queried
    		IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
    		NULL, 0, // no input buffer
    		&pdg, 
    		sizeof(pdg),     // output buffer
    		&junk,                 // # bytes returned
    		(LPOVERLAPPED) NULL);  // synchronous I/O
    
    	CloseHandle(hDevice);
    
    	if (bResult) 
    	{
    
    		DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
    			(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    		printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
    			DiskSize / (1024 * 1024 * 1024));
    	} 
    	else 
    	{
    		printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
    	}
    
    	return (bResult);
    }
    

Anmelden zum Antworten