Relative path of Cpack installed application (windows) - c++

I have an application that need to access some files in a folder located inside the application directory.
myAppDir
|-bin/myApp.exe
|-scripts/need_to_find_this_file.py
I can't use configure_file(...) or other hardcoding method because they are processed at cmake-time.
I can't even have access to the installation directory choosen by the user because not an information that can be accessed by cpack.
How could I retrieve the path ~/myAppDir/scripts at the run-time? I want to use it inside my c++ code.

Related

Append bin folder to PATH environement variable after installation

I have done a C++ program for Windows and an NSIS installer using CPack.
I want that after the installation, the user can call my program from the terminal without giving the whole path of the exe.
Sometimes some installers even add an Add useful environment variables checkbox at the end of the installation to give the user a choice.
Is it possible to add to the PATH environment variable the path to our bin folder at the end of the installation using CPack and NSIS Generator?
If this is not possible, how do other programs add environment variables during installation?
As always, check the documentation... https://cmake.org/cmake/help/latest/cpack_gen/nsis.html
CPACK_NSIS_MODIFY_PATH
   Modify PATH toggle. If this is set to ON, then an extra page will appear in the installer that will allow the user to choose whether the program directory should be added to the system PATH variable.
Note that this is hard-coded to be the $INSTDIR\bin path and is not configurable. In particular, setting CMAKE_INSTALL_BINDIR to anything other than bin will break.

How to make a specific program run from the installed path only to avoid DLL hijacking

I have an executable which is linked with some dll files. To avoid DLL hijacking I am installing that application in a protected path (say C:\Program Files) where only the admin have "create" or "write" permissions.
Still some attacker can copy the entire installation folder to some other directory and able to craft a written function with the same name to perform malicious act such as deleting a file or modifying registry settings. The .DLL files will run on the same privileges as given to the running application.
I need to verify that my application is running only from the installed path..How can I make to possible using C++ ..
Look at GetModuleFileName to check where modules are located.

Including external folder in UWP visual studio project

I have a UWP project in Visual Studio (2015, c++) and I want to include an external folder tree of assets (e.g. images) so that when I run it in the emulator those files are available (similarly, when I build a final package I want the files with the package). The files aren't fixed (i.e. I may add/remove files at a later time)
In a regular desktop application I would simply use a post-build step and do an xcopy on the folder into the target directory. This, however, does not work for the UWP build. When I run in the emulator nothing is there (or even in the target directory).
Is there some way to add a build step to copy files (retaining directory structure) or even better a way to add an external folder reference to the project?
I know I am not the only one that does this. Most results in searches are irrelevant, others aren't dynamic, or rely on the files to be within the project's directory tree.
Use AppxPackagePayload element in your .vcxproj file:
<AppxPackagePayload Include="PathToYourImage.jpg">
<TargetPath>SubfolderInPackage\FileName.jpg</TargetPath>
</AppxPackagePayload>
Unfortunately, you'll need to reference every file your want to copy separately. I don't know if there is a way to do it for a directory.
I have a UWP project in Visual Studio (2015, c++) and I want to include an external folder tree of assets (e.g. images) so that when I run it in the emulator those files are available (similarly, when I build a final package I want the files with the package). The files aren't fixed (i.e. I may add/remove files at a later time)
From your description, I think you want to mount an external folder for your uwp app. However, not all path can be accessed directly under current file access rules.
Access to other locations is available only through a broker process. This broker process runs with the user’s full privileges, and it can use these privileges on the app’s behalf for locations the app has requested via capabilities, locations requested by the user via file pickers, etc. The StorageItem encapsulates this brokerage procedure so the app doesn’t need to deal with it directly.
As Rob said that you can access to other locations with the user’s full privileges. For more you could refer to File access permissions official documentation.

Link qt dlls to multiple executables

I have four different .exes in differend subdirs of one specific directory. All of the programs need the exact same .dlls (Qt5Core, Qt5Gui and Qt5Widgets). Is there a way of having theese three .dlls just in the root directory insted of having to copy them into each subdirectory?
Here you find the order in which DLL paths are searched on Windows:
Since Safe DLL search mode is enabled by default, the order is
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
So the easiest thing to do is to ensure that you run your app from the working directory where you put the DLLs (5.). That could be done by writing a starter .bat file that navigates there first and then runs the application relative to the DLL direcory.
The other way would be to add the DLL directory to PATH in a starter .bat file (6.).
In both cases you need a wrapper script for each .exe that you want to run.
You could avoid that if you can make sure that links to the application set the working directory properly. This would work if you have an installer creating the shortcuts for the user and you know the user will not create one himself by just right-clicking the .exe.
There might also be a way using hardlinks or junctions but I don't know if you can copy and deploy those like symbolic links on Linux or if you need to create them on the target system.
Yes, you are using Windows and you can add your root directory as a path in Environment Varibles from
Computer-> Properties->Advanced System Settings -> Environment Variables

How do I set a lua.dll so lua.exe won't ask for it in every directory?

Every time I enter lua my_script.lua in command line, it prompts me for lua53.dll on the same folder. How do I set a path so I won't need to have a copy of the lua library in every folder that I want to run a .lua file? I wonder if it is made via parameters or if I should build my own .exe from the .c files using environment variables, but I don't really know.
I've downloaded the binaries from http://luabinaries.sourceforge.net (v5.3.2 - may 19th) and have put lua.exe inside C:/Windows/System32.
According to this MSDN article, the directory from which the application is loaded is the first location being checked for the DLL file. If you put the DLL next to the location of lua.exe it came with, the DLL should be found and loaded by the system.
It may be better to not put application files into your system folders. Just create a separate folder and put your Lua files (.exe and .dll) there. You can then add that folder to PATH environment variable, so that it can be found when you run it as lua.