Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Dll - How to ?

Hi
I am new to C++ programming and i am using Microsoft Visual C 6.

I need to create a dll with 2 functions.
I have had a look in c and i cannot use the MFC libraries so i have to use the Win32 Dynamic Link Library (i think)

The reason why i cannot use the MFC is because i need for my dll to be accessible from another application that has been written in c and does not use the Microsoft Foundation Classes.

So i please need help on setting up a basic dll with 2 functions .....
Help  :)

If anyone can give me really great advice and even sample code i will increase the points accordingly :)

thanx allot in advance

Craig
0
CraigLazar
Asked:
CraigLazar
  • 2
  • 2
1 Solution
 
jkrCommented:
mydll.h:
#ifdef _DLL // define this when building the DLL
#define __DYNLINK __declspec( dllexport)
#else
#define __DYNLINK __declspec( dllimport)
#endif

Some elaboration:
void __declspec(dllexport) MyFunc ();
created a MyFunc that is exported from a DLL
whereas
void __declspec(dllimport) MyFunc ();
created a MyFunc that is imported from a DLL

As you certainly don't want to use 2 different header files,
#ifdef _DLL // define this when building the DLL
#define __DYNLINK __declspec( dllexport)
#else
#define __DYNLINK __declspec( dllimport)
#endif

creates an export modifier '__DYNLINK' that expands to the correct expression depending on what your're building. You could use it e.g. this way:

dll.cpp
#define _DLL
#include "mydll.h"

so the MyFunc will be exported.

When building the application, simple use
app.cpp

#include "mydll.h"

LONG WINAPI WinMain( ...)
{
//...
}

BTW: If you want to make life easier & you are using a MS compiler,

#pragma comment(lib, "mydll.lib")

will automatically add the import library to your application.

Please ask if you need more information or some clarification!


0
 
CraigLazarAuthor Commented:
Adjusted points from 100 to 150
0
 
CraigLazarAuthor Commented:
Hi jkr's
this looks great is it possible to give me more information as in where i add the code, the differences between a cpp file and a header file , i know in c you have include librarys like stdio which is to initiate your standard input output .

anymore info would be great ....
i will try your sample code tonight, thanx again for the help

Craig
0
 
kishore_joshiCommented:
A non-MFC DLL is a DLL that does not use MFC internally, and the exported functions in the DLL can be called by either MFC or non-MFC executable files. Functions are usually exported from a non-MFC DLL using the standard C interface.

So u can build a regular dll with the help of MFC. The regular dll can be either statically Linked or Dynamically Linked. Dynamically Linked DLL takes Less memory than that of the static ones.

The steps are,
1) File--- New and select MFC-Appwizard DLL in the type of project Tab.. and     give a name to the project.
2)In step One-- Select the Regular DLL using MFC options....

3)Then in the .cpp file of this project put the decleration and defination of the functions that u want to put into the DLL using export Keyword....


4)void __declspec(dllexport) MyFun();
// return type underscore(twice) declspec(dllexport) FuncName(parameters.)

5) Then wirte the implimentation of this function in the same file..

6) compile and build the project.
7) build the project which should use this dll. In the project properties of this project, set the path for the .lib file.
8)Then copy the .dll file that is created into u r system directory and then u can have a call like this in the .c file......

9) do the following decleration and the call the function as if its defined in the same file......

void __declspec(dllimport) MyFun();

        Just try and let me know if u have any problems......
Bye


0
 
jkrCommented:
>>it possible to give me more
>>information as in where i add the
>>code, the differences between a cpp
>>file and a header file

The main difference between a header file and a cpp file is what you put in them - you can write an entire program in one .cpp or a .h file, it'd make no difference to the compiler. But the common understanding is this: A .cpp file holds the *implementation* of your program/library, whereas a header file comtains the definitions of the functions/classes/methods, so they can be used by several .cpp files.

To give you a slightly more outlined example:


mydll.h:
#ifdef _DLL // define this when building the DLL
#define __DYNLINK __declspec( dllexport)
#else
#define __DYNLINK __declspec( dllimport)
#endif

int __DYNLINK MyDll_Add ( int a, int b);


mydll.cpp :

#define _DLL
#include "mydll.h"

int MyDll_Add ( int a, int b)
{
 int nResult;

 nResult = a + b;

 return ( nResult);
}

myapp.cpp:

#include <stdio.h>
#include "mydll.h"

#pragma comment ( lib, "mydll.lib") // eventaually add the complete path here


void main( void)
{
  int n1 = 1;
  int n2 = 2;
  int n3;

   n3 = MyDll_Add ( n1, n2);

   printf ( "\nAdding %d and %d gives %d\n", n1, n2, n3);
}

0

Featured Post

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!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now