Link to home
Start Free TrialLog in
Avatar of jaydeep
jaydeep

asked on

Calling DLL dynamcally from MFC

Hello,
Earlier i had give question how to call DLL dynamically from MFC.Now i am facing problem when i call function from DLL with parameters.
It gives an error
error C2197: 'int (__stdcall *)(void)' : too many actual parameters.
can anybody help me in solving this problem
thanks
Avatar of jaydeep
jaydeep

ASKER

following is code
int z = 9;
HINSTANCE hDLL;
FARPROC abc;
hDLL = LoadLibrary("mydll");
abc = (FARPROC) GetProcAddress(hDLL, "DisplayString");
abc(z);
FreeLibrary(hDLL);
DisplayString is fun from DLL which take int as parameter.       
ASKER CERTIFIED SOLUTION
Avatar of akalmani
akalmani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi JayDeep,

The function declaration is not proper. thats why it is giving that error. My that code was for the function which takes no parameters. U can't apply the same function declaration for the functions with parameters. U have to have the maching function declarations.

Modify Ur code like

int z = 9;
HINSTANCE hDLL;       
void (PASCAL *abc)(int);//return type-funct pointer-parameter list
hDLL = LoadLibrary("DlgInDLL");
(FARPROC&)abc = GetProcAddress(hDLL, "MyFunct");
abc(z);
FreeLibrary(hDLL);

That will solve Ur problem.
Cheers,
VinExpert
Hi Jaydeep !!
  I forgot to mention u that error was due to DLL code
i.e u forgot to include extern "C" before the definition of exported function.
Please write extern "C" as it gives a C++ naming convention and also creates a .lib file
Avatar of jaydeep

ASKER

Hi!VinExpert
it worked fine.i have one question in mind.
You did not typecast the GetProcAddress this time but you typecat the pointer abc ,i would like to know reason of it.
If it's possible for you,send me your e-mail address,i would like to contact you.mine is jaydeepwagh@yahoo.com
thanks
Hi,

My email ID is k_bbb100@hotmail.com. U can contact me at any time. I will try to help U.

Cheers,
VinExpert
Avatar of jaydeep

ASKER

Hey VinExpert! thanks
there is one more querry ,why does not that code wrk for char* ,CString variables,i am trying to get info through help.
pl do contact
thanks
jaydeep
Hi,

have u changed it to
void (PASCAL *abc)(char *);?

VinExpert
Avatar of jaydeep

ASKER

ya!i have written following code.
HINSTANCE hDLL;
void (PASCAL *abc)(char*);
hDLL = LoadLibrary("mydll");
(FARPROC&)abc = GetProcAddress(hDLL, "String");
abc("Jaydeep");
FreeLibrary(hDLL);