Link to home
Start Free TrialLog in
Avatar of learn
learn

asked on

Call Dll of C++ from C++

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.
Avatar of jkr
jkr
Flag of Germany image

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.
Avatar of learn
learn

ASKER

Sorry, you are to quick!!
I cannot find any head file and lib file after I compiling the dll .....
You will have to create that header file. The .lib file should be in the same directory as the DLL.
Avatar of learn

ASKER

Sorry again, I found test.lib but no test.h

Can I write a text.h myself as text format?
Avatar of learn

ASKER


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 :-)

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'
Avatar of learn

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Anything else you need to know?
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