Solved

How to Access DLL from C++ code

Posted on 2002-03-14
7
221 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 250 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 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.

776 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