[try Beta version]
Not logged in

 
Using DLLs

Mar 10, 2012 at 8:43pm
My C++ book didn't include any material regarding the use of dynamic and static link libaries. So I tried looking up the subject and all I could find where long snippets of confusing code which I didn't really understand. Anyway, I have had a go and it seems to be working (by the fact that I am not getting any compiler or run-time errors). Here is the code I have written:
1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <Windows.h>
#include <string>
using namespace std;
HINSTANCE u3dDLL = LoadLibrary(LPCWSTR("Ultimate3D.dll"));
void u3d_render() {
int __cdecl Render();
}
void main() {
u3d_render();
}
The DLL I'm using is a 3D rendering libary by the way. So basicly my question is simply, am I doing this write? If not, what should I be doing? Thankyou for your time.
Mar 10, 2012 at 9:10pm

In you're example you are dynamically loading a DLL. In this case, each function you want to call from that DLL you need to first call GetProcAddress to get the address of each function. You need to know the function signatures to call them correctly. Search for this function on the internet for examples.
Mar 10, 2012 at 10:37pm
Okay thankyou, just having a little trouble understanding GetProcAddress, so would I have to something along the lines of this:FARPROC u3d_init = GetProcAddress(u3dDLL, "Render");
Mar 10, 2012 at 11:29pm
Yes, then you can check whether it failed or succeeded, if it failed it will return 0

1
2
if ( u3d_init != 0 )
  // success! 


As Bob said you will need to know the function signatures, to do this you can open a command prompt, navigate to the directory of your DLL, and enter "dumpbin /exports Ultimate3D.dll" which will list all of the symbols that are exported from the DLL. The function names are often mangled so if you start off with a function called 'func(int a)' it might be exported with a signature of '_func@4' or it could get completely messed up, something like ?func@@YAXH@Z' so you need to determine the signature using dumpbin.
Mar 11, 2012 at 11:33am
Thanks :) Its working now
Last edited on Mar 11, 2012 at 8:21pm
Topic archived. No new replies allowed.