How can i call some function from my DLL?

Started by
8 comments, last by lord4n 17 years, 6 months ago
How can i call some function from my DLL ? lets say the name of the dll is Test.dll and the dll has this function: __declspec (dllexport) bool positive(int num) { if (num>0) return true; return false; } how can i call this function from my other project?
True knowledge exists in knowing that you know nothing.
Advertisement
You have to provide a header file along with the library and with that they can see what methods you have defined for your library.

Dave
In this case, you have to include to the caller this header:

__declspec (dllimport) bool positive(int num);


To compile, add Test.lib generated whith your Test.dll

If you're using your DLL as a plug-in (i e, dynamically load it at runtime, instead of statically linking at build time), you should look at LoadLibrary() and GetProcAddress().
enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
If you're using your DLL as a plug-in (i e, dynamically load it at runtime, instead of statically linking at build time), you should look at LoadLibrary() and GetProcAddress().
It's worth noting that if you go for runtime linking (Which is what using LoadLibrary() and GetProcAddress() is called), then you'll need to export the function with an unmangled name, by using extern "C". E.g.:
extern "C" {__declspec (dllexport) bool positive(int num){   if (num>0)      return true;   return false;}}
and what about classes? how can i make classes in a DLL ?
and how can i call them ?
True knowledge exists in knowing that you know nothing.
Quote:Original post by lord4n
and what about classes? how can i make classes in a DLL ?
and how can i call them ?
Using the header and library method again. It's difficult (Pretty much impossible, or at least not worth the effort) to dynamically bind a class. When you export a class, you're really only exporting all of the member functions.

If you create a Win32 DLL project in MSVC, and select "A DLL that exports some symbols", it'll show you an example of a DLL that exports a function, a variable and a class.
ok i exported a class in my DLL
how can i import it into my project ?
THE CLASS IN THE DLL FILE:
class __declspec(dllexport) CJm {public:	CJm();	int Positive(int num); };
True knowledge exists in knowing that you know nothing.
Link your EXE to the .lib file your DLL project produced, and include the header file that class is in. You should be using import/export differently though, like how MSVC does:
// In CJm.h:#ifdef MYDLL_EXPORTS#   define MYDLL_EXPORT __declspec(dllexport)#else#   define MYDLL_EXPORT __declspec(dllimport)#endifclass MYDLL_EXPORT CJm {public:	CJm();	int Positive(int num); };

And then define MYDLL_EXPORTS in your DLL project's preprocessor settings. That ensures that when the DLL is built, the class is exported, and when anything else is built, the class is imported.
EDIT:
Nevermind its working.
heres the working code:
#pragma comment( lib, "e:\\jm")class __declspec(dllimport) CJm {public:	CJm(void);};int main(int argc, char* argv[]){	CJm a;	return 0;}[/soruce]
True knowledge exists in knowing that you know nothing.

This topic is closed to new replies.

Advertisement