Solved

How to Access DLL from C++ code

Posted on 2002-03-14
7
202 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

20 Experts available now in Live!

Get 1:1 Help Now