Solved

Dll - How to ?

Posted on 2000-04-10
5
238 Views
Last Modified: 2013-12-14
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
Comment
Question by:CraigLazar
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
Comment Utility
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
 
LVL 4

Author Comment

by:CraigLazar
Comment Utility
Adjusted points from 100 to 150
0
 
LVL 4

Author Comment

by:CraigLazar
Comment Utility
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
 

Expert Comment

by:kishore_joshi
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

9 Experts available now in Live!

Get 1:1 Help Now