c++: Load a .dll to new process -


we've plugin based application. each plugin exported dynamic library. due reasons have run each plugin new process ( separate executable each plugin required ). so, tring export .dll new process.

each plugin contains constructor, destructor , functions. without exporting works in way ( simplified code ):

hinstance lib = loadlibrary(boost_path_to_dll); // load plugin class interface library using factory functions typedef fooclass *(*createpluginfuncdef)(); // pointer constructor inside of plugin createpluginfuncdef createptr = (createpluginfuncdef)getprocaddress(lib, "create"); // pointer destructor inside of plugin typedef void(*destroypluginfuncdef)(fooclass*); destroypluginfuncdef destroyptr = (destroypluginfuncdef)getprocaddress(lib, "destroy");  // create new object = boost::shared_prt<fooclass>(plugincreatefactoryptr(), destroypluginfuncdef);  a->callsomefunctionfromplugin(); 

and plugin should exported new process. on web i've found example wanderley caloni jr.:

    /** * run createprocess specified parameters , handle allows * allocate memory , run threads process. */ createandgetprocessgodhandle(     lpctstr lpapplicationname,     lptstr lpcommandline     ) {     handle hret = null;     startupinfo si = { sizeof(si) };     process_information pi;     tchar tzapplicationname[max_path] = _t("");     bool bres;      // if string empty, invalidate pointer , use lpcommandline.     if (lpapplicationname && !*lpapplicationname)         lpapplicationname = null;     else         expandenvironmentstrings(lpapplicationname,         tzapplicationname,         sizeof(tzapplicationname));      bres =         createprocess(*tzapplicationname ? tzapplicationname : lpapplicationname,         lpcommandline,         null, null, true, create_new_console, null, null,         &si, &pi);      if (bres != false)     {         hret = openprocess(process_create_thread |             process_query_information |             process_vm_operation |             process_vm_write |             process_vm_read,             true,             pi.dwprocessid);          closehandle(pi.hprocess);         closehandle(pi.hthread);     }      return hret; }     /** * load dll in process. */ remoteloadlibrary(     handle hprocess,     lpctstr lpfilename     ) {     lpcstr tzloadlibrary = #ifdef unicode         "loadlibraryw" #else         "loadlibrarya" #endif         ;      hmodule hret = null;     tchar tzfilename[max_path] = { 0 };     lpvoid lpcodemem;     size_t stcodemem;      // make safe copy of module path opened.     stringcbcopy(tzfilename, sizeof(tzfilename), lpfilename);      // allocate memory in process.     stcodemem = (_tcslen(tzfilename) + 1) * sizeof(tchar);     lpcodemem = virtualallocex(hprocess,         null,         stcodemem,         mem_commit,         page_execute_readwrite);      if (lpcodemem != null)     {         // write tzfilename in allocated memory in process.         if (writeprocessmemory(hprocess,             lpcodemem,             tzfilename,             stcodemem,             &stcodemem))         {             handle hthr;             dword dwthrid;             farproc fploadlibrary;              fploadlibrary =                 getprocaddress(getmodulehandle(_t("kernel32")), tzloadlibrary);              // create remote thread loads tzfilename module.             hthr =                 createremotethread(hprocess,                 null,                 0,                 (lpthread_start_routine)fploadlibrary,                 (lpvoid)lpcodemem,                 0,                 &dwthrid);              if (hthr != null)             {                 // address module loaded.                 waitforsingleobject(hthr, infinite);                 getexitcodethread(hthr, (lpdword)&hret);                 closehandle(hthr);             }         }          virtualfreeex(hprocess, lpcodemem, 0, mem_release);     }      return hret; } 

and use in code (again simplified):

//typedef hinstance hmodule;  /* hmodules can used in place of hinstances */      int iret = 0; tchar tzprogpath[max_path] = _t("%comspec%"); tchar tzprogargs[max_path] = _t(""); tchar tzdllpath[max_path] = _t("paht_to_my_dll"); int opt;  /** load dll in process. */ handle hproc;  // start process , handle powers. // starts cmd ( know, how start hidded ?! ) hproc = createandgetprocessgodhandle(tzprogpath, tzprogargs);  // edit: // old post: seems work ( not sure ) // new post: hdll has value 0x59a80000{unused=???}, should know loaded // plugin ( so, problem inside of  remoteloadlibrary() ) hmodule hdll = remoteloadlibrary(hproc, tzdllpath);  // edit: // old post: , here part, not working ( crash call ) // new post: no crash, createprt has value = 0   // load plugin class interface library using factory functions typedef fooclass *(*createpluginfuncdef)(); // pointer constructor inside of plugin createpluginfuncdef createptr = (createpluginfuncdef)getprocaddress(hdll, "create");  // pointer destructor inside of plugin typedef void(*destroypluginfuncdef)(fooclass*); destroypluginfuncdef destroyptr = (destroypluginfuncdef)getprocaddress(hdll, "destroy"); ... 

can see mistake? ( may not possible call dll-functions in way ?!)

do may know better solution exporting dll new process?

thank !

cheers alex.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -