Using class with DLL

This is a discussion on Using class with DLL within the C++ Programming forums, part of the General Programming Boards category; I have found a tutorial about how to use DLL, but it use just one function in a DLL. I ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Using class with DLL

    I have found a tutorial about how to use DLL, but it use just one function in a DLL.

    I am a beginner and i want to use a class in a DLL and not just a function.
    I have tried to create 2 programs (one for creating the DLL and one which use the DLL).
    But i have a problem.

    I use DevCPP.
    This is my programs:

    In a DLL project:
    print.cpp:
    Code:
    #include "print.h" 
    
      
    class __declspec(dllexport) Toto 
    { 
        public: 
        int x; 
        void Print(void) 
        { 
            printf("toto\n"); 
        
        } 
    };
    print.h
    Code:
    #ifndef PRINT_H 
    #define PRINT_H 
    
    #include <iostream> 
    using namespace std; 
    
    
    class __declspec(dllexport) Toto; 
    
    
    #endif
    -------------------------
    In the project which use the DLL (i have linked my .lib file created by the program above):

    main.cpp:
    Code:
    #include "print.h" 
    #include <conio.h> 
    
    int main() 
    { 
        Toto gogo; 
       gogo.Print(); 
       getch(); 
        
       return 0; 
    }
    print.h:
    Code:
    #ifndef PRINT_H 
    #define PRINT_H 
    
    #include <iostream> 
    using namespace std; 
    
    
    class __declspec(dllimport) Toto; 
    
    
    #endif
    I don't use source code created by DevCpp.
    When i compile the program which create the DLL it works.
    But when i compile the program which use the DLL i have this error:

    Compilateur: Default compiler
    Building Makefile: "D:\asauvegardes\cd_prog6\projets\devcpp\dll\test_ cpp\loader\Makefile.win"
    Exécution de make...
    make.exe -f "D:\asauvegardes\cd_prog6\projets\devcpp\dll\test_ cpp\loader\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"

    main.cpp: In function `int main()':
    main.cpp:8: aggregate `Toto gogo' has incomplete type and cannot be defined

    make.exe: *** [main.o] Error 1

    Exécution terminée



    How can i create and use a class with a DLL?

    Thanks.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Try removing the __declspec(dllexport) from print.cpp. When defining functions, you only need to put __declspec(dllexport) for the declaration, so I assume it's the same for classes.

    Code:
    //print.cpp
    #include "print.h" 
    
      
    class Toto 
    { 
        public: 
        int x; 
        void Print(void) 
        { 
            printf("toto\n"); 
        
        } 
    };
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    Sorry. I have made big mistake while writting my class.
    I works know.

    Here is my new source code:

    DLL:

    print.cpp:
    Code:
    #include "print.h"
    
     
    void Toto::Print(void)
    {
            printf("toto\n");
        
    }
    print.h:
    Code:
    #ifndef PRINT_H
    #define PRINT_H
    
    #include <iostream>
    using namespace std;
    
    class __declspec(dllexport) Toto
    {
        public:
        int x;
        void Print(void);
    };
    
    #endif
    ----------------------------------------------
    prog which use the DLL:

    main.cpp:
    Code:
    #include "print.h"
    #include <conio.h>
    
    int main()
    {
        Toto gogo;
    	gogo.Print();
    	getch();
    	
    	return 0;
    }
    print.h:
    Code:
    #ifndef PRINT_H
    #define PRINT_H
    
    #include <iostream>
    using namespace std;
    
    class __declspec(dllimport) Toto
    {
        public:
        int x;
        void Print(void);
    };
    
    #endif
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL question
    By cboard_member in forum Game Programming
    Replies: 1
    Last Post: 04-24-2006, 02:19 AM
  2. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21