Link to home
Start Free TrialLog in
Avatar of fantasma110899
fantasma110899

asked on

Mapping functions in Memory

How I can map a procedure in memory like a Dll do? I want to obtain a handle to the procedure, just like GetProcAddress or something similar. The solution must work like a Dll do.
Avatar of nietod
nietod

Why can't you use a DLL?  

The difficulktys that in protected mode you can only write to data segments, not code segments, and you can only execute instructions that occur in code segments, not data segments.  Now there are ways around this, but I wonder if want to got that sort of trouble, because DLLs seem like exactly what you need.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need the VirtualProtect() call.  You can just allocate the memory using PAGE_EXECUTE_READWRITE, but that is a little more dangerious as the code could be altered by mistake during execution.
Avatar of fantasma110899

ASKER

I think your answer is good enough, just tell me how I copy the functions in the reserved memory. If when I test the program it works, you got the points.
I think your answer is good enough, just tell me how I copy the functions in the reserved memory. If when I test the program it works, you got the points.
What language?  in C/C++ use memcpy().

But why don't you just use a DLL?
Actually that memcpy() answer assumed you are copying them from memory.  if you are copying them from disk, you can read the procedure in from disk using ReadFile() or any similar procedure.
I don't know if what you say works because I need share memory between two or more processes. I was reading and VirtualAlloc maps the memory only in the process address space. Really I want to use SetWindowsHookEx without Dlls. Give some idea how I can do this. I was looking "Mapped Files" but I don't know if will work.
>> I need share memory between two or
>> more processes
With a few specific exceptions you can't.

I suspect you are pursuing a bad design here.  Why don't you try to explain what the ultimate goal is, because I think you're trying a bad approach to getting there.
Specifically, I want to implement a hook for messages. For that, I need use SetWindowsHookEx. One of this pararmeters is of type HOOKPROC which must be in a Dll, because maybe other processes have made the same thing. I want to know how I can do that without using a Dll. I suppose I must share this procedure with the other processes to simulate like a Dll were being used. In the other way around, how I can capture all the keys for all windows? Maybe you can answer this last question better that the other one. The keys's stuff is really what I want.  
>> I want to know how I can do that without using a Dll.
You don't!  there's no realistic way  to make it work.  The OS was written with a DLL in mind for this.  Dozens of different things will depend on this code being inside a DLL that the OS can use as an ordinary DLL.

Why not just use a DLL?  It will do exactly what you need.
I didn't use it just because I want to implement a class and I don't want to carry  a Dll wherever the class go. Do you know other way to capture the keystrokes for all windows in the system?
>> I want to implement a class
Use of  DLL will not have an impact on that.  The calss can be defined in the DLL, or used by the DLL.  Doesn't matter.

>> I don't want to carry  a Dll wherever
>> the class go
That's a reasonable concern, but you have to make tradoffs.  Alternatives migh take 100s or 1000s of hours of reaearch. programming and debugging.  They also may require different versions for NT/9x.  They also might fail on future versions of windows.  Is it worth that when windows provided a reliable mechansim for you to use that does exactly what you want, and whose only drawback is that it requires you to use a DLL?  That's a small price to pay.

>> Do you know other way to capture the
>> keystrokes for all windows in the system?
That won't take 100s or 1000s of hours of programming and debugging?  No.  
But you know other way or not? Give a clue.
One way would be to write your own keyboard device driver.  Since different keyboard may require slightly different drivers and since you can't reasonably handle all types of keyboards, you could write your driver to use the original driver to do most of the work.  This is extremely advanced programming.  I would not rocommend it.