Click here to Skip to main content
15,946,320 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 yesterday
v2
Comments
Richard MacCutchan 24hrs ago    
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 16hrs ago    
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 15hrs ago    
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 14hrs ago    
I will do that.

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 22hrs ago    
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 16hrs ago    
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 6hrs ago    
"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 16hrs ago    
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 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