Solved

Dynamic Link of DLL func in CBuilder 3

Posted on 1998-04-21
4
267 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now