Going bald here!

Started by
4 comments, last by LessBread 18 years, 6 months ago
The community here is super! Thus far I've never been made to feel an idiot with my trivial (to you all) problems in anyones responses! Thanks! That being said... Scenario I have a program that contains a function I need to call. I only know the address of this function, but I also know the parameters it needs as well as the datatypes. Now I inject a DLL into this other program that hooks some winsock functions. Which works fine. What i'm trying to do is call the other function from within the injected DLL via an exported procedure. While hammering on this issue, the app I use to inject/uninject my DLL is where I'm calling the exported procedure contained in the injected DLL. Providing 2 parameters. Whats happening is I get: Exception: Memory Access Violation (Program tried to read/wite and invalid memory address) I'm assuming this means my app doesn't have access to the function address in the target program. I thought however since I'm injected into the target program that I (the DLL) was 'part' of the target programs address space. How can I do this? Been working on this for 2 days and am about brain dead... I just need to be able to invoke this function in the target application from my own program. Like all my issues, I'm assuming its something stupidly simple I'm overlooking. Thanks in advance!
Advertisement
So, your injecting app contains the function to be called but you're trying to call it from the DLL within the target app right? If I've got that much right, then the reason is that the two processes have different address spaces. You've injected the DLL (by the way I don't know how to do that kind of thing...) but not the rest of the program. If you need to inform the injecting app of something you'll have to use IPC (I'm assuming it's not a utility function because then you could just move it into the DLL).
The title of this thread is lacking. You'd get more help with a title more specific to your question.

What mechanism are you using to inject the dll? If you hooked a window message proc or loop, take note that you can send messages to windows in other processes and use the WM_COPYDATA message to get info back. You won't ever be able to invoke a function in another process from outside of that process. You can, along with the injected dll, insert a message handler into the message proc of the hooked window and with that in place send that window messages and so on.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Ok, I use MadCodeHook to do the injection.

Program1 is a program that I inject into. This program has a function I need to call but have to do so by its pointer since its compiled and I dont have the source.

InjectDll is a DLL that gets injected into Program1. Its in this DLL that I export a procedure that when invoked calls the function in program1.

If you were asking if i'm doing a callback, no. Not for the function in program1.

I'm not sure how I can put in a message handler in the injected DLL. If I place a dialog in the DLL, execution of program1 halts until an event is triggerd in that form/dialog.

I can use IPC but still comes back to allowing the handler and program1 to run simultaneously.

Quote:Original post by PBNut
Ok, I use MadCodeHook to do the injection.


I don't know what that means. MadCodeHook? That sounds like something someone else came up with. This might help identify the approach you're using: Three Ways to Inject Your Code into Another Process.

Quote:Original post by PBNut
Program1 is a program that I inject into. This program has a function I need to call but have to do so by its pointer since its compiled and I dont have the source.

InjectDll is a DLL that gets injected into Program1. Its in this DLL that I export a procedure that when invoked calls the function in program1.


Ok. Let's call the program that instigates the injection Program 2. When Program 2 invokes the function exported by injectdll, it does so in it's own address space, presuming of course that injectdll is also loaded into Program 2.

Quote:Original post by PBNut
If you were asking if i'm doing a callback, no. Not for the function in program1.


Ok.

Quote:Original post by PBNut
I'm not sure how I can put in a message handler in the injected DLL. If I place a dialog in the DLL, execution of program1 halts until an event is triggerd in that form/dialog.


The message handler can be inserted as part of the process of hooking a window in the other process. There are a variety of hooks that can be applied, a couple of them intercept messages.

Quote:Original post by PBNut
I can use IPC but still comes back to allowing the handler and program1 to run simultaneously.


Ok.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
To add a little more to the above, code injection is not the easiest task. My advice is to start with something simple, some sample code that you know works and works well with the compiler that you regularly use. The above link provides code for MSVC. You'll probably have to google to find code that works with other compilers. Work with whichever sample you find until you get it to work for you and so that you understand how it works as well. Understanding how the mechanism works is very important because the next step after that is to adapt it to do what you want it to.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement