# How can i call some function from my DLL?

## Recommended Posts

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?

##### Share on other sites
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

##### Share on other sites
In this case, you have to include to the caller this header:

__declspec (dllimport) bool positive(int num);

##### Share on other sites
Quote:
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;}}

##### Share on other sites
and what about classes? how can i make classes in a DLL ?
and how can i call them ?

##### Share on other sites
Quote:
 Original post by lord4nand 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.

##### Share on other sites
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); };

##### Share on other sites
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.

##### Share on other sites
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]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978690

• 20
• 14
• 12
• 10
• 12