I am using Windows XP SP II and Visual Studio 2008. I've created a little program that I'd like to run when the computer boots. Here is the code:

// dummy1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "Windows.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    Sleep(5000);

    ifstream ins;
    ins.open("HelloWorld.txt");
    string textToDisplay, outFileName;
    getline(ins, outFileName);
    getline(ins, textToDisplay);
    ins.close();

    ofstream outs;
    outs.open(outFileName.c_str());
    outs << "hello\n";
    outs << textToDisplay << endl;
    outs.close();

    while (true)
    {
        cout << "hello\n";
        cout << textToDisplay << endl;
        Sleep(1000);
    }

    return 0;
}

HelloWorld.txt is a little two line text file:

HelloWorldOutput.txt
Hello World!

I have put the dummy1.exe file into the Windows registry so that it runs at startup time. i get the following results:

hello

hello

hello

etc...

No new file is created and "Hello World!" is not displayed, so it looks like the input file was not successfully opened or read from. However, when I run the program normally (i.e. double click it after the computer has already started up, it runs correctly. The new file is created and the output to the screen is:

hello
Hello World!
hello
Hello World!
hello
Hello World!
etc...

Is there some type of permissions problem? Can a program not use ifstream or ofstream in a Startup program or read or write files or something? If so, is there a way to fix that?

Thanks.

Hello World isn't displayed because nobody is logged in. And that's probably why the file doesn't get created too. try putting the *.exe in the AllUsers startup folder
C:\Documents and Settings\All Users\Start Menu\Programs\Startup

I think it will then get run after someone logges into the computer.

Hello World isn't displayed because nobody is logged in. And that's probably why the file doesn't get created too. try putting the *.exe in the AllUsers startup folder
C:\Documents and Settings\All Users\Start Menu\Programs\Startup

I think it will then get run after someone logges into the computer.

That worked Ancient Dragon. Thanks. I'm still confused about why the program ran originally, but only partially. It seems that it should either run or not run. So somehow it had enough permission to run and display text to the console screen, but apparently not enough permission to read from and write to the filesystem. I tried this both on the Desktop of an individual account and also on the C drive(which belongs to everyone, right?) and both gave the results that occurred in my last post when I access it using the following registry key:

HKEY_LOCAL_MACHINE -> SOFTWARE -> Microsoft -> Windows -> CurrentVersion -> Run

When I put it where you suggested it ran fine, including reading and writing to files. Is there a way to do this using the registry? Is this a security issue?

I don't know about the registry but I have written Windows Services programs that access the file systems because when you install a service you have to give it a login account that it will log into when it starts. Most services run in an administrative account.

Wben you just set the registry like you did the program will run but it is not running under any user account so it doesn't have permissions to do anything in the file system.

Comments
Helpful.

I don't know about the registry but I have written Windows Services programs that access the file systems because when you install a service you have to give it a login account that it will log into when it starts. Most services run in an administrative account.

Wben you just set the registry like you did the program will run but it is not running under any user account so it doesn't have permissions to do anything in the file system.

O.K. That makes sense. I'll check out Windows Services. Thanks.

This article has been dead for over six months. Start a new discussion instead.