Solved

Dynamic Link of DLL func in CBuilder 3

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

930 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

11 Experts available now in Live!

Get 1:1 Help Now