Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   Creating a simple DLL (http://www.go4expert.com/articles/creating-simple-dll-t907/)

shabbir 19Jun2006 13:40

Creating a simple DLL
1 Attachment(s)
This article shows a step by step technique to create your first DLL with VC++.

Steps to create your first DLL
  • Create a Win32 Dynamic Link Library project
  • Go to the workspace explorer. If its not visible in the GUI go to the View menu and click on WorkSpace or use Alt + 0 shortcut to make it visible.
  • Go the FileView tab and add a source file (Dll.cpp) and a header file(Dll.h).
  • Add following into Dll.h
    Code: CPP

    # include <afxwin.h>

    class _declspec(dllexport) CDll
        void ATestMethod();

  • Add following into Dll.cpp
    Code: CPP

    #include "dll.h"

    void CDll::ATestMethod()
        AfxMessageBox("CDll::ATestMethod of a dll is invoked");

  • Now create a new Win32 Console application and create it in such a manner that the output folder of both them remains the same so that you dont need to link the dll manually through the lib. Easy way of doing this is create a blank dsp file and insert that into the current workspace. Then add the main.cpp file into the project.
  • Add the following code into the main.cpp file
    Code: cpp

    #include "dll.h"

    void main()
        CDll dll;

  • Go to Project > Dependencies and set the Win32 console project is dependent on Win32 Dll project
Thats all is needed. Simple isnt it. I am attaching the sample for your convenience and explore more.

Iviglious 16Oct2006 02:13

Re: Creating a simple DLL
I understand this code, but I am wandering how to load and use a DLL file, that we have created.
For Example:

#define EXPORT extern "C" __declspec(dllexport)

EXPORT double sqr(double x)
  return x*x;

after i compile this code i recieve a makedll.dll file.
Now how can I use the function in this DLL file?
Something like this....?

#include <cstdlib>
#include <iostream>

#library makedll.dll
extern "C" double sqr(double);

int main(void)
    double num;
    printf("STARTing the program");
    printf("\n Enter a number:");
    printf("\n The square of the number is:%f",sqr(num));
    return EXIT_SUCCESS;

shabbir 16Oct2006 10:25

Re: Creating a simple DLL
It was a mistake from my part regarding the above article. Thanks for pointing out that. I have corrected it so that it should read what its intended to.

The sample was correct as far as implmentation goes but I put the wrong code snippet you need to put in main.cpp file and I have corrected that.

santhi 13Jan2007 15:27

Re: Creating a simple DLL
plz tell me that how create two workspace in project,like in simple DLL example

santhi 13Jan2007 15:31

Re: Creating a simple DLL
how can we create two workspaces in project

shabbir 13Jan2007 16:10

Re: Creating a simple DLL

Originally Posted by santhi
how can we create two workspaces in project

Dont jump into the article with your query. Instead have a seperate thread for your query in the Forum.

P.Luke 21Apr2007 04:51

Re: Creating a simple DLL
I am new to dll file. I get stuck with building a dll (C/C++) file to be call from C applications in VS.NET 2003 for my project. Does anyone know how to import it , something like extern "C" __declspec( dllexport ) int MyFunc(long parm1);. I try to do it but it just didn't work. Practical examples would be really good!! many thanks

shabbir 21Apr2007 08:39

Re: Creating a simple DLL
If you have any query with relating to article you should be discussing here and if you have a seperate query you can always use the Queries and Discussion forum for your queries. That helps in 2 ways.

1. All the experts can see and help you.
2. Other persons searching for something can also get the help.

falcon eyes 26Apr2007 05:26

Re: Creating a simple DLL
how can i know the functions contained in unknown dll file and use it in my code

shabbir 26Apr2007 07:02

Re: Creating a simple DLL
By viewing it in the dependency viewer.

All times are GMT +5.5. The time now is 02:12.