pinvoke - Passing multiple parameters using CreateRemoteThread in C# -
my goal call function in remote process using p/invoke in c# (createremotethread). problem function takes more 1 parameter. there way pass multiple parameters function?
[dllimport("kernel32.dll", setlasterror = true, exactspelling = true)] static extern intptr openprocess(int dwdesiredaccess, bool binherithandle, int dwprocessid); [dllimport("kernel32.dll", setlasterror = true, exactspelling = true)] static extern intptr virtualallocex(intptr hprocess, intptr lpaddress, uint dwsize, allocationtype flallocationtype, memoryprotection flprotect); [dllimport("kernel32.dll", setlasterror = true)] static extern bool writeprocessmemory(intptr hprocess, intptr lpbaseaddress, intptr lpbuffer, uint nsize, out uintptr lpnumberofbyteswritten); [flags] public enum allocationtype { commit = 0x1000, reserve = 0x2000, decommit = 0x4000, release = 0x8000, reset = 0x80000, physical = 0x400000, topdown = 0x100000, writewatch = 0x200000, largepages = 0x20000000 } [flags] public enum memoryprotection { execute = 0x10, executeread = 0x20, executereadwrite = 0x40, executewritecopy = 0x80, noaccess = 0x01, readonly = 0x02, readwrite = 0x04, writecopy = 0x08, guardmodifierflag = 0x100, nocachemodifierflag = 0x200, writecombinemodifierflag = 0x400 } [structlayout(layoutkind.sequential, pack=1] public struct remotethreadparams { [marshalas(unmanagedtype.u1)] public byte param1; [marshalas(unmanagedtype.i4)] public int param2; ... } [dllimport("kernel32")] public static extern intptr createremotethread( intptr hprocess, intptr lpthreadattributes, uint dwstacksize, intptr lpstartaddress, intptr lpparameter, uint dwcreationflags, out uint lpthreadid ); remotethreadparams params = new remotethreadparams(); parms.param1 = 10; parms.param2 = 200; // allocate native heap memory in process big enough store // parameter data intptr iptrtoparams = marshal.allochglobal(marshal.sizeof(remotethreadparams)); // copies data in structure native heap memory allocated marshal.structuretoptr(params, iptrtoparams, false); // use handle process intend create thread in. openprocess(...,...,...); // use alloc "committed" memory addressable other process intptr iptrremoteallocatedmemory = virtualallocex()... // copy process memory memory remoteprocess accessing writeprocessmemory(...,iptrremoteallocatedmemory,iptrtoparams,...,...); marshal.freehglobal(iptrtoparams); // safe free, have done copy createremotethread(...,...,...,...,iptrremoteallocatedmemory,...,...); // free memory allocated other process...but // careful of lifetime. // // free when thread no longer accessing allocated native // memory i.e. when it's finished. virtualfreeex(...,...,...,...);
in c/c++ code have:
#pragma pack(push,1) struct tagremotethreadparams { byte param1; int param2; } remotethreadparams, *premotethreadparams; #pragma pack(pop)
cast lpvoid
received thread function premotethreadparams
(i.e. *remotethreadparams
).
if have "strings" want 1 of parameters, have more work marshal them across. more see:
some other references:
Comments
Post a Comment