[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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
0
jaydeep
Asked:
jaydeep
  • 4
  • 3
  • 2
1 Solution
 
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
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now