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.
fantasma110899Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
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.
0
nietodCommented:
To answer the question you asked, you can use VirtualAlloc() to allocate memory for the procedure.  You will need to allocate it so that it can be written to (PAGE_READWRITE) then after you copy the procedure to the memory, you use VirtualProtect() to change the memory to PAGE_EXECUTE access so the procedure can be executed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

fantasma110899Author Commented:
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.
0
fantasma110899Author Commented:
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.
0
nietodCommented:
What language?  in C/C++ use memcpy().

But why don't you just use a DLL?
0
nietodCommented:
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.
0
fantasma110899Author Commented:
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.
0
nietodCommented:
>> 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.
0
fantasma110899Author Commented:
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.  
0
nietodCommented:
>> 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.
0
fantasma110899Author Commented:
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?
0
nietodCommented:
>> 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.  
0
fantasma110899Author Commented:
But you know other way or not? Give a clue.
0
nietodCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.