C++ getting real path of resource file - c++

I have an external dll, who require a path as string to search the image.
Now I am able to save&load bitmap files from my resources, but I don't know how to get a path like "C:\test.bmp" from a resource file.
I already tried "test.bmp" (<- is the execute folder),
"resource\test.bmp" etc in upper, lowercase, + and without s and so on.
Any idea how I get a valid path to an image file in my resource?
testBMP = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2));
That works file, but is sadly not what I can use :(

You can't get a file-path to a resource in your module, you have to use the Windows API calls. If you require the resource to have a file system path for the external dll you will need to extract it (this can be done at runtime) and save it into the file system.

Related

To Determine if a .exe file is an setup file

I need to write a c/c++/kernel driver program where I can check if an .exe file is an Setup file where I can install new application in my computer (Like an .MSI file) or an normal application (For Example: If I Open an .exe file which is named as chorme.exe then it is an normal window application which only opens chrome browser , but if I Open an .exe file which is chromeinstaller.exe it is an installer which installs chrome browser in my computer).
Is there any way to determine if it's a setup file or just a application.
Microsoft itself tried to solve this with "Installer Detection" , and from their solution it becomes obvious that there is no hard solution. They look for:
Filename includes keywords like "install," "setup," "update," etc.
Keywords in the following Versioning Resource fields: Vendor, Company Name, Product Name, File Description, Original Filename, Internal Name, and Export Name.
Keywords in the side-by-side manifest embedded in the executable.
Keywords in specific StringTable entries linked in the executable.
Key attributes in the RC data linked in the executable.
Targeted sequences of bytes within the executable.
We can safely assume they're doing this because there is no ready-made solution.

what is the proper method of using resource files in MFC project?

i have made a MFC-based game and my project includes images and sounds. Now i want to create an installer setup in order to distribute it.
i have used the resources by providing their exact path in e.g
img->Load(L"C:\\Users\\Ad33l's\\Desktop\\block mania\\block mania\\res\\db.png");
MCIWndCreate(NULL, NULL,WS_POPUP|MCIWNDF_NOPLAYBAR|MCIWNDF_NOMENU,L"C:\\Users\\Ad33l's\\Desktop\\block mania\\block mania\\res\\tick.wav");
1.Can someone tell me any way to avoid Hard-coding the actual resource path as these resource files will not be present at this same exact path in other computers ?
2.Also guide me to handle these resource files during the creation of standalone SETUP (i am using advance installer )
(as an actual answer).
Do not use absolute path, always use relative path; relative to your EXE file is one solution.
The EXE path can be found using GetModuleFileName.
char appPath[MAXFILENAMELEN];
GetModuleFileName(NULL, appPath, MAXFILENAMELEN);
(addendum) appPath name is misleading, it contains the full name of the application; you need to extract the path from the full application name.
We do something like this:
(edit to make it compilable in unicode)
TCHAR applicationPath[MAX_PATH];
GetModuleFileName(NULL, applicationPath, MAX_PATH);
CString sSoundFile(applicationPath);
PathRemoveFileSpec(sSoundFile.GetBuffer());
sSoundFile.ReleaseBuffer();
From there, you can do something like (pseudocode-ish):
img.Load( appPath + "//Images//db.png" );
You can have a variable that saves the directory they want to install your program in. After they choose the directory they want to install it in, go off of that. Or you can also use system folders like the appdata folder
A first solution would be to configure your setup project to install the installation files under the DesktopFolder\block mania\block mania\res. Then, you can access within your application the current user Desktop location and append to it the remaining fix location (block mania\block mania\res).
Another solution would be to configure the setup project to create registries at install time which will store the paths of the installation files. Then, your application could read the installation paths from registry.
Finally you could also create at install time environment variables containing the paths of the installation file and, then use within your application the environment variables to access the installed files locations.

Extract absolute file path from a QRessource loaded file

I have a file in /home/me/xmls/foo/bar.xml
and a correct qrc file containing this path
The folowing code :
QFileInfo f (":/foo/bar.xml");
cout<<f.absoluteFilePath().toAscii().data()<<endl;
outputs:
:/foo/bar.xml
when i was expecting:
/home/me/xmls/foo/bar.xml
Is there any way to recover the absolute path in the system of a QRessource loaded file ?
QResource encodes its contents into a file that gets compiled into the final binary. So, no I would expect it is not possible to get the original path at run time. As the Qt docs state: -
The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable
Also, it doesn't make sense to want the original path, as other users are unlikely to have the file at that location.
This :
QFileInfo f (":/foo/bar.xml");
means that you are going to load a file from the resource file. Basically, this is a library (static or shared), which contains resources (images, translations, etc).
That is a standard syntax to access a file in the resources, as explained in their documentation for the resources.

File I/O from current Windows position C++

I have not yet found a definitive answer about this. I am trying to have access to files in subfolders from my .EXE. When I have asked before, people tell me to use the absolute location i.e. "c:/game/info/" if I wanted to access something in /info/
But it is completely unreasonable for me or anyone to assume that someone is going to use their program from the same directory. What if the user only has a D drive? That sort of thing.
So my question is: how can I access a file in a subdirectory from my executable without relying on the entire path?
Your title says "Windows", so I'll give a WinAPI-specific answer.
On Windows, you can find your application directory with GetModuleFileName(NULL, ...), and PathRemoveFileSpec. Then PathAppend will make the full path to your data files.
Or you can store the data inside you .exe file as Win32 resources, so they never get separated.
Please note that this approach generally works only for read-only access to data files. If you try to write files in your application directory, you might be blocked by ACLs (depending on install location and local security settings of the computer).
Use GetModuleFileName (Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.)
char strExePath [MAX_PATH];
GetModuleFileName (NULL, strExePath, MAX_PATH);
You'll then need to extract the folder path (someone has already posted how to do that), and combine your path.
Make or use an installer that asks the user where to install the executable and writes that path to the registry in a well-known location for later reference.
if you use:
#include <fstream>
ifstream stream("file");
it will be working. "file" is file in directory with your exe. Of course if you want go up or down in folders hierarchy use "..\file" or "folder\file"

Relative path problem for a deployed win32 application

I have written a c++ program and deployed it in say c:\my_app, and my executable's path is c:\my_app\my_app.exe. Say, my_app needs many files such as the_file.txt, which is located in c:\my_app\the_file.txt.
In my executable, I open the txt file as, xx.open("the_file.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt". I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app....
Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path