Solved

Call Dll of C++ from C++

Posted on 2002-06-13
10
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 138
cb: unreferenced local variable 11 72
C++ get user from AD  (VS6) 7 55
IdTCPClient1->Disconnect(); not working 3 62
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 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.

862 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

24 Experts available now in Live!

Get 1:1 Help Now