How can i call some function from my DLL?
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?
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
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
__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().
Quote:Original post by hplus0603It'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.:
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().
extern "C" {__declspec (dllexport) bool positive(int num){ if (num>0) return true; return false;}}
Quote:Original post by lord4nUsing 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.
and what about classes? how can i make classes in a DLL ?
and how can i call them ?
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:
how can i import it into my project ?
THE CLASS IN THE DLL FILE:
class __declspec(dllexport) CJm {public: CJm(); int Positive(int num); };
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:
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.
// 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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement