Fopen returns null unless explored there? - c

This is a very odd error.
I am using Visual Studio 2012, and in a C++ project (as a container for a C project), I am loading a file in this way:
const char* fname = "SomeFile.csv";
if(!(fp = fopen(fname, "r")))
{
printf("Error! Could not open %s!\n",fname);
return;
}
The CSV is in the same folder as the .EXE, and that is the intention for this program.
1. When I run it in debug, it fails to read the CSV.
2. When I put in the full pathname to the file, it works correctly and loads the CSV.
3. When I go to the output folder in Windows Explorer, and run the .exe, it correctly loads the CSV.
4. Now here is the weird part. When I go to another folder (anywhere else), and I then paste in the full path including the .exe into Windows Explorer, it starts up the program, as it should, but it fails to read the CSV.
As a caveat, if I have the folder loaded in explorer, but run it in the VS2012 debugger, it also fails to load the CSV.
What is going on here? Why would it only find it if I am running it while the window is open in explorer?

fopen, open, etc. will, given a bare filename with no path components, attempt to open the named file in the "current directory". Given a filename without a filesystem root it will attempt to open the file in a directory relative to the current directory. Only when given an absolute (full) path will it look exactly where you've told it.
In a command-line based system, the current directory is pretty obvious - it's the directory you are in when you issue the command.
In a Graphical User Interface the notion of a current directory is a bit more mushy:
Navigate in Windows explorer to the program (let's say WORD.EXE) and double click it - the current directory is likely to be the directory where the program is - the place to which you navigated (but there's no standard that says this has to be the case)
Navigate to your documents for your project and double click a Word document, which launches WORD.EXE automatically - what is the current directory? It's likely to be where the document is.
Launch WORD.EXE from the Start menu - what the heck is the current directory now?
Launch your program from the debugger - now what is the current directory?
This last point is why you have to tell the IDE / debugger what to use as the current directory when it launches your program.
The fileOpenDialog doesn't exactly "default" to the current directory - it open to where you last opened it, without changing the current directory for the program - when you pick a file it then passes the full path of that file to the program.

Related

Run a .exe file when a file is opened in a folder

I am mounting a folder as a virtual drive and i want to run a .exe file everytime user opens any file present in that folder. To be precise the folder would contain dummy files present on some other machine. By dummy files i mean the file would be listed but it would be a empty file. Whenever user opens a file i want the .exe program to download that file from another machine and display it to user.
That functionality (remote access on demand) can be implemented using reparse points and file system filters.
You could
use hooks to rewrite the jump address of OpenFile and in the
detour function check for the handle type, retrieve it's info by
using GetFileInformationByHandleEx, parse the data, download
what you need, open the downloaded file and then return
STATUS_SUCCESS or any appropriate error status in case one occurs.
Note
this is a bit more complicated as you also need a auto-inject
mechanism to inject function/library into each process according to
it's architecture.
this is not a safe procedure as most AV's will most likely consider your code malware.

GetPrivateProfileString and AppData VirtualStore directory

I have a program which reads GetPrivateProfileString from a file ".\abcd.ini" - i.e. it will look for the ini file in the current directory.
If it does not find the ini file, it has a default value set in the 3rd parameter to GetPrivateProfileString.
I have an installer which installs the program to c:\program files (x86)\abcd\client directory.
Initially, the installer also installed an abcd.ini file in the same directory with a particular profile string key/value pair. Post that, I changed the installer to not install any ini file.
However, the program continued taking the value from the old ini file which I had shipped even if it didn't exist in that directory.
After doing a system wide search I found a copy of abcd.ini in c:\Users\myusername\AppData\Local\VirtualStore\Program Files (x86)\abcd\Client
Once I deleted this, the program worked correctly (as if there is no ini file).
Googling it seems that the virtualstore is used because myuser does not have full permissions for c:\program files (x86). However, the program itself doesn't write to the ini file, it only reads from it.
Is this actually how it's supposed to be? Why is the ini file copied to AppData & why does the program read from there if there is no local copy?
I am on Windows 10 64 bit.
The diagnostic is that the EXE program does not contain a manifest that declares itself compatible with UAC. Not unusual for the kind of app that still uses GetPrivateProfileString().
Is this actually how it's supposed to be?
Yes, this the way modern versions of Windows (major version >= 6, Vista and up) deal with legacy programs that assume the user always has admin privileges. Redirecting the file access to the VirtualStore directory ensures that the missing access rights to Program Files directory does not cause trouble.
it only reads from it
The OS does not have a time machine to guess whether you might write to the file and did so in a previous session. So it has to check the VirtualStore directory first. To find that .ini file.
It is also important to not assume that it was your program that got the .ini file in that directory. It could have been done by another ancient program, like a text editor. Or a previous version of your program. Or the installer you use.
Yes, because the program would crash, so Windows redirects the program to the VirtualStore directory.

Access context menu items from windows batch file

Okay I currently need to check about 200 files, so of course i want to automate as much as i can. the software I need to use to inspect them does not seem to have a command line interface, so I am currently stuck right-clicking them and clicking edit.
Is there any way to access that edit command from command line, so I can automate this process, or am I stuck opening 200 files like this.
Okay I figured it out, and here's how I did it!
Go to regedit, and find the file extension im trying to deal with in HKEY_CLASSES_ROOT
then inside the folder for the file extension i found the name of the folder of the program this filetype uses, and that folder was also located on HKEY_CLASSES_ROOT.
Then inside that folder is a "shell" folder wherein all of the contextual options are located.
That shell folder had an element called "edit" and that element contained a shell command that was used behind the scenes to launch the editor with the specific file.
Now I can write my batch script with this command!

read from a file while installation in nsis but do no copy it on the destination pc

I want to display the version on the installation dialog pages of nsis, by reading the version from a text file, but i have to copy this on the destination pc where the executable is run, but i want that the text file is not copied on the user's pc but is only read from,
that is,
i want to include this file into the exe, and read text from it to display on the nsis dialog pages, but not copy it anywhere on the pc wherever the exe is run?
is this possible? or is there any other way of doing this?
In general, you can use $PLUGINSDIR constant. It is de-facto temporary directory on target system and you can put there something and use. Following code will copy the file into the temporary directory on the target machine, but whole directory will be deleted after the installation completion. (InitPluginsDir is needed somewhere before)
InitPluginsDir
File /oname=$PLUGINSDIR\blah.txt "..\myfile.txt"
But in your case, it could be better to solve it in some other way. You can define some constant containing version number and use it in the code, can't you? The !define command could be in generated file so you can automate it...

can I save a file to the current directory in air?

I have inherited an air app, and I don't know a lot of the capabilities and limitations of air. Can I open a local from the current directory for read and write?
Using FileReference, I have been able to save a file to my /Documents directory. But is there way to have it just save in the current directory? Or have the prompt that currently comes up default to the current directory instead of my /Documents dir?
I've read http://livedocs.adobe.com/flex/3/html/help.html?content=Filesystem_08.html, and can't figure it out.
Thanks
If current directory happens to be application directory, then no. AIR has concept of various directories:
File.applicationDirectory - where app itself is installed, read-only.
File.applicationStorageDirectory - buried somewhere deep (on Windows, it is C:\Documents and Settings[User name]\Application Data[Application name]\Local Store) - you can use it to store files.
File.documentsDirectory - shortcut to current user's documents directory. Do not throw garbage there.
File.desktopDirectory - shortcut to user's desktop, should be treated even more carefully.

Resources