Sign in to follow this  

How can i call some function from my DLL?

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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)
#endif

class 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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this