Solved

Dynamic Link of DLL func in CBuilder 3

Posted on 1998-04-21
4
269 Views
Last Modified: 2006-11-17
I want to Dynamically Load a DLL in C++ Builder   Ex:
 HINSTANCE hndl;
 FARPROC Func;
 hndl = LoadLibrary("c:\\wwwroot\\cgi-bin\\control.dll");
 Func = (FARPROC) GetProcAddress(hndl,"Passed");
 if(Func(Name.c_str(),Password.c_str())){ FreeLibrary(hndl); return 1;}//Passed
 else {FreeLibrary(hndl); return 0;}

I'm getting an error message saying "Too Many Arguments" to the DLL function even though the number of arguments is correct
Regards Matlock.
0
Comment
Question by:Matlock
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 1162642
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162643
The problem is that you've declared the function pointer as a pointer to function that takes no parameters.  Then you try to use it with two string parameters.  In c++ the function pointers must be used with the same number and type of parameters that they are declared with.  Since I asume that this is a win32 program, I'll drop the FAR stuff.  If I'm wrong we can put it back.  You want something like this.

typedef bool (*ProcPtrTyp)(char *,char *);
ProcPtrTyp Func;
Func = (ProcPtrTyp) GetProcAddress(hndl,"Passed");
if(Func(Name.c_str(),Password.c_str()))
{
   FreeLibrary(hndl);
   return 1;
}//Passed
else
{
   FreeLibrary(hndl);
   return 0;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1162644
I've created a typedefinition for a pointer to a function of the format you've needed.  This isn't necessary, but it makes things much easier to read.  Then I declared Func to have the appropriate type.  Note I didn't know what type the function returned so I declared it as bool.  It might be int.

An improvement that would generate smaller code would be to store the result of the function call in a local variable.  Then call free library.  Then determine your return value from the saved result of the function call.  This will give you only one return point from the function (returns are actually a little costly) and one free library call.  Makes the code easier to maintain too.
0
 

Author Comment

by:Matlock
ID: 1162645
Thanks nietod,
your added comments made it clear where I was going wrong
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question