Go4Expert

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
    {
    public:
        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;
        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.

Iviglious 16Oct2006 02:13

Re: Creating a simple DLL
 
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;
}


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
 
Quote:

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
 
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

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
 
Pls
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.

falcon eyes 26Apr2007 18:21

Re: Creating a simple DLL
 
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

shabbir 26Apr2007 18:58

Re: Creating a simple DLL
 
Quote:

Originally Posted by falcon eyes
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

Please post your query as a separate thread with proper title. If you have anything relating to the article it should go here.

falcon eyes 27Apr2007 03:24

Re: Creating a simple DLL
 
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

shabbir 27Apr2007 07:05

Re: Creating a simple DLL
 
Its an application in Visual studio

falcon eyes 27Apr2007 23:21

Re: Creating a simple DLL
 
How can i reach it,i tried to find it but i'm failed can u pls tell me where can find this application

shabbir 28Apr2007 07:09

Re: Creating a simple DLL
 
Quote:

Originally Posted by falcon eyes
How can i reach it,i tried to find it but i'm failed can u pls tell me where can find this application

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.

falcon eyes 28Apr2007 16:32

Re: Creating a simple DLL
 
i understood thanks

parvez.yu 6Mar2008 14:53

Re: Creating a simple DLL
 
your code was really helpful even i could create my own dll


All times are GMT +5.5. The time now is 09:54.