Creating a simple DLL

Discussion in 'MFC' started by shabbir, Jun 19, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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:
      # include <afxwin.h>
      
      class _declspec(dllexport) CDll
      {
      public:
      	void ATestMethod();
      };
      
    • Add following into Dll.cpp
      Code:
      #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:
      #include "dll.h"
      
      void main()
      {
      	CDll dll;
      	dll.ATestMethod();
      }
      
    • 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.
     

    Attached Files:

  2. Iviglious

    Iviglious New Member

    Joined:
    Oct 15, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Soft Systems
    Location:
    Bulgaria
    Home Page:
    http://www.pegashometour.com
    Hallo,
    I understand this code, but I am wandering how to load and use a DLL file, that we have created.
    For Example:
    Code:
    //makedll.cpp
    #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....?
    Code:
    //testdll.cpp
    #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:");
        scanf("%f",&num);
        printf("\n The square of the number is:%f",sqr(num));
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     
    Last edited by a moderator: Oct 16, 2006
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  4. santhi

    santhi New Member

    Joined:
    Jan 12, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    plz tell me that how create two workspace in project,like in simple DLL example
     
  5. santhi

    santhi New Member

    Joined:
    Jan 12, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    how can we create two workspaces in project
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Dont jump into the article with your query. Instead have a seperate thread for your query in the Forum.
     
  7. P.Luke

    P.Luke New Member

    Joined:
    Apr 20, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    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
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  9. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Pls
    how can i know the functions contained in unknown dll file and use it in my code
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    By viewing it in the dependency viewer.
     
  11. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    could u tell me please how to do it in some details,cause i have a dll file which deal with parallel ports but i don't know how to use it cause i don't know any thing about the inside functions
    Regards
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please post your query as a separate thread with proper title. If you have anything relating to the article it should go here.
     
  13. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    pls shabbir
    is the dependency viewer a tool in visual studio or it's external application if so could u tell me from where i can download it cause i have a dll file dealing with parallel port but i don't know any thing about the functions it contains
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its an application in Visual studio
     
  15. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    How can i reach it,i tried to find it but i'm failed can u pls tell me where can find this application
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Again I will tell you should have separate thread for the query and not use the same article for it. If you are not willing to create the thread sorry but I cannot just answer it here.
     
  17. falcon eyes

    falcon eyes New Member

    Joined:
    Apr 8, 2007
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    i understood thanks
     
  18. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    your code was really helpful even i could create my own dll
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice