|
Hi, all
How do you “point” a function pointer to an array of byte code? I have converted a series of assembly instructions to byte codes (I don’t want to use inline assembly), and want my function pointer to point to that array of byte codes. The problem is that the VC++ compiler won’t cast from chars to the defined function pointer. The following code might explain better:
<br />
int (* FuncPtr)(int x, int y);
char ByteCode[] = {...};
<br />
FuncPtr = ByteCode;
The problem is that VC++ won’t covert from char array to the function definition. I tried adding “(void *)” when pointing but it still wouldn’t convert.
|
|
|
|
|
It seems I was logged out when I posted this...
Anyhow can anyone help me
Aidman » over and out
|
|
|
|
|
I have done this in the past and it has worked very well. As previously noted, it is very risky.
In my case, I used this technique to invoke bits of machine code generated at run time by compiling a script language on the fly.
BYTE CodeBuf[] = { ... };
typedef int (*CodeFunc)();
CodeFunc func = (CodeFunc)CodeBuf;
int returnvalue = (*func)( arg1, arg2 );
The Ten Commandments For C Programmers
|
|
|
|
|
Thanks, but why is it risky?
Aidman » over and out
|
|
|
|
|
Anonymous wrote:
The problem is that the VC++ compiler won’t cast from chars to the defined function pointer
I'm not surprised. This is an extremely dangerous thing to do, and should be avoided if at all possible. Is there any reason why you don't want to use inline assembly? You do realise that you can write a function as a separate assembly language file and link it with the rest of the program, don't you?
If you have to do this (and I strongly recommend that you don't), then you have to cast to the exact type.
typedef int (*FUNCPTR)(int, int);
FUNCPTR FuncPtr;
char ByteCode[] = {...};
FuncPtr = (FUNCPTR)ByteCode; although, I'm not sure if the compiler will let you do this. It may simply not want to cast a non-function pointer to a function-pointer, and I don't blame it
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"
|
|
|
|
|
Ok thanks
Ryan Binns wrote:
This is an extremely dangerous thing to do, and should be avoided if at all possible
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?
Aidman » over and out
|
|
|
|
|
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
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"
|
|
|
|
|
typedef int (*tMyFuncPtr)(int x, int y);
tMyFuncPtr Funcptr;
FuncPtr = ByteCode
The warnings:
a) Are you aware of all the performance issues of mixing code and data on todays processors?
b) specify the calling convention in the typedef!
c) not all processor architectures allow execution on tsack or heap
d) don't do that unless you can prove it's worth the trouble
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen
|
|
|
|
|
Ok thanks
peterchen wrote:
a) Are you aware of all the performance issues of mixing code and data on todays processors?
peterchen wrote:
c) not all processor architectures allow execution on tsack or heap
Where can I find the info on performance issues (for mixing code and data) and processor support (for execution on tsack or heap)?
Aidman » over and out
|
|
|
|
|
Aidman wrote:
Where can I find the info on performance issues (for mixing code and data)
unfortunately, I don't know anymore. I just remember an article that discussed that techniques like "compiled bitmaps" (popular with ModeX) aren't worth the trouble anymore.
Most CPU architectures (including Intel) have separate Code and Data Caches. If I recall correctly, the main performance problem ins when there's an overlap, the CPU flushes both Code and Data cache (since they are not meant to overlap). I might be wrong here, though.
Aidman wrote:
and processor support (for execution on tsack or heap)?
Windows allows separate read/write/execute flags to be set for allocated pages (i.e. it could deny the stack exectution rights - zap go all buffer overflow intrusions). It doesn't make use of this on the x86 / Pentium platform; you would have to check the docs of the particular CPU/OS/Compiler combination but it's possible and makes sense.
Peter
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen
|
|
|
|
|
I have a problem with my MDI. I have three views. Two are CHtmlViews and when one of the CHtmlView (View2) is clicked; it is suppose to call the other CHtmlView (View1) to go to that website. I have in the CApp class the global variable theApp and a public class GetHtmlView(). GetHtmlView goes through the active frames until View1 is found and return a pointer to it. The problem that I get is
'CApp::GetHtmlView' : cannot access protected member declared in class 'CApp'
: see declaration of 'CApp::GetHtmlView'
: see declaration of 'CApp'
This is CApp::GetHtmlView declaration
CActWin* GetHtmlView();
This is CApp declaration
public CApp : public CWinApp
I have no protected members inside of CApp.
This is where I get the error inside of View2
CActWin* pActWin1 = theApp.GetHtmlView();
pActWin1->Navigate2(lpszURL,NULL,NULL);
The theApp is global like previously stated
extern CApp theApp;
Off topic, but does anyone know the best solution for when View2 is clicked and I don't want View2 to navigate to that website that was clicked on. I used in the OnBeforeNavigate2(); Navigation("current html", NULL, NULL). I just think there should be a better way to prevent View2 from going to another site.
Thanks for any help
|
|
|
|
|
What value I would get if I send for example WM_USER + 100 to window, that have no handler for it (should it process it to DefWindowProc)?
|
|
|
|
|
If you know that the window definitely does not have a handler for the message, then you will get a value of 0. If you don't know for sure, don't rely on it - windows often post messages to themselves for doing various things, and they can have any value and may not be documented.
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"
|
|
|
|
|
There does not seem to be an ActiveX Control for MS Excel from office 97. Is this true? If so is there anyway of writing an app that uses an ActiveX Control for Excel from a later version of office, but that will still be compatible with office 97?
Sincerely,
Mardigin
|
|
|
|
|
Hi,
My program needs to stop a do/while loop when the user decides it. I first use a progress dialog bar which works great but it is not convinient.
I would like to stop a do/while loop using a button of my toolbar.
How can I tell the program to spend sometime checking on the toolbars and menu while it is running within the loop?
Thanks,
RNO
|
|
|
|
|
I use something like this to detect if the user presses a button inside a loop:
MSG msg;
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
jhaga
---------------------------------
Do not hire a man who does your work for money, but him who does it for love of it.
Go confidently in the direction of your dreams! Live the life you've imagined. As you simplify your life, the laws of the universe will be simpler.
Do not be too moral. You may cheat yourself out of much life. Aim above morality. Be not simply good; be good for something.
-Henry David Thoreau-
|
|
|
|
|
I think the normal thing to be done in this case is running your do/while loop in a separate thread so you can handle all the messages, including toolbar notifications, from the main application thread normally.
Your while loop condition should check on a global variable that is to be changed by the toolbar message to fail the condition.
|
|
|
|
|
Hi there,
I'm writing a serial port class that has its own thread to read data out of the port in the background. That bit all seems to work fine, but I want to get the data from the buffer that I'm storing it in and update a read only text box in a dialogue in my app. Where is the best place to do this should I do something like that in the OnIdle function and just use a pointer to the dialogue to update the variable or is there a better way.
Cheers
Andy
|
|
|
|
|
One solution is messages. Send a message from the worker thread to the main thread, which updates the controls in the dialog box.
Kuphryn
|
|
|
|
|
I want to output this characters which range between 0x0000 and 0xFFFF, but some of them can not output, I don’t know the reason, and how I can know which character can output?
|
|
|
|
|
All characters can be output. It's up to the particular device (e.g., display, printer, file) as to whether it can be rendered or not.
|
|
|
|
|
.. and also remember that if you are 'outputing' your characters via an edit control, ensure that the font being used can render unicode characters. Not all can display the all the character sets.
"..Even my comments have bugs!"
Inspired by Toni78
|
|
|
|
|
Thought CPians might be interested in the future of some of the boost www.boost.org[^] libraries, which are being considered in a technical report for the next Committee meeting.
http://std.dkuug.dk/jtc1/sc22/wg21/docs/library_technical_report.html[^]
Below is a message with a bit more information.
From: Beman Dawes (bdawes@acm.org)
Subject: Re: Boost and the next C++ Standards
View this article only
Newsgroups: comp.std.c++
Date: 2003-07-03 16:48:01 PST
scott@coyotegulch.com (Scott Robert Ladd) wrote in message news:<pan.2003.06.30.00.48.26.743059@coyotegulch.com>...
> What is the likelihood of the Boost libraries (in whole or in part) being
> integrated into the next C++ Standard?
Ah! The committee needs to do some communicating. So... See
http://std.dkuug.dk/jtc1/sc22/wg21/docs/library_technical_report.html
> I keep getting asked the question, and wondered if any consensus had
> emerged among C++ standardizers.
For this Technical Report, the cutoff date has already passed. So
except for two or three proposals still in the pipeline, the content
of this first Library TR are firming up.
Of the 12 proposals accepted so far, 10 grew out of the Boost
libraries.
(One Boost library, static assert, is also likely to become part of
C++, but as a core language feature rather than part of the library.)
--Beman Dawes
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts you aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
Hi
I have another Assertion problem.
You see I have Dialog app. This has other custom dialogs. But in this situation, 2 dialogs. The main dialog is used to call a 2nd dialog when necessary. The app receives information via the net (TCP stream style).
Now info comes in, and the MainDlg class has a thread that listens for the information. Info comes in, it is sent to the 2ndDlg.
Now in the 2nd dlg, I have declared a OnInitDialog function which sets the info from the Main Dlg and places it in the List Control of the 2nd Dlg. But I get the Assertion failure.
And this is the function that causes that:
// CString support (windows specific)
_AFX_INLINE int CString::Compare(LPCTSTR lpsz) const
{ ASSERT(AfxIsValidString(lpsz)); return _tcscmp(m_pchData, lpsz); } // MBCS/Unicode aware
I dont know how this is because I said:
2ndDlg.receivedInfo = MainDlg.Info ;
Then,
2ndDlg.DoModal() //this should initialise the window of Dlg.
Help is appreciated. Thanks.
Live in peace
|
|
|
|
|
Problem solved. Thanks. Followed your advice and further eradicated the check that says
if(CString == NULL) <-this gave me the new Assertion ERROR.
Perfect. McLaren 4-5, will be podium tomorrow, hopefully a 1-2. Watch and see!! I feel it.
Let the network do the work for you.
|
|
|
|