c++ - How to call function inside injected dll -


i'm trying keyboard messages process using injected dll,but don't know have call function in own program. here injected dll functions :

//this dll main function   bool apientry dllmain(handle hmodule,dword  ul_reason_for_call,lpvoid   lpreserved)       {         /* open file */         file *file;         fopen_s(&file, "d:\\dll\\temp.txt", "a+");          switch (ul_reason_for_call) {         case dll_process_attach:             hinst = (hinstance)hmodule; // should function calling here????             installhook();              break;         case dll_process_detach:             fprintf(file, "dll detach function called.\n");             break;         case dll_thread_attach:             fprintf(file, "dll thread attach function called.\n");             break;         case dll_thread_detach:             fprintf(file, "dll thread detach function called.\n");             break;         }         hinst = (hinstance)hmodule;         /* close file */         fclose(file);         return true;     } 

here install hook function installing keyboardproc process

   bool __declspec(dllexport)__stdcall installhook()         {             hwnd targetwnd;             handle hprocess;             unsigned long processid = 0;             hkb = setwindowshookex(wh_keyboard, (hookproc)keyboardproc, hinst, getcurrentthreadid());             return true;         } 

and keyboardproc function body

lresult __declspec(dllexport)__stdcall  callback keyboardproc(int ncode, wparam wparam, lparam lparam) {     char ch;     messageboxa(nullptr, "key touched\n", "dll_process_attach", mb_ok | mb_iconwarning);         {         if (((dword)lparam & 0x40000000) && (hc_action == ncode))         {             if ((wparam == vk_space) || (wparam == vk_return) || (wparam >= 0x2f) && (wparam <= 0x100))             {                 file *file;                 fopen_s(&file, "d:\\dll\\temp.txt", "a+");                 fprintf(file, ncode + ".\n");             }         }     } while (0);     return callnexthookex(hkb, ncode, wparam, lparam); } 

and here main program injected dll destination process

int procid = 9448;     handle process = openprocess(process_all_access, false, procid);     if (process == null) {         printf("error: specified process couldn't found.\n");     }      /*     * address of loadlibrary function.     */     lpvoid addr = (lpvoid)getprocaddress(getmodulehandle(l"kernel32.dll"), "loadlibrarya");     if (addr == null) {         printf("error: loadlibrarya function not found inside kernel32.dll library.\n");     }      /*     * allocate new memory region inside process's address space.     */     lpvoid arg = (lpvoid)virtualallocex(process, null, strlen(buffer), mem_reserve | mem_commit, page_readwrite);     if (arg == null) {         printf("error: memory not allocated inside chosen process.\n");     }      /*     * write argument loadlibrarya process's newly allocated memory region.     */     int n = writeprocessmemory(process, arg, buffer, strlen(buffer), null);     if (n == 0) {         printf("error: there no bytes written process's address space.\n");     }      cout << procid << "\nhandle:" << process << "\naddress:" << addr << "\nvirtualarg:" << arg << "\nwm:"<<n<<"\n";       /*     * inject our dll process's address space.     */     handle threadid = createremotethread(process, null, 0, (lpthread_start_routine)addr, arg, null, null);     if (threadid == null) {         printf("error: remote thread not created.\n");     }     else {         printf("success: remote thread created.\n");     }      /*     * close handle process, becuase we've injected dll.     */     closehandle(process); 

what wrong in code , must change desired result!

yes, can called dll_process_attach. according msdn

hmod [in] type: hinstance handle dll containing hook procedure pointed lpfn parameter. the hmod parameter must set null if dwthreadid parameter specifies thread created current process , if hook procedure within code associated current process.

so change hmod null

hkb = setwindowshookex(wh_keyboard, (hookproc)keyboardproc, null, getcurrentthreadid());


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -