Creating A DLL Of An Application

From WxWiki
Jump to navigation Jump to search

Creating a wxWidgets DLL application

Basically you need to define:

  • DllMain()

This function has to ifdef'ed for __WXMSW__, as it is Win-specific.

wxInitialize()(instead of wxEntry)

  • wxTheApp->OnExit()

wxUninitialize()


These functions may be found in app.cpp. There are 2 definitions based on the _WINDLL ifdef. Information may also be found by reading:

  • wx2/src/makefile.vc
  • wx2/src/makevc.env
  • wx2/src/msw/makefile.vc

Remember to use a .def-file or WXEXPORT with the classes/functions you need to export.

This did not work for me (mimo_at_gn.apc.org). Maybe you can add some more. I am trying to get hold of this guy -> Slavek who seems to have found a work around and planned to write a HOWTO. An example in this place would be very helpful.

Examples

wxJavaScript is said to be a good example of how to make a wxWidgets DLL

A simple example is available here: fromdll.zip

The fromdll.zip example above is compatible with wxWidgets versions up to and excluding 2.5.4, follow the link below for an example of a DLL application which links to more recent versions of wxWidgets:

http://wxforum.shadonet.com/viewtopic.php?p=5964#5964

Problems

Problems are reported with wxThread internal initialisation.

Also the 2.5.2 release has wxEntry decleration issues. Get the fixed app.h files from CVS for now.

This appear to be broken in 2.5.3, could someone do a update with the proper way to do this now.

Creating a wxWidgets DLL application with wxWidgets 2.6.2, with GUI support

Here is a simple example which allows you to store some dialogs in a wxWidgets DLL application the 2.6.2 way, and use that DLL in any other language. That code is directly insipired from fromdll.zip example above.

Header file : wxWidgetsDLL.h

#ifndef wxWidgetsDLLH
#define wxWidgetsDLLH

#pragma once

// This is the method i use to link the libs to my DLL project in
// c++ builder 6, not used with eg. mingw32/dev-cpp. If anybody has
// a hint on c++ builder 6 linking, just tell me

#ifdef CBUILDER
#pragma link "wxzlib.lib"
#pragma link "wxmsw26.lib"
#endif

#include "wx/wx.h"

#include <windows.h>
#include <process.h>

HANDLE ThreadId;

class wxDLLApp : public wxApp
{
    bool OnInit();
};

#endif wxWidgetsDLLH


Source file : wxWidgetsDLL.cpp

#include "wx/wx.h"
#include <process.h> 
#include "Unit1.h" 

IMPLEMENT_APP_NO_MAIN(wxDLLApp)

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    wxApp::SetInstance(new wxDLLApp());
    wxEntry(GetModuleHandle(NULL), NULL, NULL, SW_SHOW);
    return true;
}

#ifdef __WXMSW__
BOOL APIENTRY DllMain(HANDLE hModule,
    DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    { 
    case DLL_PROCESS_ATTACH:
        ThreadId = CreateThread(NULL,0,ThreadProc,NULL,0,NULL);
        break;
    case DLL_THREAD_ATTACH: break;
    case DLL_THREAD_DETACH: break;
    case DLL_PROCESS_DETACH:
        wxEntryCleanup();
        break; 
    }

    return TRUE;
}
#endif

// I could have #define MYEXPORT __stdcall __declspec(dllexport) but it works that way too... 
extern "C" __stdcall __declspec(dllexport) void DLLFunction(HWND handle) 
{
    wxDialog * dlg = new wxDialog(NULL, -1,
        "Hello World From DLL", wxDefaultPosition, wxDefaultSize,
        wxDEFAULT_DIALOG_STYLE, wxDialogNameStr);
    dlg->Show();
}

bool wxDLLApp::OnInit()
{
    return true;
}

I hope this helps, actually it worked for me, i ran some tests with the dll linked with a Delphi application, and the dialog box shows up. But please note that there are some issues when linking your DLL statically with wxWidgets. What i did is linking my DLL dynamically with wxWidgets, it seemed to solve all application initialization errors, so don't forget to define WXUSINGDLL in your compiler's settings, and compile your wxWidgets libs dynamically, at least for making wxWidgets DLLs.

Any feedback/comments : tomprod at wanadoo dot fr

Creating a wxWidgets DLL application with wxWidgets 2.8.9, with GUI support

I found none of hints above helpful for wxWidgets 2.8.9 built as DLL.

(From now on, library or dll always means your library, not wxWidgets library.)

App that works from function in static library seems to work fine also from function in dll. Example:

// This is in any .cpp file in library.
class MyApp: public wxApp
{
public:
    bool OnInit()
    {
        wxDialog* dlg = new wxDialog(NULL, -1,
            "Hello World From Library", wxDefaultPosition, wxDefaultSize,
            wxDEFAULT_DIALOG_STYLE, wxDialogNameStr);
        dlg->Show();
        return true;
    }
};

static wxAppConsole *wxCreateApp()
{
    wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "my app");
    return new MyApp;
}

// The only public function, declared also in library's header.
void RunApp()
{
    wxApp::SetInitializerFunction(wxCreateApp);
    wxEntry(0,NULL);
}

The only problem that causes apps started from dll crash (when mouse enters wx window for first time), while apps started from static lib run fine, is bug [1]. This was later fixed in 2.9.0, most likely by changeset 56863 [2]. Before this fix, my workaround for 2.8.9 was to add "static" at the beginning of src/msw/window.cpp line 5179, "wxDynamicLibrary dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM);". This workaround was widely tested and used without any problems in 32bit and 64bit apps, however it's unsafe hack compared to proper fix in 2.9.0.

Any feedback: stepan dot hrbek at lightsprint dot com