| | |
[C++/Win32] Plugin System Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I'm working on a plugin system for my application. The plugins are in the form of a dll loaded by my application using
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).
I think the error occurs beacuse of this line
Thanks in advance.
LoadLibraryEx. I store information about each plugin in an arrray of structs: C++ Syntax (Toggle Plain Text)
typedef struct _InternalPluginInfo { UINT ID; HMODULE Module; int Flags; PluginInfo Info; } InternalPluginInfo;
C++ Syntax (Toggle Plain Text)
int PluginManager::RemovePlugin(UINT ID) { err = 0; UINT index = FindPlugin(ID); if(err == 0) { UINT k = 0; InternalPluginInfo* temp = new InternalPluginInfo[Count - 1]; if(!temp) { err = 2; return FAILURE; } for(UINT i = 0; i < Count; i++) { if(Plugin[i].ID != ID) { temp[k].ID = Plugin[i].ID; temp[k].Flags = Plugin[i].Flags; temp[k].Module = Plugin[i].Module; temp[k].Info = Plugin[i].Info; k++; } } if(Plugin[index].Info.PluginName) delete [] Plugin[index].Info.PluginName; if(Plugin[index].Info.PluginAuthor) delete [] Plugin[index].Info.PluginAuthor; if(Plugin[index].Info.Callback) (Plugin[index].Info.Callback)(cPluginQuit, 0, 0); FreeLibrary(Plugin[index].Module); delete [] Plugin; Plugin = temp; Count = k; return SUCCESS; } err = 1; return FAILURE; }
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.
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.
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.
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.
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*.
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*.
C++ Syntax (Toggle Plain Text)
typedef struct _PluginInfo { std::string PluginName; std::string PluginAuthor; int Flags; PluginCallback Callback; } 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.
Here is an example of how to use map. All it does is associate a number with an instance of PluginInfo object.
C++ Syntax (Toggle Plain Text)
#pragma warning(disable: 4786) #include <windows.h> #include <iostream> #include <string> #include <map> typedef enum _RequestCode { cPluginInit = 1, cPluginQuit = 2 } RequestCode; typedef long (*ServiceFunc)(UINT, RequestCode, UINT_PTR, ULONG_PTR); typedef long (*PluginCallback)(RequestCode, UINT_PTR, ULONG_PTR); typedef struct _PluginInfo { std::string PluginName; std::string PluginAuthor; int Flags; PluginCallback Callback; } PluginInfo; std::map<UINT,PluginInfo*> theList; int main() { PluginInfo* info = new PluginInfo; theList[1] = info; PluginInfo* pinfo = theList[1]; return 0; }
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.
![]() |
Similar Threads
- bank system problem !!!! (C++ programming) (C++)
- Generic Host Process for Win32 Services encountered a problem and needed to close (Viruses, Spyware and other Nasties)
- Plugin system in c# (C#)
- WIN32 GUI application - Problem popping up dialog box (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with a 2D array from a text file
- Next Thread: need help in programing a matrix calculator
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






