Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Making a dll

Posted on 2002-07-15
4
Medium Priority
?
372 Views
Last Modified: 2010-04-01
I've spent ages looking through MSDN and the web trying to work out how to create a dll file with functions that can be called from other programs but i just couldnt get the hang of it! If one of you would be so kind to either explain that to me or post some proper sample code i'd be very grateful! ThanX in advance...
P.S: I'm using VS6 ;)
0
Comment
Question by:Professor
[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
4 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7153856
Step 1 :-  Create a Win32 Dynamic Link Library project and add a .cpp & a .h file.

     Step 2 :-  In  the .cpp file, create a class instantiated from the CWinApp file.

             
               # include <stdafx.h>
               # include "SourceFile.h"

               class CDllApp : public CWinApp
               {
                 public:
     
                    CDllApp::CDllApp()
                    {
                         Calc(0,0);
                    }

                 DECLARE_MESSAGE_MAP()
               };

               BEGIN_MESSAGE_MAP(CDllApp,CWinApp)

               END_MESSAGE_MAP()

               CDllApp DllObject;    

     Step 3 :-  In the .h file (Here it is SourceFile.h) define the functions to be used.
             Also specify the "dllexport" value for the _declspec function.

               extern "C" _declspec(dllexport) int Calc(char no1,char no2)
               {
                    char result;
                    result = no1 + no2;
                    return result;
               }

     Step 4 :-  Then Compile the Dll.

     Step 5 :-  Then Create a normal Win32 Application with a .cpp file & a .h file.

     Step 6 :-  In the .h file, ( Here it is AppHeader.h )declare the function
             with the dllimport value of _declspec

             extern "C" _declspec(dllimport) Calc(int FirstValue,int SecondValue);

     Step 7 :-  In the .cpp file, use the function.

               # include
               # include "AppHeader.h"

               class MainFrame : public CFrameWnd
               {
                 public:

                    MainFrame()
                    {
                         Create(0,"Trial");
                    }

                    void OnLButtonDown(UINT nFlags,CPoint point)
                    {
                         int res;
                         char str[5];
                         res = Calc(998,226);
                         sprintf(str,"%d",res);
                         MessageBox(str);
                    }

                    DECLARE_MESSAGE_MAP()
               };


     Step 8 :-  In the Link tab of the "Project->Settings" Dialog, Go to the Text Box labelled
             "Object / Library Modules" and specify the path of the Dll File.

             Then copy the compiled dll file to your current app path directory and run
             the program.

             Note :- The DLL file may not be visible due to the File View Options in the
             Windows folder. So, U can either go to the DOS Prompt and copy the file
              or enable the setting "Show all files" in Windows explorer to copy the file.

     Note :-    To Create a DLL that uses MFC,See the following example.Note that extern "C"        
 has not been used and the macro  AFX_MANAGE_STATE(AfxGetStaticModuleState()); has been
 used to implement MFC.    

             _declspec(dllexport)CString Display(CString a,CString b)
             {
               AFX_MANAGE_STATE(AfxGetStaticModuleState());
               CString Str;
               Str = a + b;
               return Str;
             }


Good Luck
0
 
LVL 22

Expert Comment

by:ambience
ID: 7153959
here is a simpler way. Create a Win32 regular DLL project that exports some symbols using AppWizard.

The rest should be self explaining by looking at the generated code which by the way is a lot simpler. The above post seems to be using MFC and unless you have some expertise you might find hard to understand what it takes to write a simple dll.

to export a function or a symbol you need to add __declspec(dllexport) to its definition. for ex.

void __declspec(dllexport) sayHello()
{
}

int __declspec(dllexport) myVar;

if you want your symbols to be accesible from non C++ clients you also need to add extern "C" like

extern "C"
{
void __declspec(dllexport) sayHello()
{
}
}

and that is all. ofcourse when you want to use a dll you need to do the inverse i.e. dllimport. you need to prototype your functions or import variables like

void __declspec(dllimport) sayHello();
int __declspec(dllimport) myVar;

and then use them like you normaly do. Its always a good idea to put the protypes or definitions inside a common header that is shipped with dlls, and is used both by the dll itself and the client. This is waht you will see in the sample generated app.

hope this helps


0
 
LVL 1

Accepted Solution

by:
VR_ earned 1000 total points
ID: 7155675
Hi Professor just try this link:

http://www.functionx.com/visualc/applications/staticdll.htm

I began from this
0
 

Author Comment

by:Professor
ID: 7184599
I really appreciate your help and the tutorial is really great... As I have to many points anywayz... Ill give you 250 for your answer ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

719 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