.net - Injecting a C# Forms application into another application -


there plenty of answers how inject dll process. how same thing c# forms application (exe) instead of dll.

basically want run in virtual address space of process. first allocate memory, create remote thread. how existing exe run in there? there limitations that, (could have running inside explorer.exe example)?

i did quite awhile ago own unmanaged app (without injection - not matters). once you've got unmanaged dll injected desired app's address space, should create dedicated thread, initialize com on (with coinitializeex or oleinitialize), following (error checks skipped brevity):

hmodule hmodmscoree = loadlibrary(_t("mscoree.dll"))  hresult (stdapicalltype *pcorbindtoruntimeex)(lpcwstr pwszversion, lpcwstr pwszbuildflavor, dword startupflags, refclsid rclsid, refiid riid, lpvoid far *ppv); get_proc_address(hmodmscoree, corbindtoruntimeex);  ccomqiptr<icorruntimehost> m_host; pcorbindtoruntimeex(null, null, 0, clsid_corruntimehost, iid_icorruntimehost, (void**)&m_host); m_host->start();  ccomqiptr<iunknown> unk; m_host->createdomainsetup(&unk); ccomqiptr<mscorlib::iappdomainsetup> domainsetup; unk->queryinterface(&domainsetup); domainsetup->put_applicationbase(curdir);  ccombstr appname; parseparam(m_commandline, cmdlineopt_appname, &appname); domainsetup->put_applicationname(appname);  ccombstr config; parseparam(m_commandline, cmdlineopt_configfile, &config); domainsetup->put_configurationfile(config);  unk.release(); m_host->createdomainex(m_managedapp, domainsetup, null, &unk); ccomqiptr<mscorlib::_appdomain> appdomain; unk->queryinterface(&appdomain); appdomain->executeassembly_2(m_managedapp, &m_exitcode); 

make sure dependency assemblies (if any) available in base folder (curdir in code).

edited: done .net 2.0. don't know if has changed since then. find more info on clr hosting here.

edited: get_proc_address this:

#ifdef _unicode     #define func_t(func) func##w     #define get_proc_address_t(mod, func) \         ((farproc&)p##func = ::getprocaddress(mod, #func "w")) #else     #define func_t(func) func##a     #define get_proc_address_t(mod, func) \         ((farproc&)p##func = ::getprocaddress(mod, #func "a")) #endif 

you'd need #include fusion.h , mscoree.h (can found in windows sdk) , #import mscorlib.tlb (for .net 2.0 c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.tlb).


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -