Solved

Call Dll of C++ from C++

Posted on 2002-06-13
10
196 Views
Last Modified: 2010-05-18
Hi Experts,

From VC++6.0(Win32 Console Application), I compiled a DLL, test.dll including two files:

test.def:
LIBRARY test
DESCRIPTION test
EXPORTS
     myDllFunction @1
---------------------------
test.cpp:
int _stdcall myDllFunction(int s1)
{     return s1;
}

Now, can you show me the code to call the function myDllFunction(int s1) from VC++ Win32 Console Application?
I hope, we can call that directly by including a head file/lib file.....

Thank you in advance.
0
Comment
Question by:learn
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7075772
if you have a 'test.h' like e.g.

test.h:

int _stdcall myDllFunction(int s1);

all you need is the import library (test.lib) that is created when you build the DLL:

app.cpp:

#include "test.h"

#pragma comment ( lib, "test.lib") // tell the linker that we need test.lib

void main () {

 myDllFunction ( 1);
}

Make sure that both test.h and test.lib are accessible from the other project.
0
 

Author Comment

by:learn
ID: 7075781
Sorry, you are to quick!!
I cannot find any head file and lib file after I compiling the dll .....
0
 
LVL 86

Expert Comment

by:jkr
ID: 7075792
You will have to create that header file. The .lib file should be in the same directory as the DLL.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:learn
ID: 7075794
Sorry again, I found test.lib but no test.h

Can I write a text.h myself as text format?
0
 

Author Comment

by:learn
ID: 7075808

Can I write test.h like this:

#pragma comment ( lib, "test.lib") // tell the linker that we need test.lib
int _stdcall myDllFunction(int s1);

So, the user doesn't need to put
#pragma comment ( lib, "test.lib")
into his app.cpp :-)

0
 
LVL 86

Expert Comment

by:jkr
ID: 7075820
Sure, this is a plain C/C++ header file - e.g. like this:

//test.h:
#ifndef TEST_H
#define TEST_H

int _stdcall __declspec ( dllimport) myDllFunction(int s1);

#endif

Actually, if you wanted to use the test.h header file in both the application and the DLL (which is recommended, you would

//test.h:
#ifndef TEST_H
#define TEST_H
#ifdef TEST_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
#endif

int _stdcall __DYNLINK myDllFunction(int s1);

#endif

Then, you'd add a '#define TEST_DLL' to 'test.cpp'
0
 

Author Comment

by:learn
ID: 7076008
Hi,

Thank you again.

>#ifndef TEST_H
I guess that said 'if not define  TEST_H then', but
1. what is TEST_H?
2. I cannot see and test.h in your last comment, how VC++ knows the .lib file?
0
 
LVL 86

Accepted Solution

by:
jkr earned 60 total points
ID: 7076038
#ifndef TEST_H
//...
#endif

is usually used to guard header files against multiple inclusions (can easily happen in a growing project) which would result in errors. This makes the compiler only recognize the code in between when it 'sees' it the first time.

>>2. I cannot see and test.h in your last comment, how VC++ knows the .lib file?

You are absolutely right, you could make it read

//test.h:
#ifndef TEST_H
#define TEST_H
#ifdef TEST_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
#pragma comment ( lib, "test.lib")
#endif

int _stdcall __DYNLINK myDllFunction(int s1);

#endif
0
 
LVL 86

Expert Comment

by:jkr
ID: 7138466
Anything else you need to know?
0
 
LVL 11

Expert Comment

by:griessh
ID: 7262279
Dear learn

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

749 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