Solved

Call Dll of C++ from C++

Posted on 2002-06-13
10
191 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

19 Experts available now in Live!

Get 1:1 Help Now