visual studio - How to export C++ class as a dll? -


this question has answer here:

i came c#/java background, i'm trying figure out how create c++ dll behaves similar c# dll.

i have experimented __declspec(dllexport) , __declspec(dllimport), have managed work on static methods. sure due limited understanding.

how can export classes in c++ (in entirety including private members) , able instantiate them on referencing end c#? pointer online resources/tutorial it.

i started using mfc dll template, , have no idea 90% of them , why i'm inheriting cwinapp. tried tag class ccpppracticelibraryapp no longer compile.

// cpppracticelibrary.h : main header file cpppracticelibrary dll //   #pragma once  #ifndef __afxwin_h__     #error "include 'stdafx.h' before including file pch" #endif  #include "resource.h"       // main symbols  #ifdef ccpppracticelibraryapp_exports #define ccpppracticelibraryapp_api __declspec(dllexport)  #else #define ccpppracticelibraryapp_api __declspec(dllimport)  #endif  // ccpppracticelibraryapp // see cpppracticelibrary.cpp implementation of class //  class ccpppracticelibraryapp : public cwinapp { public:     ccpppracticelibraryapp();     static ccpppracticelibraryapp_api void sayhelloworld(); // overrides public:     virtual bool initinstance();      declare_message_map() }; 

the definition file:

//cpppracticelibrary.cpp : defines initialization routines dll.

#include "stdafx.h" #include "cpppracticelibrary.h"  #ifdef _debug #define new debug_new #endif  #define ccpppracticelibraryapp_exports    begin_message_map(ccpppracticelibraryapp, cwinapp) end_message_map()   // ccpppracticelibraryapp construction  ccpppracticelibraryapp::ccpppracticelibraryapp() {     // todo: add construction code here,     // place significant initialization in initinstance }  void ccpppracticelibraryapp::sayhelloworld() {     printf( "hello world"); }   // 1 , ccpppracticelibraryapp object  ccpppracticelibraryapp theapp;   // ccpppracticelibraryapp initialization  bool ccpppracticelibraryapp::initinstance() {     cwinapp::initinstance();      return true; } 

the client/referencing method

// testconsoleapplication.cpp : defines entry point console application. //  #include "stdafx.h" #include "testconsoleapplication.h" #include "cpppracticelibrary.h"  #ifdef _debug #define new debug_new #endif   // 1 , application object  cwinapp theapp;  using namespace std;  int _tmain(int argc, tchar* argv[], tchar* envp[]) {     int nretcode = 0;      hmodule hmodule = ::getmodulehandle(null);      if (hmodule != null)     {         // initialize mfc , print , error on failure         if (!afxwininit(hmodule, null, ::getcommandline(), 0))         {             // todo: change error code suit needs             _tprintf(_t("fatal error: mfc initialization failed\n"));             nretcode = 1;         }         else         {             // todo: code application's behavior here.             /*ccpppracticelibraryapp* testcallinglibrary =  new ccpppracticelibraryapp();             testcallinglibrary->sayhelloworld();*/             ccpppracticelibraryapp::sayhelloworld();         }     }     else     {         // todo: change error code suit needs         _tprintf(_t("fatal error: getmodulehandle failed\n"));         nretcode = 1;     }      return nretcode; } 

i able uncomment following lines in above code:

        /*ccpppracticelibraryapp* testcallinglibrary =  new ccpppracticelibraryapp();         testcallinglibrary->sayhelloworld();*/ 

from msdn

to export of public data members , member functions in class, keyword must appear left of class name follows:

class __declspec(dllexport) cexampleexport : public cobject { ... class definition ... }; 

also, consider there more ways this, .def-files. take time read through explanations on msdn-site.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -