Prevent Windows' open file dialog from checking read permission - winapi

I'm using MFC's CFileDialog to select a file. I'm only interested in the complete file path as my application is not going to open it directly. Though the file dialog denies selecting the file by stating: "You don't have read permission to open this file". (Which is correct -- I don't have read permission; I don't want to open the file.)
So, is there a way under Windows to get to the path by using a file dialog?
Here's my code:
CFileDialog dlg(true, nullptr, nullptr, OFN_FILEMUSTEXIST, nullptr, nullptr, 0, true);
dlg.DoModal();

I found a workaround by (ab)using the save file dialog which does not check if you have read/write permission to a file:
CFileDialog dlg(false, nullptr, nullptr, OFN_FILEMUSTEXIST, nullptr, nullptr, 0, true)
One might want to change the title of the dialog as the application won't save anything to the selected file:
dlg.m_ofn.lpstrTitle = "Select file";

The file open dialog implements OFN_FILEMUSTEXIST by attempting to open the file.
If you don't have read permission to open the file, this will fail.
The solution is to not use the OFN_FILEMUSTEXIST flag, and implement your own check if needed (you could either do this after the dialog closes, or before it closes by using a hook).

Related

How to run .Bat file in background

I need to print the pdf file from the browser, so I have created a custom URL protocol to trigger the bat file, now I need to prevent the command prompt window from opening, also I need to protect the bat file from editing from the user side.
Open notepad and add the below content to it, then save the file as hidebatch.vbs:
echo Set MyScript = CreateObject("WScript.Shell")
echo MyScript.Run "C:\Path\to\your\batchfile.bat", 0, False
Simply replace C:\Path\to\your\batchfile.bat with the path to your batchfile. Then to launch this, instead of statring up your batchfile, you would run cscript hidebatch.vbs

autorun.inf won't open file

I want to run the open file in autorun.inf, but I can not. When I tried to shellexecute a .txt file, it opened, but in hidden mode. I found notepad.exe processes in Task Manager. I can't shellexecute .vbs file. But I can change the label and icon at my USB. Can anybody help me in this situation?

vb6 read and write a text file in win 7

I am trying to execute a .exe file (created by VB6) on Windows 7.
What the application will do is to simply create a text file, and then read the text for a text file.
All the functions are being tested successfully on Windows XP. When I execute the .exe file on Win 7, the functions act as usual but the file I created doesn't exist on the specific path (C:\test.txt). The most weird thing is that I can still read the content from the text file (through the .exe file) despite being unable to find the text file on file explorer.
Then I discovered that I have to choose [run as Administrator] to execute the .exe file, so that the file (test.txt) will be created on the C: drive. I am very curious how the .exe file can still read the text file even it is not existed, and how can I force the .exe file to run as Administrator?
Here is the coding to write and read a file.
Open "C:\" & "test.txt" For Output As #1
Print #1, cDrive.Text
Close #1
Open "C:\" & "test.txt" For Input As #1
Input #1, msg
Close #1
cDrive.Text = msg
Exit Sub
To answer the third remark:
Windows Vista and Windows 7 User Access Control (UAC) introduced a feature called the VirtualStore which is designed to add an extra layer of security protection for applications installed under the Program Files folder. If you search for the file on you hdd you might find a second instance of the file in /User/AppData/Local/VirtualStore
So that's why it is still able to read text.txt allthough is doesn't excist in the location you mentioned.
I suggest that you run the program as an administrator by using the feature in the shortcut (after compiling and installing).
If you asked about it when in debug mode - I believe that if the user that you use to debug is an admin - it's enough.
I have no answer for your question "I am very curious how the .exe file can still read the text file even it is not existed"
You should not specify open as #1 directly, use FreeFile() function instead Look here for a sample.
Good luck
Drive C is being protected by Win7, you can still write to it, but you really shouldn't.
If you want to run the app as admin:
right click on the EXE
select PROPERTIES
go into COMPATABILITY tab
check RUN THIS PROGRAM AS ADMINISTRATOR check box
click OK
Now every time you run the app, it will run as administrator

Create .exe file to open .html document

I need to create a .exe file that opens an .html document with the default browser.
In command prompt, start myfile.html works, but I don't know how to turn that into an .exe file.
How can I do that please?
Thank you.
Quite useless IMHO... Anyway use a .BAT file instead and use some app around there that converts .BAT to .EXE.
Why not just use a .bat file? Much less hassle than compiling an .exe. As well, in Windows, double-clicking the .html file would open it in the default browser anyways, so you're not gaining much.
I'd echo the questions about why you'd want to do this, but if you really insist on doing it, it shouldn't be terribly difficult.
#include <windows.h>
int main(int argc, char **argv) {
if (argc !=2) {
MessageBox(NULL, "Usage: exec_html <html_file>", "Usage Error", MB_OK);
return 1;
}
if ((int)ShellExecute(NULL, "open", argv[1], NULL, NULL, SW_SHOWNORMAL) < 32) {
MessageBox(NULL, argv[1], "Could not open file", MB_OK);
return 1;
}
return 0;
}
Compile with a C or C++ compiler of your choice. If (as sounds like the case) you don't normally use either, the easiest way to go is probably get Microsoft Visual C++ Express, a free (as in beer) download.
First download a bat to exe converter here, then open notepad and type the following:
#echo off
start myfile.html
exit
Save it as whatever you want (.bat) and then start bat to exe converter and select the batch file and click the compile button.
This way works and too everyone wondering why you might want to do this, I infact have been looking everywhere for this and am glad I found it. I needed to make a ZIP file and inside I needed my game to be runable through an exe file and my game could only be run through HTML so I found this and have been able to do so. :)
You can use ShellExecute with "open" as verb from C or C++.
First download bat to exe converter from the following link:
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
Then open notepad and type the following:
#echo off
start myfile.html
exit
Save it as whatever you want.bat and then start bat to exe converter and select the batch file and click the compile button.
Now you get the exe file you wanted.

RunTime Error '70' Permission Denied in VB6

I am using VB6. The tool that i have created extracts few zip files and unzips them onto a folder that i create locally.In the clean up part of my code, i have deleted the folder using this code
If (f.FolderExists(path + "Extracted Files") = True) Then
f.DeleteFolder (path + "Extracted Files")
End If
When i run this code, i get an error Run Time Error '70' and Permission Denied in the line f.DeleteFolder(path + 'Extracted Files').
Where am i going wrong ? Or do i need to create the folder with a different permission ?
Perhaps one or more of the files is read-only? Use the optional force parameter to force deletion:
f.DeleteFolder (path + "Extracted Files"), True
You are using a library written in another language to extract the files? Verify that any pointer was open, it is likely that some file is opened, good luck!
Run Registry Editor by typing regedit in Start Search or command prompt.
In Registry Editor, navigate to the following registry key:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System
Locate the following DWORD registry subkey in the right pane: EnableLUA
Double click on EnableLUA
On value prompt, set value to 0.
Exit from Registry Editor.
Restart the computer.
To enable the UAC again, set value of EnableLUA to 1.

Resources