Pass %PROGRAMFILES% environment variable to _access_s [duplicate] - c++

This question already has answers here:
Using Windows Environment Variable in Native Code
(2 answers)
Closed 10 months ago.
I am trying to check for the existence of a file using _access_s
errno_t ret = _access_s("%PROGRAMFILES(x86)%\\Microsoft\\Edge\\Application\\msedge.exe", 0); ---> //error, returns 2
errno_t ret = _access_s("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", 0); ----> //works fine
How to use environment variables in the path for _acces_s method?
Edit: I am looking for a winrt api as this is UWP app I am working on.

Before passing the path to _access_s you need to expand the environment variables.
You can use ExpandEnvironmentStringsA function for that. This function gets a path with environment variables and outputs a path with the variables expanded.

Related

The correct way to make my program run at startup [duplicate]

This question already has answers here:
How to run a program automatically as admin on Windows 7 at startup?
(9 answers)
Closed 4 years ago.
(1) I want to make my program run at startup. I did that step:
I added my program path to that registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run [Or]
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Previously, the program was running at startup but after I have added the Manifest File to give my program the administrative privileges as follow:
Since that action, the program doesn't run at startup.
Finally, the code which adds the value to Software\\Microsoft\\Windows\\CurrentVersion\\Run:
// Set launch at startup setting
bool startup = wxAtoi(CPublic::getConfigItem("settings/startup"));
wxString appName = wxTheApp->GetAppName();
wxRegKey regKey(wxRegKey::HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Run");
if (startup == 1) {
regKey.SetValue(appName, wxStandardPaths::Get().GetExecutablePath());
} else {
regKey.DeleteValue(appName);
}
What's the problem then?
(2) There is another simple question related to that question:
How to make my program hides after running at startup into the system tray?
To answer the 2nd question (the 1st one is answered in the comment by #Snetfel above), you simply need to create a wxTaskBarIcon and avoid creating any (visible) normal windows on startup.

chdir() returns 0, but debugging in the very next line after chdir() call shows that directory has not changed [duplicate]

This question already has answers here:
chdir() not affecting environment variable PWD
(3 answers)
Closed 7 years ago.
MyCode:
int ushExCd(Cmd cmd){ // BuiltIn shell command implementation
char * home = "HOME";
int returnVal = 1;
if(cmd->nargs > 1){ // target directory is specified in args[1]
returnVal = chdir(cmd->args[1]);
}else{ // target directory is not specified, use "HOME" instead
returnVal = chdir(getenv(home));
}
if(-1 == returnVal){
perror("cd");
}
return 1;
}
Using a llvm debugger to check returnVal right after its assignment shows that its value is 0 (though its initialized to 1), but doing p (char*)getenv("PWD") in the debugger immediately after the chdir() system call shows that the directory has not changed.
I was using Xcode, but then I tried using bash shell, and a tcsh using gcc compilers with no use.
Whats even more interesting is, the same piece of code runs successfully on a shell that another person implemented, which might indicate an incorrect shell implementation, but then I am debugging right inside this code snippet and there should be no way the chdir() is getting lost at a later point of time.
I think that the getenv("PWD") will give you the current directory when your program was launched.
To know the current directory, use getcwd().

C++ Write Registry [duplicate]

This question already has answers here:
Using C++ to edit the registry
(5 answers)
Closed 9 years ago.
I will write this registry key in the registry.
It should be written in C++ for windows 7 and 8.
This is the .reg file:
REGEDIT4
Windows Registry Editor Version 5.00
[-HKEY_CLASSES_ROOT\.bin\]
[-HKEY_CLASSES_ROOT\binimage\]
[-HKEY_CLASSES_ROOT\Bootvis Trace File\]
[-HKEY_CLASSES_ROOT\Crestron SIMPL Windows Compiled Program\]
[-HKEY_CLASSES_ROOT\elby.VCDMount.1\]
[-HKEY_CLASSES_ROOT\gBurner\]
[-HKEY_CLASSES_ROOT\ImgBurn.AssocFile.bin\]
[-HKEY_CLASSES_ROOT\IsoBuster.bin\]
[-HKEY_CLASSES_ROOT\IZArcBIN\]
[-HKEY_CLASSES_ROOT\MPlayerFileVideo\]
[-HKEY_CLASSES_ROOT\PAISO\]
[-HKEY_CLASSES_ROOT\StuffIt.Archive.Open.Generic\]
[-HKEY_CLASSES_ROOT\StuffIt11.Archive.Open.Generic\]
[-HKEY_CLASSES_ROOT\TzBIN\]
[-HKEY_CLASSES_ROOT\UltraEdit.bin\]
[-HKEY_CLASSES_ROOT\VLC.bin\]
[HKEY_CLASSES_ROOT\.bin\]
#="exefile"
"Content Type"="application/x-msdownload"
[HKEY_CLASSES_ROOT\.bin\PersistentHandler\]
#="{098f2470-bae0-11cd-b579-08002b30bfeb}"
[HKEY_CLASSES_ROOT\.bin\shell\open\command\]
#="\"%1\" %*"
IDK how to do it but maybe anyone here can help me :)
Thanks :)
You should use the WinAPI functions for registry handling. You can find the list here.
I would be checking these functions:
RegCreateKeyEx
RegOpenKeyEx
RegGetValue
RegSetValueEx
RegCloseKey

Execute file in another directory [duplicate]

This question already has answers here:
system("c:\\sample\\startAll.bat") cannot run because of working directory?
(4 answers)
Closed 9 years ago.
Consider the following:
I have a c++ program in C:\Documents\myProgram.exe
With this code in it:
system("start C:\\somefolder\\start.bat");
That will start the target file (start.bat) in C:\Documents\ instead of `C:\somefolder\'.
My question is, how do I execute the file in it's own directory instead of myProgram's directory?
In theory this is what I want to accomplish using c++:
cd C:\somefolder\,
start start.bat
If you're on windows anyway, use ShellExecute, you can set more things and launch even documents, links etc.
To do this you can do one of two things (that I found).
A) You can use chdir() in unistd.h; see http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
or
B) You can use something called the File System Interface, from the GNU library, for more advanced stuff; see http://www.gnu.org/software/libc/manual/html_node/File-System-Interface.html#File-System-Interface.
Anyway, best of luck, I hope you find something that will work!

get output of system() into a variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to capture stdout from a system() command so it can be passed to another function
In linux to get the current status of a service I wrote this code segment::
char cmd[100];
sprintf(cmd,"service %s status",argv[1]);
system(cmd);
It is running fine and it shows the output on the console like : mysql is running OR mysql is stopped
But I need this console output in a string variable. How can I get 'mysql is running' in a string variable so that I can use this string variable later.
thankx.
If you want to capture output then use popen() rather than system().