Have Executable Separated From DLL's (Is That Explicit Linking?) - c++

I want to have my DLL's in a subdirectory of the directory where my executable is. My current directory looks like:
Main Folder: [Folder]
Program.exe
sfml.dll
Assets [Folder]
Picture.png
Music.wav
When I really want it to look like:
Main Folder: [Folder]
Program.exe
Assets [Folder]
Picture.png
Music.wav
MyDlls[Folder]
sfml.dll
When I try to put them (DLL's) in a folder I get the error message:
The program can't start because sfml-system-d-2.dll is missing from your computer. Try reinstalling the program to fix this problem.
So, then I looked into explicit linking, and followed the tutorial here:
http://www.dreamincode.net/forums/topic/118076-dlls-explicit-linking/
If explicit linking is not what I need to use, then please tell me what I need to do. Else, please tell me what is wrong with my code below: (Also, I do not know if this is static or dynamic linking..??)
// Startup.h
#ifndef STARTUP_H
#define STARTUP_H
#include <iostream>
#include <windows.h>
class Startup
{
private:
HINSTANCE hDLL;
public:
// Explicitly link SFML DLL's
typedef int(*funcAdd) (int, int);
typedef int(*funcSubtract) (int, int);
void LoadDLLs()
{
// Retrieve DLL handle.
vector<LPCSTR> libraries = {"openal32.dll",
"sfml-audio-2.dll",
"sfml-audio-d-2.dll",
"sfml-graphics-2.dll",
"sfml-graphics-d-2.dll",
"sfml-system-2.dll",
"sfml-system-d-2.dll",
"sfml-window-2.dll",
"sfml-window-d-2.dll"};
for (int i = 0; i < libraries.size(); i++)
{
hDLL = LoadLibrary(libraries[i]);
if (hDLL == NULL)
{
std::cout << "Failed to load library.\n";
}
else
{
funcAdd Add = (funcAdd)GetProcAddress(hDLL, "Add");
funcSubtract Subtract = (funcSubtract)GetProcAddress(hDLL, "Subtract");
if (Add)
std::cout << "10+10=" << Add(10, 10) << std::endl;
if (Subtract)
std::cout << "50-10=" << Subtract(50, 10) << std::endl;
FreeLibrary(hDLL);
}
std::cin.get();
}
};
#endif

You could register an App Path (see link), making sure you add your Applications alternate DLL folder location to the App Path PATH value.

You cannot do what you want directly. The code you attached will work only for dynamic loading dlls, but it is not the case.
What you want to do will be platform specific and you need to set the path for the library before executing the program.

Related

This breakpoint will not be hit

I looked at numerous posts concerning this problem but none of them apply in my case.
I have a C++ class in one file that has 3 methods. I can set a breakpoint in one method. However, I cannot set a breakpoint on any line of code in the other two methods. This class is build as a library with DEBUG set. All optimizations are turned off.
Below is the code for the two problem methods in this class.
Blockquote
#include "pch.h"
#include <stdio.h>
#include <afxwin.h>
#include <cstring>
#include <io.h>
#include <iostream>
#include <string>
#include "Log.h"
CLog::CLog()
{
ptLog = NULL; // this is the file ptr
}
void CLog::Init()
{
int iFD;
DWORD iLength;
int iStat;
HMODULE hMod;
std::string sPath;
std::string sFile;
int i;
hMod = GetModuleHandle(NULL); // handle to this execuatble
std::cout << "Module = " << hMod;
if(hMod)
{
// Use two bytes ASCII (UNICODE) if set by compiler
char acFile[120];
// Full path name of exe file
GetModuleFileName(hMod, acFile, sizeof(acFile));
std::cout << "File Name = " << acFile<<"\n";
// extract file name from full path and append .log
sPath = acFile;
i = sPath.find_last_of("\\/");
sFile = sPath.substr(i + 1);
sFile.copy(acFile, 120);
std::cout << " File Name Trunc = " << sFile;
sFile.append(".log");
iStat = fopen_s(&ptLog, sFile.data(), "a+"); // append log data to file
std::cout << "fopen stat = " << iStat;
if (iStat != 0) // failed to open error log
{
return;
}
iFD = _fileno(ptLog);
iLength = _filelength(iFD);
// Check length. If too large rename and create new file.
if (iLength > MAX_LOG_SIZE)
{
fclose(ptLog);
char acBakFile[80];
strcpy_s(acBakFile, 80, acFile);
strcat_s(acBakFile, ".bak"); // new name of old log file
remove(acBakFile); // remove previous bak file if it exists
rename(acFile, acBakFile);
fopen_s(&ptLog, acFile, "a+"); // Create new log file
}
}// end if (hMod)
}
,,,
ptLog is declared as FILE *
This class is invoked with the following code:
#include <iostream>
#include "..\Log\Log.h"
int main()
{
CLog Logger;
Logger.Init();
Logger.vLog((char *) "Hello \n");
}
Blockquote
This code is also compiled as debug. If a set a breakpoint on "Loggger.Init()"
the debugger will hit the breakpoint. If select 'Step Into' it will not enter
the code in the Init() method. The code does execute since I can see the text on the console. If I put breakpoints anywhere in the Init() method they do not break.
I did the following:
Removed log.lib from the input to the Linker.
Obviously, the Link failed due to unresolved externals.
Put back log.lib and rebuilt.
Turned off the option "Require source files that exactly match the original version"
Debug and breakpoints worked.
Enabled the option.
Retried debug and the breakpoints still worked.
Did a full rebuild and breakpoints worked.
I don't really understand it because I had performed numerous cleans and rebuilds
previously.
I did find another issue. I had '/clr' option on.
This is for Common Language Runtime support for the .lib.
The module linked to it did not have Common Language Runtime on. In this case,
the breakpoints were ignored. When I turned off '/ clr', the breakpoints
functioned properly

Why does my DLL load properly when given an absolute path but not a relative path?

I have created a simple dll using /MDd flag on windows 10 using msvc 2019 compilers. The dll only contains a simple add function (like in all the tutorials). After building this library I've copied it into a test folder for explicit linking. Basically, the test passes if I give it the full absolute path to the dll but it doesn't load if I only provide the name of the dll.
Here is the test code:
//test_add.cpp
#include <windows.h>
#include "gtest/gtest.h"
TEST(test, test_add_windows) {
#if defined(_WIN32) || defined (_WIN64)
typedef int (*addPtr)(int, int);
// full path works and the test passes
HINSTANCE hinstLib = LoadLibrary(TEXT("D:\\ACrossPlatformCppLibrary\\test\\ACrossPlatformCppLibrary.dll"));
// relative path does not work: library fails to load
// HINSTANCE hinstLib = LoadLibrary(TEXT("ACrossPlatformCppLibrary.dll"));
std::cout << hinstLib << std::endl;
ASSERT_NE(hinstLib, nullptr);
auto add = (addPtr) GetProcAddress(hinstLib, "add");
ASSERT_NE(add, nullptr);
int x = 5;
int y = 6;
int answer = add(x, y);
ASSERT_EQ(answer, 11);
BOOL fFreeResult = FreeLibrary(hinstLib);
#else
ASSERT_TRUE(true);
#endif
}
And my directory tree
I figured out the answer. I ran another test from the same file to get the current directory:
TEST(test, test2) {
char *fileExt;
char szDir[256]; //dummy buffer
GetFullPathName(".", 256, szDir, &fileExt);
printf("Full path: %s\nFilename: %s", szDir, fileExt);
}
Which outputs:
Full path: D:\ACrossPlatformCppLibrary\cmake-build-debug\test
The problem was that I copied the dll into the source directory, not the build directory.

Creating folders in My Documents

I'm having trouble creating a directory structure in the Windows My Documents directory. I use
ExpandEnvironmentStrings(L"%USERPROFILE%\\Documents",dir,MAX_PATH);
to get the directory then I create a new Directory in there
CreateDirectoryW(dir,NULL)
then in there I want to create another directory so in essence i want Documents\foo\bar however when I go to the foo directory via the Library on explorer side bar 'bar' isn't found unless I go to C:\users\xxx\Documents\foo then its there. Also if I go to Libraries\Documents\foo and right click->New->Folder isn't an option.
I was wondering if there is a Security Option to CreateDirectory I'm supposed to use or what I'm doing wrong
If you want to create a directory tree, you can use SHCreateDirectoryEx. The following code works well on my computer.
#include <iostream>
#include <Windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
int main()
{
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, path)))
{
PathAppend(path, "foo\\bar");
if (SHCreateDirectoryEx(NULL, path, NULL) != ERROR_SUCCESS)
{
std::cout << "Error: " << GetLastError();
}
}
else
{
std::cout << "Error: " << GetLastError();
}
}
Notice that this works only on Windows 2000 Professional or higher.

