jecjackal
jecjackal

Reputation: 1437

Qt cannot cannot create/write to C:\

I am writing a Qt program (4.7 for windows 7 initially) that requires writing to the installed directory (C:\Program Files...). No files are being created when I try to write to a location that would be "protected" (program files, C:\ etc). However, QFile is not giving me any error code (error() is returning 0 which means it worked fine).

Here is a code snippit that I am using that is not working. I am closing the file its just much later in the program.

QApplication a(argc, argv);

// Setting plugin paths.
QStringList paths = QCoreApplication::libraryPaths();
paths.append(QCoreApplication::applicationDirPath());
QCoreApplication::setLibraryPaths(paths);



// Debug file.
QString path = QCoreApplication::applicationDirPath() + "/debug.dat";
//QFile debugFile(QCoreApplication::applicationDirPath() + "/debug.dat");
QFile debugFile("C:/debug.txt");
qDebug() << debugFile.error();
debugFile.setPermissions(QFile::WriteUser | QFile::WriteGroup | QFile::WriteOwner | QFile::WriteOther);
debugFile.open(QFile::WriteOnly);
QTextStream debugStream(&debugFile);

// Processing the arguments.
debugStream << QString("Processing Arguments\n");

Does anyone have any tips on how to solve this problem?

Thanks for the help,

Jec


Adding a manifest file is the route I choose to fix this problem.

Thanks for all of the help.

Upvotes: 5

Views: 6480

Answers (3)

rohanpm
rohanpm

Reputation: 4274

QFile may be giving you an error code, but you've failed to check for it.

You should do something more like:

if (!debugFile.open(QFile::WriteOnly)) {
    qWarning() << "Failed to open" << debugFile.fileName() << "for write:" << debugFile.errorString();
}

You've checked the return value of QFile::error, but only before calling open - you need to check after the open attempt.

Upvotes: 2

Mihai Limbășan
Mihai Limbășan

Reputation: 67826

Have you checked whether the file isn't created in the VirtualStore for that user? Check the Event Viewer under Applications and Services Logs -> Microsoft -> Windows -> UacFileVirtualization -> Operational. If you see entries with event ID 5000, a FileCreateVirtualExclude event has occurred.

Check if the file didn't get created under %USERPROFILE%\AppData\Local\VirtualStore. If it did, you might need to embed a manifest requesting the required privileges (i.e., turning virtualization off.)

For more details, see New UAC Technologies for Windows Vista (scroll down and look for Virtualization.)

Upvotes: 7

rubenvb
rubenvb

Reputation: 76785

You need to acquire sufficient user access rights (ie "Run as Administrator") to write to such folders in Windows Vista+. Either start the app as administrator, or ask for Administrator rights via a call to WinAPI.

Upvotes: 2

Related Questions