Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling/Using a function which is inside a DLL file

Status
Not open for further replies.

Jayson

Programmer
Jan 31, 2001
75
US
I have written a function inside DLL file. Is there a way to call this function outside the DLL file?

Any help will be appreciated?
 
There are several ways to do this; the method I describe here is just one of them.

Step 1: Export the function from the DLL.

For information on how to do this, create a new Win32 Dynamic Link Library project in VC++. Pick the "DLL that exports some symbols" option.

Let's say that you call it "MyDLL". Then you can look at the "MyDLL.h" and "MyDLL.cpp" files that App Wizard will generate for you. They have some pretty helpful comments.

Step 2: Call the code from another DLL or EXE

This part is really pretty easy. Essentially you just add an include statement for "MyDLL.h" and then call the function just as you would call any function:

int nReturnValue = fnMyDLL( );

But the linker will complain unless you tell it where to find the library file. So you'll need to go to the "Link" tab under Project...Settings and add "MyDLL.lib" to the Object/Library modules list.

At that point it should build. However, note that you may have to set up your include paths (under Tools...Options...Directories) so that VC++ can find "MyDLL.h" and "MyDLL.lib". Alternatively, you can specify relative (EX: "..\MyDLL\MyDLL.h") or absolute paths to the two files.

Finally, when you run your EXE, you might want to put "MyDLL.DLL" into the same folder as the EXE so that Windows can easily find it.

Note that this is just one method, and it's really just scratching the surface. For more info, you could start by going to msdn.microsoft.com and searching on "dllexport". One useful link is given below.

 
Hi

HMODULE hDll = LoadLibrary(szPath);

HRESULT (*MyFuncFromDll) (void);
MyFuncFromDll = (HRESULT (*) (void))GetProcAddress (hDll, "MyFuncFromDll");
HRESULT hr = MyFuncFromDll();

FreeLibrary(hDll);

That's all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top