0

I wrote this simple console program (writeTxt.exe):

#include "stdafx.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char        *fileName = "test.txt";
    ofstream    outStream(fileName, ios::out);

    outStream << "This is a very simple test." << endl;

    return 0;
}

Then I run it on the console of Windows Server 2008 using runas command (I logged-in as a different user from User1):

runas /user:User1 writeTxt.exe

But the program doesn't produce the file test.txt. If I log-in to the server as User1 and run the program, it works correctly. Do I have to set something for this to run correctly?

ArthurN
  • 179
  • 5
  • 15

1 Answers1

1

I believe that runas always launches programs with their working directory set to C:\Windows\System32 (or moral equivalent) rather than whatever your current working directory is when you invoke runas.

If User1 has permissions to write to that directory, that's where the file will be. If they don't have such a permission, then the program will fail.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I don't find a way to set the permissions of `C:\Windows\System32` in Windows Server 2008 explicitly (using `Properties -> Security` tab), even as administrator. So I set the `User1` account as administrator account (which I assume has a write permission to `C:\Windows\System32`) and tried this, but the program still doesn't produce `test.txt`. – ArthurN Sep 25 '13 at 08:25
  • 1
    @ArthurN - I could have been more expansive in my answer - rather than trying to let `User1` write to that directory, I'd change the program so that it doesn't attempt to write to it's current working directory (at least, not without setting it to something else) at all - or pass a complete path as an argument to the program. – Damien_The_Unbeliever Sep 25 '13 at 08:29
  • Thanks! Setting the full path name explicitly in the code solves this problem. – ArthurN Sep 25 '13 at 08:45