-1

I'm writing code in C++ using Visual Studio (2017) and trying to use functions from a 3rd party library.

I have been told that "[library] doesn't provide a linker library, so you need to resolve the functions at runtime, not at compile time".

How can this be done in Visual Studio?

Thanks

Edit: obviously I tried googling but all that came up were suggestions for fixing compilation and runtime errors

Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • 3
    Example code can be found at https://learn.microsoft.com/en-us/windows/win32/dlls/using-run-time-dynamic-linking. – R Sahu Jun 29 '20 at 14:45
  • 1
    [`GetProcAddress`](https://learn.microsoft.com/en-ca/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress) – ChrisMM Jun 29 '20 at 14:46
  • 1
    Or more verbose on youtube: James McNellis "Everything You Ever Wanted to Know about DLLs" around 7:20 – KIIV Jun 29 '20 at 14:49
  • See also [How to generate an import library (LIB-file) from a DLL?](https://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll), though you would need to first understand the reasons *why* the library chose *not* to provide an import library. – dxiv Jun 29 '20 at 15:24

1 Answers1

1

If you just need a couple functions

HMODULE hMod = LoadLibrary("C:\\lib\\path\\library.dll");
void* fnPtr = GetProcAddress(hMod, "nameOfExportedFunction");

To call the function you need to know the calling convention and the arguments, here is an example of how to do it:

typedef void* (__cdecl* _Cvar_Get)(const char* var_name, const char* var_value, int flags);
_Cvar_Get Cvar_Get = (_Cvar_Get)GetProcAddress(hMod, "nameOfExportedFunction");
void* result = Cvar_Get("cl_gamepath", "Name", 0);

if the DLL is in the same directory as your exe, then you can just use "library.dll"

If you need all the functions then use this answer which dxiv posted in the comments

GuidedHacking
  • 3,628
  • 1
  • 9
  • 59
  • How do you call the function then? – Jessica Chambers Jun 30 '20 at 09:20
  • Or create objects of a type that is defined in the library? – Jessica Chambers Jun 30 '20 at 13:35
  • 1
    @JessicaChambers you need to know the calling convention of the function and the argument types, you can cast that function pointer to that function type and then call it by address like that, I will update my answer – GuidedHacking Jun 30 '20 at 16:52
  • tldr; I found your website and this thread: https://guidedhacking.com/threads/how-to-call-a-game-function.7825/ It seems no matter what I put as "`_Cvar_Get`" VS informs me that it is not defined (it also does not consider `__cdecl *` a valid identifier) – Jessica Chambers Jul 01 '20 at 14:27
  • 1
    @JessicaChambers this is just an example, I updated the answer for you. The return value was a struct that I didn't give you, for the example I just changed it to a void* return value. You shouldn't have any issue with __cdecl. Keep in mind you need to supply the return type, arguments & calling convention yourself, you can't just paste my answer – GuidedHacking Jul 01 '20 at 14:46
  • dw I didn't just blindly c/p, I changed the types etc to be in line with what I'm doing but I do not understand the errors I'm getting - I just posted a new question because otherwise I'm going off topic here. Thanks for all your help! Your(?) website is a huge help – Jessica Chambers Jul 01 '20 at 14:51