How to call functions from DLL

i don't know how to call function from DLL,in my MFC project work space.
i have created a DLL ,I know name of functions.
jaydeepAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
yazdchiConnect With a Mentor Commented:
Hi,

I'm not sure if the DLL from which you are trying to call the function uses MFC or not?

If it does then it's very simple, all you do is to first of all you link the DLL to the executable by going to project seting and the link tab, and then where it says "Object/Library Modules" enter the path to the .lib file of the DLL (this is the way I always link my DLLs, or you can try the LoadLibrary way mentioned in the previous comments; personnaly I've never tryed it myself).

Now that the DLL is linked to the executable, put the AFX_API_EXPORT macro in fron of your function declaration (both in the function prototype and the function implementation) like this:

long AFX_API_EXPORT AddNum(short num1, short num2);

long AFX_API_EXPORT AddNum(short num1, short num2)
{
   return (long)num1 + num2;
}

Now all you do is to put the function prototype declaration in a header file and include it where ever you want to call the function from; now you can call the function as if it was not in the DLL.

I am currently working on a project which is made up of about 3 or 4 executables and about 6 or 7 DLLs (both Regular and MFC Extension DLL, which are all dynamically linked to MFC), and I have used this method in all of them and it words, I have even linked and used functions in my DLLs which are defined and implemented within other DLLs and it all works using this method.

Hope this helps.

PS. This method only works for global functions and not functions defined as part of classes (ie. member functions), if this is what you want then let me know because you have to export classes differently.

Regards
Mohsen
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

if function name is MyFunct(), then in ur class
declare as

extern "C" __declspec(dllimport) void MyFunct();

tyhen u can use MyFunct(); call

Hope this helps.
VinExpert
0
 
akalmaniCommented:
HI !!
    Just an extension of vinexpert

1) If u r linking u r DLL statically then u need the declaration as shown by VinExpert.
2) If u r calling u r DLL dynamically then u don't need any declaration.

MTV Enjoy
akalmani
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jaydeepAuthor Commented:
i want to connect DLL dynamicall,but how my program will understand that fuction from which DLL it has to call.
So i guess there must be some declaration that DLL in my work space before fun call of DLL.
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

U can include the Dll name with path during build itself, in project settings. Or in Ur code u can use LoadLibrary(...)
to load the Dll.

If Ur Dll name is "MyDll.dll", the use
HANDLE hDll = LoadLibrary("MyDll.dll");

Then use the functions....

Then free the library using
FreeLibrary((HMODULE)hDll);

Try it out.
VinExpert
0
 
Nicolay_ChCommented:
See MSDN this topic in "DLL Task":

Import into an Application Using __declspec(dllimport)
0
 
jaydeepAuthor Commented:
hi!
it gives an error for function name at compilation time.
If we directly call function,it is in DLL,naturally  program will give an error.there is no declaration of that function in program
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

How u r using the functions?
U have to have the function declaration for the function, then use
GetProcAddress(...), to get the address of the function. Then use that function.

Try it out.
VinExpert
0
 
jaydeepAuthor Commented:
HELLO
ave tried this way earlier.
i am writting code ,if possible correct it.
void (*pInsert)();//I guess here it is wronge,declaration of pointer
HINSTANCE      hf;
hf = ::LoadLibrary ("DLLTest3.dll");
pInsert =::GetProcAddress (hf,"nscpl");
pInsert->nscpl;//nscpl is name of function which returns       BOOL.
DLLTest3.dll i name of DLL which is in windows/system.
if possible pl do correct it
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Difficult to cxorrect without dll.

Try like this.

void (*pInsert)();//what is teh prototype of this function in dll?-it should match.
HINSTANCE hf;
hf = ::LoadLibrary ("DLLTest3.dll");
pInsert =::GetProcAddress (hf,"nscpl");
BOOL ret = pInsert();

Try it out and let me know the result asap.
VinExpert


0
 
jaydeepAuthor Commented:
Hi!
i did write code as following.
void (*pInsert)();//what is teh prototype of this function in dll?-it should match.
HINSTANCE hf;
hf = ::LoadLibrary ("DLLTest3.dll");
pInsert =::GetProcAddress (hf,"nscpl");
 pInsert();

//in dll function i made change that it returns void.i have just put messagebox to check.there no other code.

it gave me following error at line of GetProcAdress.
C:\jsw\test\testView.cpp(344) :
 error C2440: '=' : cannot convert from 'int (__stdcall *)(void)' to
'void (__cdecl *)(void)'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.


earlier also when i tried it was giving same error.pl try at your end.
0
 
Vinayak KumbarSr Program ManagerCommented:
HI,

Use the following style.
HINSTANCE hDLL;
FARPROC abc;
hDLL = LoadLibrary("DLLTest3.dlll");

abc = (FARPROC) GetProcAddress(hDLL, "nscpl");

abc();

FreeLibrary(hDLL);

Hope this helps,
VinExpert
0
 
brames74Commented:
U could put the the DLL which contains the function in the system32 directory. then the application program from which u want to call the function u can link to the dll by including the library file of the DLL in the project settings(link tab) of the application.

then u can call the function using dllexport function as some others have suggested.
hope this is what u asked for.
brames_99
0
 
jaydeepAuthor Commented:
Hi!
i want to call DLL dynamically,
i guess the solution which you have given  is used for static linking.
0
 
Nicolay_ChCommented:
Here:

see GetProcAddress() function of WinAPI

0
 
yazdchiCommented:
Hi Jaydeep,

In your comment you said "Hi!
i want to call DLL dynamically,
i guess the solution which you have given  is used for static linking.", I wasn't sure if you ment my proposed answer or not, but if you are then I'm not sure what you mean by my solusion being "used for static linking". The solution I gave was for Dynamically linking DLL.

