5

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.

jecjackal
  • 1,407
  • 2
  • 20
  • 35

3 Answers3

7

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.)

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
  • You were absolutely correct. My files were being stored in the virtual store (well most of them anyway. I had a subfolder that wasn't being stored virtually for some reason). I added the manifest so my program is now running virtually. Thanks. – jecjackal Jan 10 '11 at 04:00
2

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • AFAIK you can't request admin rights for an already running process but have to start another process to perform the operations requiring those permissions. – Frank Osterfeld Jan 09 '11 at 18:24
2

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.

rohanpm
  • 4,264
  • 17
  • 16
  • Wow, you were right. I was checking the error code before opening the file. Well thats what happens when I stay up late coding :). Thanks. – jecjackal Jan 10 '11 at 04:01