Page 1 of 2

trouble with wxDynamicLibrary

Posted: Fri Apr 18, 2008 12:17 pm
by fischi
hi all,

i'm new in this forum and hope somebody can help me. my problem is, that i can call a function in a dll from my application. but just after calling the dll the application does not refresh and does not stop by clicking finish (the button is just overpainted and you can't click it again). here are some code snips:

the call of the dll from the application cpp:

void TextFrame::OnClickButtonDLL(wxCommandEvent &event)
{
wxDynamicLibrary dll(wxT("Plugin_sourceforge.dll"));
if(dll.IsLoaded())
{
wxDYNLIB_FUNCTION(CreatePlugin_function,CreatePlugin,dll);
if(pfnCreatePlugin)
{
dll.Detach();
IEditorPlugin* plugin = pfnCreatePlugin();
plugin->EditNode();
}
}
dll.Unload();
}

the dll cpp:

extern "C" __declspec(dllexport) IEditorPlugin* CreatePlugin(){return new MyPlugin; }
IMPLEMENT_APP_NO_MAIN(PluginDLLApp)
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{ switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: { wxSetInstance((HINSTANCE)hModule);
int argc = 0;
char **argv = NULL;
wxEntryStart(argc, argv);
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH: wxEntryCleanup();
break;
}
return TRUE; }

void MyPlugin::EditNode()
{
wxMessageBox(wxT("Hallo."));
}

Posted: Tue Apr 22, 2008 9:55 pm
by Widgets
If you haven't found it already look at UpCase's wxPlugin
http://forums.wxwidgets.org/viewtopic.php?t=13178

Posted: Wed Apr 23, 2008 7:43 am
by fischi
thanks for the link, but i found this example already and i used it as model for my own projekt. but when i compile the wxplugin example the same error es described above occurs. its a really strange thing.

Posted: Wed Apr 23, 2008 8:30 am
by fischi
ok now i read the whole topic.;) hm they didn't find an answer to this problem. what a pity. but i keep on trying. maybe i find a solution. if yes i will post it here.

Posted: Wed Apr 23, 2008 4:21 pm
by Widgets
I've just started using the example code myself quite recently , but I have not seen the problem of not repainting etc, so I can't comment on that. OTOH, everything I've tried so far, including moving and resizing has worked without any problems at all.

The only possibility I can think of is the matter of linking or using wxWidgets. I've seen a lot of comments about the need to go for dynamic linking to wxWidgets and in fact to use this code for my needs I have switched from statically linked libraries to DLLs.

Posted: Thu Apr 24, 2008 10:19 am
by fischi
it works for you? hm, widgets, could you do me a favour plaese? maybe you can test my, to the basics, reduced code. i just want to know if it works for you. thank you very much.

main_app.h:

Code: Select all

#include "wx.h"

class TextEditorApp : public wxApp {
   public:   virtual bool OnInit();
};

DECLARE_APP(TextEditorApp)

class TextFrame : public wxFrame {
public:
   TextFrame( const wxChar *title, int xpos, int ypos,
              int width, int height );
   ~TextFrame();

   void OnClose(wxCloseEvent &event);
   void OnClickButtonDLL(wxCommandEvent &event);
   
      
protected:
   DECLARE_EVENT_TABLE()
private:
   wxButton* button;

   enum {
      
      wxBT_DLL
   };
};
main_app.cpp:

Code: Select all

#include "main_app.h"
#include "dynlib.h"
#include "DLLanfang.h"
#include "string.h"

#define wxUSE_DYNLIB_CLASS 1

IMPLEMENT_APP(TextEditorApp)

bool TextEditorApp::OnInit() {
   TextFrame *frame = new TextFrame(
      wxT("Basisanwendung"), 100, 100, 400, 300);
   frame->Show(TRUE);
   SetTopWindow(frame);
   return true;
}

TextFrame::TextFrame( const wxChar *title,
    int xpos, int ypos, int width, int height )
    : wxFrame( (wxFrame *) NULL, -1, title, 
                wxPoint(xpos, ypos), wxSize(width, height))
{

button = new wxButton(this,wxBT_DLL,wxT("DLL Aufruf"));

  }

TextFrame::~TextFrame() { }

BEGIN_EVENT_TABLE(TextFrame, wxFrame)
   EVT_CLOSE(TextFrame::OnClose)
   EVT_BUTTON(wxBT_DLL, TextFrame::OnClickButtonDLL)
   
END_EVENT_TABLE()

void TextFrame::OnClose(wxCloseEvent& event) {
   
   Destroy();
   
  }