Search for path of .exe

So I'm looking for a piece of code that allows me to search for the path of the file it's being executed in. For example, I'm doing an autorun program for use in pendrives (example) but I don't know if it'll end up as D:, F:, G: or whatever so the program would search it's own path and open another file based on the path he is found using some 'if' statements.
Here's what I thought:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main () {
// Insert 'search path' code and needed variables here.
if (-ThePath- == "d:\\AutoRun.exe")
{
system ("d:\\MyFolder\\OtherProgram.exe");
}
else if (-ThePath- == "f:\\AutoRun.exe")
{
system ("f:\\MyFolder\\OtherProgram.exe");
}
else if (-ThePath- == "g:\\AutoRun.exe")
{
system ("g:\\MyFolder\\OtherProgram.exe");
}
else
{
cout << "An error ocurred.\n";
cout << "Press enter to exit...\n";
cin.get();
};
return 0;
}
Is there some way this could be done?
GetModuleFileName : documentation here
EDITED - Pedro, the sample code from Microsoft handles a lot of things. To get the file path, all you need is :
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ) {
// handle error in GetModuleFileName
} else {
// now, szPath contains file path
};
In standard C++ argv[0] contains the name of the executable. For a program invoked in the normal way this will be the path of the executable on Windows.

Loading .png Image file with Allegro 5