I think you might be confused by the terms "Dynamic" and "Static"; I think you're talking about "Explicit" and "Implicit" Linking. (I myself had to look at a book for the following just to make sure I had the facts right).

When you create a Static library, the contents of the DLL is merged in with the EXE so the size of the EXE becomes larger and say if you had ten programs which where linked to this DLL statically, then it's like loading ten copies of the DLL is loaded into memory (this is much faster when initially running your program but it could be a waste of memory space).

But when a DLL is created Dynamically, then the code of the DLL does not become part of the EXE, hence a smaller sized EXE, and only one copy of the DLL is loaded into memory no matter how many EXEs need to use it. This is slower when initailly running the program becuase you have to not only load the EXE into memory but also all the DLLs that it uses it, but memory is not wasted if more than one EXE need to access the code within DLL.

Now as far as I know Explicit and Implicit linking are only when you create your DLL Dynamically and are not used when creating Static DLL.

"Explicit" linking is when the DLL is not loaded until you explicitly state when you want to load the library, using LoadLibrary() and then you have to get the address of the functions you want to call (before calling them) using the GetProcAddress() function as mentioned in the previous comments given by other people.

"Implicit" linking is the method I sugested. In Implicit linking only the import library file with a .LIB extension is statically linked (which is reletively has a small size) to the EXE and not the DLL. The ,lib file contains all the exported function addresses which means you don't have to mess around with GetProcAddress() every time you want to call a function; you just call them like any other function. In fact when you link your programs to shared MFC DLLs they are automatically lined Iplicitly which explains why we don't have to use GetProcAddress() when we call MFC functions. The only bad thing about Implicit linking that I can think of at the moment is that the DLL is loaded as soon as the EXE is run, and not only when you need it to; even if you use only one function from the DLL and only once in your program, still the DLL is loaded as soon as you run your EXE and it remains in the memory until all the EXEs that make use of this DLL are unloaded.

My personnal view is that it's better to loose a bit of performance if it means you don't have to mess aroud will LoadLibrary(), UnLoadLibrary() and GetProcAddress() all the time.

Hope this help and clears up a few thing, and sorry if the text was too long and gave you a head-ake, but it was the only way I could explain it.

Let me know if I can be of any more help.


Regards,
Mohsen
0
 
jaydeepAuthor Commented:
hi!Mohsen
Thanks for your suggestion.
now i will explain you ,why i need
explicit linking.
I am working on project which i want to
make it Multilengual (All europian languages).For that i want to call some
function.
In such situation i will have no. of DLL equal to no. of lanluages.Depending on present DLL seleted i have to call
it's function.
one more thing with help of some more guies like you,i have called DLL function without parameters & with parameters fot int & float.
i don't know why it's not working for char* & CString variables.
So i need explicit linking.

thanks

0
 
yazdchiCommented:
Hi Jaydeep,

Thanks for your reply, now I know what you want... Ok I'm not 100% sure about this but I have a strong feeling you can still use AFX_API_EXPORT to export your functions. The only difference is the way you load and call your functions. Here's a bit of code I got out of a book on linking and calling functions explicitly, personnaly I've never tried it so please let me know if it works.

First declare a pointer to a function with the same parameters and return type as the function you want to call; next load the library you want by calling LoadLibrary(), this will return a pointer to the DLL instance or NULL if it fails. The get a pointer to the function you want to call and assign it to the pointer to a function that you declared before using GetProcAddress(), agin check if pointer is NULL or not. Once you have done what you wanted to do and are finished with the loaded DLL, you MUST free it by calling FreeLibrary(). Here is the code (the function being called here is called InserNode):

void (*pfuncInsertNode)();
HINSTANCE hInstDll;

hInstDll = ::LoadLibrary("MyDll.DLL");
ASSERT(hInstDll != NULL);

pfuncInsertNode = ::GetProcAddress(hInstDll, "InsertNode");
if(pfuncInsertNode == NULL)
{
  FreeLibrary(hInstDll);
  AfxMessageBox("Unable to load 'MyDll.DLL'");
  return FALSE;
}

pfuncInsertNode();    // Call function
....
....
FreeLibrary(hInstDll);
return TRUE;


Hope this helps, please let me know if it works, I've never tried Explicit linking, thanks.

Regards,
Mohsen

0
 
jaydeepAuthor Commented:
thanks
but i have done this way,it works.
i  could do it for function without any parameter & function with int or float as parameter.
now i am facing problem,that i want to pass char* & Cstring object to fuction in DLL through my MFC program
thanks
0
 
yazdchiCommented:
Hi Jaydeep,

As far as I know there is no reason for you not to be able to pass char and CString as parameters, as long as both your DLL and your EXE support MFC (in the case of the CString); I've always been able to call functions from DLL which had char & CString as parameters (in Implicitlly linked DLLs but there is no reason why Explicit linking should be any different).

It is possible that your problem is some where else in the program. It might be helpfull if you could post the parts of the code that you're having problem with, so we could see exactly what it is you're trying to do and haw you're doing it.

Regards,
Mohsen
0
 
jaydeepAuthor Commented:
hi!Mohsen
before sending this reply ,i tried once here.it worked,i guess there was some small mistake.
are you working on VC++.
if you don't mind i would like to contact you.
my e-mail is jaydeepwagh@yahoo.com
0
 
yazdchiCommented:
Hi Jaydeep,

I'm glad you solved your problem, the answer to your question is yes, I do work with VC++, and no I don't mind if you were to contact me, if I can help you I will. My email is:

mohsen@yazdchi.demon.co.uk

Regards,
Mohsen
0
All Courses

From novice to tech pro — start learning today.