Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Access DLL from C++ code

Posted on 2002-03-14
7
Medium Priority
?
249 Views
Last Modified: 2011-04-14
I have written a DLL and compiled it properly.  I want to be able to access the DLL file and use it in my C++ code.

//The Connection is the function I want to call from
// my DLL File
typedef int Connection(char,char,char,int,int);

void main(int argc, char *argv[]) {
    //Dial is the name of my DLL
    HINSTANCE hInstance = LoadLibrary("Dial");
    PROC p;

    if(hInstance != NULL)
     p=(PROC)::GetProcAddress hInstance,"Connection");

   //this isn't working...
   p(PHONENUMBER, USER, PWD, REDIAL, PAUSE);    
     
}


Does anyone have any suggestions..I'm a little bit confused on how to access the function.

Thanks. jcast17
0
Comment
Question by:jalgazi
  • 5
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6865960
Did you export the functions properly? In the DLL, you'll have to use

extern "C" __declspec ( dllexport) int Connection(char,char,char,int,int);
0
 

Author Comment

by:jalgazi
ID: 6865971
Yes, I did do that in my DLL file.

extern "C" __declspec(dllexport) int __cdecl Connection(char phonenumber[3], char user[20], char psw[20], int redial, int pause)
{
   .....
}

is what I did above correct
0
 
LVL 86

Expert Comment

by:jkr
ID: 6865975
Actually, you could also add a .def (module definition) file to your DLL project:

LIBRARY mydll

EXPORTS
    Connection
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 6865991
>>is what I did above correct

Not really - there is no error checking at all. Better make it read

typedef int (*Connection_Proc)(char,char,char,int,int);

void main(int argc, char *argv[]) {
   //Dial is the name of my DLL
   HINSTANCE hInstance = LoadLibrary("Dial.dll"); // Is the path to dial.dll OK?
   Connection_Proc p;

   if(hInstance != NULL) {
    p=(Connection_Proc)::GetProcAddress hInstance,"Connection");

  //this isn't working...
  *p(PHONENUMBER, USER, PWD, REDIAL, PAUSE);    
  }
}
0
 

Author Comment

by:jalgazi
ID: 6866017
It works, thanks.

I only had to remove the * infront of the *p(PHONENUMBER, USER, PWD, REDIAL, PAUSE);     It gave me compiling errors.

0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 6866035
>>It works, thanks.

Great. So I'll post this as an "answer" so that we can move this one to the PAQ section...

BTW, what about your other Q at http://www.experts-exchange.com/cplusprog/Q.20276477.html ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6866686
>> only had to remove the *

Sorry, yes, this should have read

(*p)(PHONENUMBER, USER, PWD, REDIAL, PAUSE);    

But it is supposed to work either way...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

971 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