CopyFile kopiert nicht - GetLastError() -> "The handle is invalid"



  • Hallo, da bin ich mal wieder 😃
    Ich wollte mein Programm eigentlich nur in den Autostart-Ordner kopieren, was sich als schwieriger erweist, als gedacht; CopyFile (und andere, selbstgeschriebene Kopier-Funktionen von Forenmitgliedern) kopieren nicht, obwohl der Compiler keine Fehlermeldung ausspuckt.

    GetLastError() gibt mir immer "..failed with error 6: The handle is invalid" zurück.
    Hier der Code:

    SHGetSpecialFolderLocation(0, CSIDL_STARTUP, &ItemID); //Pfad des Startup-Ordners holen
      SHGetPathFromIDList(ItemID, Buffer);
    
      AnsiString ZielOrdner = AnsiString(Buffer);
    
      char Quelldatei[_MAX_PATH];
      GetModuleFileName(NULL,Quelldatei,sizeof(Quelldatei)); //Pfad meines Programms holen
    
      CopyFile(Quelldatei, ZielOrdner.c_str(),true);
    

    Hab vorher mit if(FileExists(Quelldatei)) ShowMessage("Datei existiert!"); schon erfolgreich getestet, ob der richtige Dateipfad gefunden wurde. Habe das Programm auf WinXP und 7 mit Admin-Rechten probiert.

    Bis bald



  • Der zweite Parameter bei CopyFile heisst nicht Zielordner, sondern Zieldatei, sprich gibt den neuen Dateipfad inkl. Dateiname an.

    char Buffer[MAX_PATH];
    LPITEMIDLIST ItemID;
    SHGetSpecialFolderLocation(0, CSIDL_STARTUP, &ItemID); 
    SHGetPathFromIDList(ItemID, Buffer);
    
    AnsiString ZielOrdner = AnsiString(Buffer) + "\\programm.exe";
    
    //char Quelldatei[_MAX_PATH];
    //GetModuleFileName(NULL,Quelldatei,sizeof(Quelldatei)); 
    AnsiString Quelldatei = Application->ExeName;  
    
    CopyFile(Quelldatei.c_str(), ZielOrdner.c_str(),false);
    


  • ..dass das so ist konnte ich der Hilfe irgendwie nicht entnehmen (und auch vielen Forenbeiträgen, wo CopyFile benutzt wurde nicht). Vielen Dank!



  • Ich weiss nicht, wie es in der deutschen Hilfe beschrieben ist, aber in der englischen ist es eindeutig:

    BOOL CopyFile(

    LPCTSTR lpExistingFileName, // pointer to name of an existing file
    LPCTSTR lpNewFileName, // pointer to filename to copy to
    BOOL bFailIfExists // flag for operation if file exists
    );

    Parameters

    lpExistingFileName
    Points to a null-terminated string that specifies the name of an existing file.

    lpNewFileName
    Points to a null-terminated string that specifies the name of the new file.

    bFailIfExists
    Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.


Anmelden zum Antworten