Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Call Dll of C++ from C++

Posted on 2002-06-13
10
Medium Priority
?
200 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
[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
  • 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
Industry Leaders: 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 240 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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 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 clear a vector as well as how to detect empty vectors in C++.

610 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