I am writing a game in allegro and would like to load some image files. However, whenever I call al_load_bitmap, I am getting a null pointer. I am using XCode 4.1 as my IDE. I would try compiling using g++ (in case it is a path issue) however I don't know what I need to do in order to compile it in the command line with g++ (simply g++ main.cpp does not work). Anyways, here is my code:
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
for (int i = 0; i < NUM_TILES; i++)
{
switch (static_cast<Tile>(i)) {
case GRASS:
al_set_path_filename(path, "grass.png");
tileFiles[i] = al_load_bitmap(al_path_cstr(path, '/'));
if (!tileFiles[i])
{
std::cerr<<"grass.png not initialized"<<std::endl;
}
break;
case DIRT:
al_set_path_filename(path, "dirt.png");
tileFiles[i] = al_load_bitmap(al_path_cstr(path, '/'));
if (!tileFiles[i])
{
std::cerr<<"dirt.png not initialized"<<std::endl;
}
break;
default:
std::cerr
<< "Missing case statement for datatype Tile numbered at "
<< i
<< " in Board::Board (float mw, float mh, int tst)"
<< " declared in Board.cpp."
<< std::endl;
break;
}
}
I have already run:
if(!al_init_image_addon()) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
and I have also put:
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_native_dialog.h"
at the top of my file. Neither grass.png, nor dirt.png load and they are in the exact same directory as my main.cpp file. I get no compilation errors, but I consistently get the null pointer when I try to load my images, so when it comes time to draw them to the display, they do not show. Please help!
Neither grass.png, nor dirt.png load and they are in the exact same directory as my main.cpp file
Just a debugging tip... If you were to output the result of al_path_cstr(path, '/') to the console, it should be extremely obvious why the call is failing.
ALLEGRO_RESOURCES_PATH is the location of "bundled resources," which on OS X means the directory of the executable file. (If you were to use an app bundle, then it would be the resource folder of the bundle.) As a quick check, just copy the images into the same directory that your executable file is being built.
Most IDEs have very odd directory structures, IMO. I would ultimately set it up so that you are building into something like:
/src/main.c
/include/main.h
/obj/release
/obj/debug
/bin/game.exe
/bin/game-debug.exe
/bin/image.png
But that's just my preference. Use whatever you like, but you should read the docs again to get a clear picture of the different locations that al_get_standard_path() reveals.
Okay, I had been having the same problem, and I was absolutely positive that I was looking in the correct directory and that the resources for the program were there. I used al_path_cstr(path, '/') and allegro's working directory was as expected. Then I looked at the resource file sizes....
All my resources in my build directory were zero bytes. Copied them over myself and it solved the issue. I hope this helps some one out!