?
Solved

Mapping functions in Memory

Posted on 1999-11-12
15
Medium Priority
?
277 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:fantasma110899
  • 9
  • 6
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2203277
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
 
LVL 22

Accepted Solution

by:
nietod earned 40 total points
ID: 2203300
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
 
LVL 22

Expert Comment

by:nietod
ID: 2203314
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 

Author Comment

by:fantasma110899
ID: 2203451
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
 

Author Comment

by:fantasma110899
ID: 2203500
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
 
LVL 22

Expert Comment

by:nietod
ID: 2203618
What language?  in C/C++ use memcpy().

But why don't you just use a DLL?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2203624
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
 

Author Comment

by:fantasma110899
ID: 2203727
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
 
LVL 22

Expert Comment

by:nietod
ID: 2203812
>> 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
 

Author Comment

by:fantasma110899
ID: 2203912
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
 
LVL 22

Expert Comment

by:nietod
ID: 2203962
>> 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
 

Author Comment

by:fantasma110899
ID: 2204254
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
 
LVL 22

Expert Comment

by:nietod
ID: 2204283
>> 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
 

Author Comment

by:fantasma110899
ID: 2204305
But you know other way or not? Give a clue.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2204341
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question