Solved

How to Access DLL from C++ code

Posted on 2002-03-14
7
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

632 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