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
jaydeepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jaydeepAuthor Commented:
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.       
0
akalmaniCommented:
Hi JayDeep !!
 
The DLL code looks like this for MyDLL

extern "C" __declspec(dllexport) void MyTest(int i)
{
     //Do whatever u want with this int i
    //If u want CString instead of integer u can pass but change the
   //declaration      
}

compile this code and dump the DLL iun system directory or
else u can give path while loading the DLL


The code where u want to call the DLL

typedef void (CALLBACK * MYTESTFN)(int);
      
MYTESTFN lpfnDll;
HINSTANCE hInstance = LoadLibrary("MyDLL");
lpfnDll = (MYTESTFN)::GetProcAddress(hInstance,"MyTest");
lpfnDll(9);
FreeLibrary(hInstance);

Hope this helps u..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vinayak KumbarSr Program ManagerCommented:
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

akalmaniCommented:
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
0
jaydeepAuthor Commented:
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
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

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

Cheers,
VinExpert
0
jaydeepAuthor Commented:
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
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

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

VinExpert
0
jaydeepAuthor Commented:
ya!i have written following code.
HINSTANCE hDLL;
void (PASCAL *abc)(char*);
hDLL = LoadLibrary("mydll");
(FARPROC&)abc = GetProcAddress(hDLL, "String");
abc("Jaydeep");
FreeLibrary(hDLL);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.