GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


A Paste by Anonymous
// dll cpp file

const char *product_name = "name"; // Change to product name

// Exported proceedures
__declspec(dllexport) extern "C" const char * get_product_name()
    {
    return product_name;
    }


__declspec(dllexport) extern "C"  char * get_registeration_code(const char* user, char *buffer)
    {

    return buffer;
    }


/// header loader

#ifndef ___PLUGIN_H
#define ___PLUGIN_H

#include <windows.h>
#include <string>

class plugin
    {
    HMODULE dll;
    public: bool LoadPlugin( std::string libfile )
                {
                this->dll = LoadLibrary( libfile.c_str() );
                if( !this->dll )
                    {
                    LPVOID lpMsgBuf;
                    FormatMessage(
                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS,
                        NULL,
                        GetLastError(),
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                        (LPTSTR) &lpMsgBuf,
                        0,
                        NULL );

                    // Display the string...
                    std::string msg;
                    msg = "Error loading plugin: ";
                    msg.append(libfile);
                    msg.append("\nSystem Error: ");
                    msg.append( std::string( (const char*)lpMsgBuf));
                    // Free the buffer.
                    LocalFree( lpMsgBuf );

                    MessageBox( 0, msg.c_str() , "Error", MB_ICONERROR);

                    return false;
                    }

                this->get_registeration_code_ptr = (get_registeration_code_proc)GetProcAddress( this->dll, "get_registeration_code" );
                if( ! this->get_product_name_ptr  )
                    {
                    MessageBox( HWND_DESKTOP,  "Invalid plugin! cannot locate 'get_registeration_code_proc' " , libfile.c_str(), MB_ICONERROR);
                    return false;
                    }
                
                this->get_product_name_ptr       = (get_product_name_proc) GetProcAddress( this->dll, "get_product_name" );
                if( ! this->get_product_name_ptr )
                    {
                    MessageBox( HWND_DESKTOP, "Invalid plugin! cannot locate 'get_product_name_ptr' " , libfile.c_str(), MB_ICONERROR);
                    return false;
                    }

                return true;
                }

            void FreePlugin()
                {
                if( this->dll == INVALID_HANDLE_VALUE )
                    {
                    FreeLibrary(this->dll);
                    }
                }

            char* get_registeration_code(const char* user, char *buffer)
                {
                if( get_registeration_code_ptr )
                    {
                    get_registeration_code_ptr(user, buffer);
                    }
                return buffer;
                }
            const char * get_product_name(void)
                {
                if( get_product_name_ptr )
                    {
                    return get_product_name_ptr();
                    }
                return "Uninitalized";
                }
    

    private: typedef char * (__stdcall * get_registeration_code_proc) (const char *, char *);
             typedef const char * (__stdcall * get_product_name_proc)(void);


    private: get_registeration_code_proc    get_registeration_code_ptr;
             get_product_name_proc          get_product_name_ptr;

    public: plugin(): get_registeration_code_ptr(0), get_product_name_ptr(0)
                {// do nothing
                }
    };

#endif // ___PLUGIN_H