void TextFrame::OnClickButtonDLL(wxCommandEvent &event)
{
	wxDynamicLibrary dll(wxT("Plugin.dll"));
	
	if(dll.IsLoaded())
	{
		wxDYNLIB_FUNCTION(CreatePlugin_function,CreatePlugin,dll);
		
		if(pfnCreatePlugin)
		{
			
			dll.Detach();
			
			IEditorPlugin* plugin = pfnCreatePlugin();
			
			plugin->EditNode();
						
		}
	}

dll.Unload();}
plugin.h:

Code: Select all

#include "DLLanfang.h"

class MyPlugin : public IEditorPlugin
{
public:
	virtual void EditNode();
	
	};

class PluginDLLApp : public wxApp													
	{																				
		bool OnInit() {return true;}												
	};
plugin.cpp:

Code: Select all

#include "plugin.h"

	extern "C" __declspec(dllexport) IEditorPlugin* CreatePlugin(){return new MyPlugin; }		
															
	IMPLEMENT_APP_NO_MAIN(PluginDLLApp)												
																					
	BOOL APIENTRY DllMain( HANDLE hModule,											
                       DWORD  ul_reason_for_call,									
                       LPVOID lpReserved											
	                                         )										
	{																				
		    switch (ul_reason_for_call)												
			{																		
                case DLL_PROCESS_ATTACH:											
                {       wxSetInstance((HINSTANCE)hModule);							
                        int argc = 0;												
                        char **argv = NULL;											
                        wxEntryStart(argc, argv);									
                }																	
                break;																
																					
                case DLL_THREAD_ATTACH:												
                break;																
																					
                case DLL_THREAD_DETACH:												
                break;																
																					
                case DLL_PROCESS_DETACH:											
                 wxEntryCleanup();
				break;																
			}																		
		return TRUE;																
	}																				

void MyPlugin::EditNode()
{
	
	wxMessageBox(wxT("Hallo."));
	
   }

DLLanfang.h:

Code: Select all

#include "wx.h"

class IEditorPlugin : public wxEvtHandler
{
public:
		virtual void EditNode()=0;
	};

typedef IEditorPlugin* ( *CreatePlugin_function)();

Posted: Thu Apr 24, 2008 10:22 am
by fischi
oh forgotten to say, i linked wxWidgets as dll too. otherwise it won't work.

Posted: Thu Apr 24, 2008 4:09 pm
by Widgets
I compiled the whole thing with my VS 2005 setup and I can't see any problems with it at all.
I can move, resize or minimize and restore the main window, before or after I make the Hallo box pop up.
I'll attach my zipped up 'solution'
If I missed anything, let me know.

Posted: Thu Apr 24, 2008 7:16 pm
by fischi
thank you very much widgets, but the problem is still the same. i really don't know what this could be. but thank you for your help.

Posted: Thu Apr 24, 2008 9:57 pm
by Widgets
Interesting; mine shows no such problems.

Actually I hadn't tied what your picture shows, but I have just run it again and I can move (both main frame and) the dialog pop-up all over the screen, including the application frame and it always redraws things as they should be.

When run under the debugger it does produce some memory leaks, which I have not tried to investigate.

The only thing remotely suspicious was that I had to fix some of the header includes, which in your code were different from the convention I saw used in the general wxWidgets code I've seen.

Posted: Tue Apr 29, 2008 10:47 am
by fischi
hey widgets, what version of visual c++ 2005 express do you use? i use version 8.0.50727.762 (SP.050727-7600) maybe this could be a reason why it is not working. (and my .NET Framework version is 2.0.50727 SP1)

Posted: Tue Apr 29, 2008 3:37 pm
by Widgets
My Visual Express says: 8.0.50727.42,
the .Net Framework:2.0.50727 SP1.

I rather doubt that that difference will account for the problem.

Did you try to compile the code using the solution file from the zip file I attached previously>

Posted: Wed Apr 30, 2008 6:28 am
by fischi
yes i compiled it. the output you can see above in the picture. its really strange.

Posted: Wed Apr 30, 2008 6:59 am
by fischi
but i had to make a new projekt using your files, because with the sln file the compiler said, that he couldn't find _strdup in buffer.h .

Posted: Wed Apr 30, 2008 3:38 pm
by Widgets
I have had problems with deprecated functions such as strdup etc because the compiler warns about the possibility of buffer overruns when they are used. MS has some replacement functions they recommend and if you're not concerned about portability to *nix, you may as well use them.
You can avoid the warnings by adding /wd4996 to the compiler command line ( or with an equivalent pragma directive)

As for the basic problem, I have no real idea what might be the cause :-(

If you care to, zip up the whole basic project including the .sln & project files and I'll try to run them here.

Another 'silly' question: You have compiled both the main app and the plugin as using wxWidgets as a DLL??

What it looks like to me is that somehow the event handlers in the two parts seem to get disconnected, so that the main app no longer gets a chance to do its thing in updating the screen while the dialog is up.