Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried to write to the desktop of my computer using the code below from a dialog box, but it could not create a file there? Why is that so? What was wrong? I do not wish to make my App run naturally with administrator priviledge. Browsers that do not run with administrator previledge can access my desktop. What could be the issue.


C++
HANDLE hFile = CreateFile(wstFilePath.c_str(), // file name
               GENERIC_WRITE,        // open for write
               0,                    // do not share
               NULL,                 // default security
               CREATE_ALWAYS,        // overwrite existing
               FILE_ATTRIBUTE_NORMAL,// normal file
               NULL);                // no template
           if (hFile == INVALID_HANDLE_VALUE)
           {
             //Messabox here
            return (INT_PTR)TRUE;
           }



Previously I had tried the following code with same result:


C++
ofstream out(stFilePath, ios::out|ios::binary);
if(!out)
{
    //Messabox here
    return (INT_PTR)TRUE;
}



What was wrong?

What I have tried:

I have tried debugging effort again and again. The internet won't offer assistance for something so basic.
Posted
Updated 22-Jul-24 14:02pm
v2
Comments
Richard MacCutchan 23-Jul-24 6:59am    
The most important piece of information in this question is the actual path of the file you are trying to create. Without that it is impossible even to guess what is wrong.
Gbenbam 23-Jul-24 14:49pm    
I believe the path is valid because it was returned by GetSaveFileName Function. I will modify the code to include that. I actually thought it may be a permission problem that I can be told how get around. Browsers write effortlessly to my desktop without any special permission.
Richard MacCutchan 23-Jul-24 16:02pm    
Believing is not always the same as knowing. If you show us the actual path that causes the problem we may be able to offer some suggestions.
Gbenbam 23-Jul-24 16:19pm    
I will do that.
Richard MacCutchan 30-Jul-24 6:17am    
And a week later you still have not provided the information.

Start by using the debugger to find out exactly what is in wstFilePath and what c_str returns as a string - that is the full path to the file and for the desktop, it (by default) needs to be "C:\Users\[Your Username]\Desktop\Filename.Extension" (though the user can configure a different location I've not seen an installation where that's happened).

If that looks like it should be correct, check the error code using GetLastError and see what that says as a cause.

But I'd guess it's the wrong path, a badly formed path, or a permissions problem with the folder you are actually trying to open.

Sorry, but we can't do any of that for you - we have no access to your system or your code while it's running!
 
Share this answer
 
Comments
Dave Kreskowiak 23-Jul-24 9:11am    
OneDrive can take over the Desktop folder so it's not directly under your username anymore. It'll end up under C:\Users\username\OneDrive\Desktop.
Gbenbam 23-Jul-24 14:51pm    
I believe the path is valid because it was returned by GetSaveFileName Function. I will modify the code to include that. I actually thought it may be a permission problem that I can be told how get around. Browsers write effortlessly to my desktop without any special permission.
OriginalGriff 24-Jul-24 0:41am    
"I believe the ..." has no place in debugging!
Never "believe"; always check. It's surprising how often what you think must have happened didn't, and it's the basics that throw you off the scent. You have an incredibly powerful tool that will tell you exactly what is going on with code flow and variable content - use it!

Browsers can't write to your desktop without special permission - they explicitly ned the user to instruct them to do it. If they could, the ransomware / malware problems would be thousands of times worse than it is!
Checking that the path is correct is the first thing to do, but you should also pay attentation that the process needs access rights to that desktop. So watch out for the error code which may hint that you havent the correct right.

BTW: the browsers are doing tricky stuff with handling the HTML with lower rights and disk access with normals rights.
 
Share this answer
 
Comments
Gbenbam 23-Jul-24 14:51pm    
I believe the path is valid because it was returned by GetSaveFileName Function. I will modify the code to include that. I actually thought it may be a permission problem that I can be told how get around. Browsers write effortlessly to my desktop without any special permission.
This code works for me :
C++
void TestOtherCode()
{
	FilePath fileName = { 0 };
	GetEnvironmentVariable( "USERPROFILE", fileName, MAX_PATH );
	if( ! fileName[ 0 ] )
	{
		printf( "unable to obtain environment variable\n" );
		return;
	}

	PathAddBackslash( fileName );   // adds a backslash if necessary
	strcat( fileName, "Desktop\\TestFile.txt" );

	FILE * fp = fopen( fileName, "w" );
	if( ! fp )
	{
		printf( "failed to open %s\n", fileName );
		return;
	}

	fprintf( fp, "a line of text\n" );

	printf( "test concluded\n" );
}
This tells me that it can be done but you are doing something incorrectly. I would start by verifying the name of the file you are writing too. This code demonstrates one way you can obtain it.
 
Share this answer
 
Comments
Gbenbam 25-Jul-24 6:24am    
I think is the path. I normally use
wstring wstPath(2048,L'\0');

to create a buffer for getting FilePath from API. My guess is that since the path is much more shorter than the buffer size,the extra null character interfered. Its not really about the desktop it about writing to path returned by GetSaveFileName. In this test in question, I used the desktop. I am yet to confirm my assumption on why my attempt to write to desktop failed. I will shortly though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900