I'm modifying an open-source GUI (written in c) to add a new menu item to the tray app. The new menu entry executes a file (update.exe) in the program root in c:\program files directory. Im using shellexec() and it works fine on x86, but since the path is different on x64 - c:\program files (x86), it fails to load. I'd like to use something to the effect of an environment variable like %programfiles%. What is an elegant solution to this?
Alternatively, I do have the ability to change where the update.exe file is stored. Putting it in 'program files' just seemed the most logical. For the problem above, should it go outside of program files? If so where?
You can use GetModuleFileName() to retrieve the full path to the executable. Then you'll just have to cut the filename from the end, and you'll have the complete directory path.
Something like this should to the trick:
TCHAR szPath[MAX_PATH];
LPTSTR szFileName;
GetModuleFileName(g_hInstance, szPath, sizeof(szPath)/sizeof(TCHAR));
szFileName = _tcsrchr(szPath, TEXT("\\"));
*szFileName = TEXT('\0');
// szPath now contains the path
(Warning! Untested!)
Related
I have a project written in C. Currently it's using UNIX makefiles to compile itself for Linux, but recently I've been looking into CMake, to be more portable.
The executable, when running, needs to access some asset files that are part of the project. When using makefiles, I would just compile the project with:
make prefix=/usr
make prefix=/usr install
So while the project is compiled, it knows that it will end up in /usr, and when running, it searching for its own project files there (in something like /usr/share/my-project/).
I created a very basic CMakeLists.txt, that compiles the .exe file, and installs it together with one other asset file in the install directory. I then run the following commands to create an NSIS installer for Windows:
cmake.exe --build --config Release .
cpack.exe
Which succesfully gives me the NSIS installer. When run, it shows the user a few steps, one of them is to decide where the project will be installed, which the user can modify.
So my question is, at that point the project has already been compiled, so how can I pass to my project its own install location, so it can access files included in the project? How do other projects do this? I couldn't find much information online about it, which makes me think I might be taking the wrong approach.
For anyone stuck in a similar problem, I found one solution.
Upon looking online, this seems to be something not recommended for Unix systems, and setting the install location during compilation is pretty standard.
For windows, however, I found the function GetModuleFileNameW (GetModuleFileNameW function (libloaderapi.h)).
It returns the path to the current executable (something like C:\Program Files\<my-app>\bin\my-app.exe). I've confirmed it returns the right path, even when I install the project on different directories. It returns the result using wchar_t, so unicode directories are also supported.
Here is a small example of how it can be used:
// to keep the example simple, this is assuming maximum 1000 characters in the path
wchar_t dynamicProjectLocationW[1000];
GetModuleFileNameW(NULL, dynamicProjectLocationW, 999);
dynamicProjectLocationW[999] = L'\0';
// given a path like "C:\X\Y\bin\myapp.exe" find the second to last slash
// so we can get the path "C:\X\Y\"
wchar_t *pointer = dynamicProjectLocationW;
wchar_t *secondToLastSlash = 0;
wchar_t *lastSlash = 0;
while (pointer[0] != L'\0') {
if (pointer[0] == L'\\') {
secondToLastSlash = lastSlash;
lastSlash = pointer;
}
pointer++;
}
// cut the path short, so we can use the project path to find other files
if (secondToLastSlash) {
secondToLastSlash++;
secondToLastSlash[0] = L'\0';
}
This is solving my problem for now, so I'll be using this until a better solution is found.
I use Clion 2016.1. For example, I run such code in the directory ~/CLionProjects/Tutorial:
#include <stdio.h>
int main() {
char * string;
string = "Hello, everyone";
printf(string);
}
Why does Clion go for this code to this directory?:
/home/ken/.CLion2016.1/system/cmake/generated/Tutorial-9a39f70/9a39f70/Debug/Tutorial
Hello, everyone
Process finished with exit code 15
How to make the programs running in "normal" directory ~/CLionProjects/Tutorial?
UPD
I want to read a "data.csv" file locates in the current directory (where main.c is). But CLion looks for it in /home/ken/.CLion2016.1/system/cmake/generated/Tutorial-9a39f70/9a39f70/Debug/Tutorial. How to make that CLion looks for data.csv in ~/CLionProjects/Tutorial?
If you want to appear your binaries in a folder you've specified, you need to tell it CLion by adjusting your CMakeLists.txt like this:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
Edit:
Somehow it seems that these options are not respected in the current version of CLion (2016.2). Therefore one might has to change the desired output directory via: Build, Execution, Deployment | CMake settings and set it there.
For CLion v2016.1 and earlier
To change build output path, open CLion Settings and go to Build, Execution, Deployment | CMake settings and set it there.
This is more an answer for the UPD question than the original. But the answers are related. .EXE is built into a folder that depends on the build options (debug, release, etc). An exe's default working directory is the folder where the exe is found. That is the folder where the program will look for data files, etc, unless you tell it to look elsewhere.
You can change the working directory for the exe with a runtime option. I made a YouTube tutorial for my COSC1030 (Beginning C++) students but the solution is the same for everyone: https://youtu.be/dTtZEAfh_LM
I have a simple Gtk GUI application written in C. I want to be able to render external images within a frame in my main window. The code for this is as follows:
GtkWidget myImage;
myImage = gtk_image_new_from_file("assets/image.png");
gtk_fixed_put(FTK_FIXED(frame), myImage, 0, 0));
The image shows as expected, but only if I cd into the directory where it exists. ie:
cd /tmp/bin/
./gtktest
If I run it from another location, like so, the image is never found.
/tmp/bin/gtktest
Is there a way to have the application set the present working directory (PWD) to the location of the binary itself?
Thank you.
You are using a relative path to cwd but you need to use either a relative path based on the location of the program binary or an absolute path based on the filesystem's root like /usr/lib/yourprogram/assets.
If you want to build a relative path based on the location of the binary, which might be more flexible in some situations, then you should use dirname(). Like this:
char *my_location = dirname(argv[0]); // argv[0] contains the path to the binary
Check man 3 dirname for more info.
Put the assets inside a GResource and compile them directly into your program.
You could call chdir(2) from inside your Gtk program, or its Glib wrapper g_chdir
I am working using CMake on a little C project using OpenGL. To be able to run, my executable needs to access some resources files such as 3D meshes, textures or shader program sources.
When I run the generated executable, the current folder is the directory where it is created. This directory may differ depending on the binary tree location (out of source ? insource ? anywhere in the coputer). But my resources are located near my source tree.
I would like my CMakeLists.txt to copy the resource folder in my executable output directory but I have not a good idea of the way to do that. Besides, I am not sure this is a "best practice" of CMake.
Thank you for reading :)
You have 2 useful variable to do so: CMAKE_CURRENT_BINARY_DIR and CMAKE_BINARY_DIR, the former refers to the current CMakeLists.txt output directory, the latter refers to the top level project output directory.
Most of the time, you handle resources near the executable depending on it, then you'll certainly want to refer to CMAKE_CURRENT_BINARY_DIR.
configure_file(
"MyResourceDir/myresource"
"${CMAKE_CURRENT_BINARY_DIR}/" COPYONLY
)
This command will copy resource of the CURRENT_CMAKE_SOURCE_DIR/MyResourceDir named myresource in the directory matching the current CMakeLists.txt.
You can glob files of your MyResourceDir and loop on it (maybe there is also some function to copy directory instead of list of files).
I have a library and a console application that uses a library. The library has a folder with source and header files.
My project is in a child/inner directory but that library directory that I want to include is in a parent/upper directory.
My project directory:
H:\Gmail_04\gsasl-1.0\lib\libgsaslMain
Includes files are here:
H:\Gmail_04\gsasl-1.0\src
How can I use paths relative to the project directory, to include folders that are in a parent/upper directory?
Instead of using relative paths, you could also use the predefined macros of VS to achieve this.
$(ProjectDir) points to the directory of your .vcproj file, $(SolutionDir) is the directory of the .sln file.
You get a list of available macros when opening a project, go to
Properties → Configuration Properties → C/C++ → General
and hit the three dots:
In the upcoming dialog, hit Macros to see the macros that are predefined by the Studio (consult MSDN for their meaning):
You can use the Macros by typing $(MACRO_NAME) (note the $ and the round brackets).
If I get you right, you need ..\..\src
I have used a syntax like this before:
$(ProjectDir)..\headers
or
..\headers
As other have pointed out, the starting directory is the one your project file is in(vcproj or vcxproj), not where your main code is located.
By default, all paths you define will be relative. The question is: relative to what? There are several options:
Specifying a file or a path with nothing before it. For example: "mylib.lib". In that case, the file will be searched at the Output Directory.
If you add "..\", the path will be calculated from the actual path where the .sln file resides.
Please note that following a macro such as $(SolutionDir) there is no need to add a backward slash "\". Just use $(SolutionDir)mylibdir\mylib.lib.
In case you just can't get it to work, open the project file externally from Notepad and check it.
There are a couple of hints you need to know.
consider your app is running under c:\MyRepository\MyApp
a single dot on your path means the folder where your app runs. So if you like to reach some folder or file under MyApp folder (imagine c:\MyRepository\MyApp\Resources\someText.txt) you can do it like var bla = File.Exists(./Resources/someText.txt)
and you can go one level up with double dots (..) think about a folder under c:\MyRepository\SomeFolder\sometext.txt
for MyApp, it will be like
var bla = File.Exists(../SomeFolder/someText.txt)
and it is possible to go 2,3,4.. levels up like
../../SomeFolder (2 levels up)
../../../SomeFolder (3 levels up)
and path starting with no dots means the drive root. var bla = File.Exists(/SomeFolder/someText.txt) will look for the c:\SomeFolder\someText.txt in our scenario.