[C++/Win32] Plugin System Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 13
Reputation: webspy is an unknown quantity at this point 
Solved Threads: 0
webspy's Avatar
webspy webspy is offline Offline
Newbie Poster

[C++/Win32] Plugin System Problem

 
0
  #1
Nov 13th, 2006
I'm working on a plugin system for my application. The plugins are in the form of a dll loaded by my application using LoadLibraryEx. I store information about each plugin in an arrray of structs:
  1. typedef struct _InternalPluginInfo
  2. {
  3. UINT ID;
  4. HMODULE Module;
  5. int Flags;
  6. PluginInfo Info;
  7.  
  8. } InternalPluginInfo;
But when I try to remove a plugin from the array with the following function I get a "memory could not be read" error from Windows. When there is only 1 plugin in the array, it works. When there are 2, it fails with that error. When there are 3 or more it seems to work also (altought if there are a lot of plugins it fails again).

  1. int PluginManager::RemovePlugin(UINT ID)
  2. {
  3. err = 0;
  4. UINT index = FindPlugin(ID);
  5. if(err == 0)
  6. {
  7. UINT k = 0;
  8. InternalPluginInfo* temp = new InternalPluginInfo[Count - 1];
  9. if(!temp)
  10. {
  11. err = 2;
  12. return FAILURE;
  13. }
  14.  
  15. for(UINT i = 0; i < Count; i++)
  16. {
  17. if(Plugin[i].ID != ID)
  18. {
  19. temp[k].ID = Plugin[i].ID;
  20. temp[k].Flags = Plugin[i].Flags;
  21. temp[k].Module = Plugin[i].Module;
  22. temp[k].Info = Plugin[i].Info;
  23. k++;
  24. }
  25. }
  26.  
  27. if(Plugin[index].Info.PluginName)
  28. delete [] Plugin[index].Info.PluginName;
  29. if(Plugin[index].Info.PluginAuthor)
  30. delete [] Plugin[index].Info.PluginAuthor;
  31.  
  32. if(Plugin[index].Info.Callback)
  33. (Plugin[index].Info.Callback)(cPluginQuit, 0, 0);
  34.  
  35. FreeLibrary(Plugin[index].Module);
  36.  
  37. delete [] Plugin;
  38. Plugin = temp;
  39. Count = k;
  40.  
  41. return SUCCESS;
  42. }
  43.  
  44. err = 1;
  45. return FAILURE;
  46. }
I think the error occurs beacuse of this line temp[k].Module = Plugin[i].Module;, but I'm not sure. When I remove it from the code, everything seems to work fine.

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: [C++/Win32] Plugin System Problem

 
0
  #2
Nov 13th, 2006
this is obviously a c++ program so why don't you use a c++ container such as <map> to house the plugins? Other than that, its hard to say what your problem is because you did not post enough code. It would be helpful if you just zipped up all the files and attached it to your post.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 13
Reputation: webspy is an unknown quantity at this point 
Solved Threads: 0
webspy's Avatar
webspy webspy is offline Offline
Newbie Poster

Re: [C++/Win32] Plugin System Problem

 
0
  #3
Nov 13th, 2006
I've attached the whole source code (Code-Blocks project, both sources and binaries, plus a sample plugin included).

I'm still learning C++, but I didn't heard of C++ containers yet :rolleyes:. I just searched about them on the web and found out they're part of the Standard Template Library (heard of C++ templates, but know little about them). Could you please point me to some good C++ STL/Templates tutorials ?

Thanks for helping me.
Attached Files
File Type: zip Plugin Host.zip (619.4 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: [C++/Win32] Plugin System Problem

 
0
  #4
Nov 13th, 2006
Here is one you should bookmark.

Plugin.h should be using std::string objects instead of char* -- it will make your life a lot easier because you don't need to allocate memory for those strings. Look over your code and see where else you can substitute std::string for char*.

  1. typedef struct _PluginInfo
  2. {
  3. std::string PluginName;
  4. std::string PluginAuthor;
  5. int Flags;
  6. PluginCallback Callback;
  7.  
  8. } PluginInfo;
Last edited by Ancient Dragon; Nov 13th, 2006 at 2:58 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: [C++/Win32] Plugin System Problem

 
0
  #5
Nov 13th, 2006
Here is an example of how to use map. All it does is associate a number with an instance of PluginInfo object.

  1. #pragma warning(disable: 4786)
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6.  
  7. typedef enum _RequestCode
  8. {
  9. cPluginInit = 1,
  10. cPluginQuit = 2
  11.  
  12. } RequestCode;
  13.  
  14. typedef long (*ServiceFunc)(UINT, RequestCode, UINT_PTR, ULONG_PTR);
  15. typedef long (*PluginCallback)(RequestCode, UINT_PTR, ULONG_PTR);
  16.  
  17. typedef struct _PluginInfo
  18. {
  19. std::string PluginName;
  20. std::string PluginAuthor;
  21. int Flags;
  22. PluginCallback Callback;
  23.  
  24. } PluginInfo;
  25.  
  26. std::map<UINT,PluginInfo*> theList;
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32. PluginInfo* info = new PluginInfo;
  33. theList[1] = info;
  34. PluginInfo* pinfo = theList[1];
  35. return 0;
  36. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 13
Reputation: webspy is an unknown quantity at this point 
Solved Threads: 0
webspy's Avatar
webspy webspy is offline Offline
Newbie Poster

Re: [C++/Win32] Plugin System Problem

 
0
  #6
Nov 13th, 2006
Thanks. The problem was indead caused by PluginName and PluginAuthor not being allocated. I was expecting them to be allocated by the dll. They're now allocated by the application and the dll only has to fill them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC