Click here to Skip to main content
15,921,276 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: design-time license??? Pin
Mardigin5-Jul-03 16:47
Mardigin5-Jul-03 16:47 
QuestionPointing a function pointer to a variable? Pin
Anonymous5-Jul-03 14:53
Anonymous5-Jul-03 14:53 
AnswerSorry I was logged out Pin
Aidman5-Jul-03 14:55
Aidman5-Jul-03 14:55 
GeneralRe: Sorry I was logged out Pin
Rick York5-Jul-03 21:54
mveRick York5-Jul-03 21:54 
GeneralRe: Sorry I was logged out Pin
Aidman6-Jul-03 7:12
Aidman6-Jul-03 7:12 
AnswerRe: Pointing a function pointer to a variable? Pin
Ryan Binns5-Jul-03 20:29
Ryan Binns5-Jul-03 20:29 
GeneralRe: Pointing a function pointer to a variable? Pin
Aidman6-Jul-03 7:05
Aidman6-Jul-03 7:05 
GeneralRe: Pointing a function pointer to a variable? Pin
Ryan Binns6-Jul-03 17:50
Ryan Binns6-Jul-03 17:50 
Aidman wrote:
Why is it dangerous and should be avoided? What can go wrong? Becouse the only diffrens I can see is that you have a function in your data memory area instead of the code area.
What else am I missing?


I think a couple of things were pointed out below. But there are a few problems:

1. The Visual C++ compiler uses certain registers that it expects to be preserved between function calls. If they're not preserved, then errors can be introduced into the program.

2. By default, the data memory area can be read/written, but can not be executed from. You have to change the access permissions of the data area so that it can be executed from. This is done by Windows to prevent malicious programs from compiling code in memory (to bypass virus scanners) and then executing it.

3. If you use the wrong calling convention, the stack can be corrupted and when the function returns, the program may start executing at the wrong place, causing access violations, and possibly crashing the computer.

4. If you accidentally overwrite this array with other data, then the code will obviously become corrupted and will cause problems if you then execute it as a function. The code memory space is protected from this by Windows, again using access permissions (which can be changed, btw).

5. What if somebody hacks your program after it's loaded and changes the data in this array? This is a big security hole. The code memory is protected from writing by Windows (as I said above), but the data memory is not.

Basically, if you know exactly what you're doing, then it's no more dangerous than any other code, as long as you seriously think about what can go wrong. If you're not sure exactly how things are handled, then I'd recommend doing it another way. If it's static code, then either use the inline assembler or a separate assembly language file, and let the compiler sort everything out. If the code is dynamically compiled by your program, then consider compiling it to a DLL and loading it with LoadLibrary() (a bit more difficult, but a lot safer). In general, executing code from data memory is not a good idea because of the security concerns and possibility of data corruption.

Hope this explains things better Smile | :)

Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

AnswerRe: Pointing a function pointer to a variable? Pin
peterchen5-Jul-03 21:51
peterchen5-Jul-03 21:51 
GeneralRe: Pointing a function pointer to a variable? Pin
Aidman6-Jul-03 7:08
Aidman6-Jul-03 7:08 
GeneralRe: Pointing a function pointer to a variable? Pin
peterchen6-Jul-03 22:17
peterchen6-Jul-03 22:17 
GeneralProtected member access, that's not protected Pin
pdunn5-Jul-03 14:39
pdunn5-Jul-03 14:39 
GeneralUnknown message Pin
Xakep5-Jul-03 12:54
Xakep5-Jul-03 12:54 
GeneralRe: Unknown message Pin
Ryan Binns5-Jul-03 20:36
Ryan Binns5-Jul-03 20:36 
QuestionMS Office 97 ActiveX Control? Pin
Mardigin5-Jul-03 12:15
Mardigin5-Jul-03 12:15 
QuestionHow do I stop a do/while loop using a toolbar Pin
Rno355-Jul-03 10:58
Rno355-Jul-03 10:58 
AnswerRe: How do I stop a do/while loop using a toolbar Pin
jhaga5-Jul-03 11:37
professionaljhaga5-Jul-03 11:37 
AnswerRe: How do I stop a do/while loop using a toolbar Pin
MAAK6-Jul-03 13:31
MAAK6-Jul-03 13:31 
GeneralUpdating dialogue Pin
andyg.1015-Jul-03 4:01
andyg.1015-Jul-03 4:01 
GeneralRe: Updating dialogue Pin
valikac5-Jul-03 7:09
valikac5-Jul-03 7:09 
Questionhow I can know which character can output? Pin
Alice805-Jul-03 3:58
Alice805-Jul-03 3:58 
AnswerRe: how I can know which character can output? Pin
David Crow5-Jul-03 12:26
David Crow5-Jul-03 12:26 
GeneralRe: how I can know which character can output? Pin
RChin6-Jul-03 23:25
RChin6-Jul-03 23:25 
GeneralTR1 (C++ Standard) Pin
Andrew Walker5-Jul-03 3:48
Andrew Walker5-Jul-03 3:48 
GeneralDEBUG ASSERTION FAILED: File: afx.inl Line: 157 Pin
spokes5-Jul-03 3:33
spokes5-Jul-03 3:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.