2015年11月7日 星期六

Visual Studio C++ 2012 DLL V.S. C++ Builder XE DLL




  1. VC dll

    These is sample code for Created C Style DLL. Especially, the implementing function is used C++ class. If any Result want to catch,  using Pointer(call by pointer) or reference (call by reference) is enabled. Any "Create Pointer" in "VC dll" is only deleted by VC dll itself.

    //GlobalHeader.h
    #pragma once

    class A_Header
    {
    public:
              A_Header() {};
              virtual ~A_Header() {};
              virtual int ADD( int a, int b)=0;
    }
    //-------------------------------------------------------------------------------------------------------------
    #ifdef DLLDIR_EX
       #define API __declspec(dllexport)   // export DLL information
    #else
       #define API __declspec(dllimport)   // import DLL information
    #endif

    typedef void * PTR;

    extern "C"
    {
              API PTR DEMO_Create();
              API void DEMO_Free(PTR aThis);
              //Define Functions
              API int DEMO_ADD( PTR aThis, int a, int b);
    }



    //GlobalHeader.cpp
    #include " GlobalHeader..h"
    #include "TEST.h"
    #define THIS reinterpret_cast< Header *>( aThis )
    extern "C"
    {
              //-------------------------------------------------------------------------------------------------
              PTR DEMO_Create()
              {
                       return new TEST();
              }
              //-------------------------------------------------------------------------------------------------
              void DEMO_Free( PTR aThis)
              {
                       delete THIS;
              }
              //-------------------------------------------------------------------------------------------------
              int DEMO_ADD( PTR aThis, int a, int b)
              {
                       return THIS->ADD( a, b);
              }
    }
      
    //TEST.h
    //Define Class and Implement

    #include "GlobalHeader..h"
    #include <iostream>
    class TEST :: public A_Header
    {
              TEST();
              virtual ~TEST();
              virtual int ADD( int a, int b);
    }

    //TEST.cpp
    TEST::TEST() { std::cout << “create ptr” << endl; }
    TEST::~TEST() { std::cout << “delete ptr” << endl; }
    int TEST::ADD( int a, int b) { return (a + b); }
  2. C++ builder

    How to used dll of step1? In below demo, the name of dll is test.dll.

    2.1. Getting c++ builder style lib, entered below command in command window.
    implib -a btest.lib test.dll
    2.2. Include GlobalHeader.h
    2.3. Create PTR
    PTR ptr = DEMO_Create();
    2.4. Used Function
    int result = DEMO_ADD( 1, 2);
    2.5. Delete PTR
    DEMO_Free(ptr);


沒有留言:

張貼留言