Solved

How to Access DLL from C++ code

Posted on 2002-03-14
7
211 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
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.

 
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

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

910 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

16 Experts available now in Live!

Get 1:1 Help Now