|
A feature of MFC is that any toolbar buttons and menu items that don't yet have a handler get disabled. You can turn this off by setting CFrameWnd::m_bAutoMenuEnable to FALSE in your CMainFrame constructor.
--Mike--
When 900 years old you reach, look as good you will not. Hmm.
1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Hi all,
Is it possible to declare a function pointer outside function arguments? I mean like declaring a pointer as a separate variable which isn’t an argument and can change the function it is pointing at, assuming that whatever function it points at always has the same arguments. Like this code for example. Please note that I know this following code doesn’t compile, I just want to explain my question in code, it’s always a lot easier.
<br />
void realfunc(void){
return;<br />
}<br />
<br />
int main(){<br />
void (*funcptr)(void);
*funcptr=realfunc;
funcptr();
return 0;<br />
}<br />
So is there something similar to this code? In other words is it possible to do something like this?
Aidman » over and out
|
|
|
|
|
Short answer: Yes
"You can stand all night at a redlight anywhere in town, hailing Marys left and right but none of them slow down. I've seen the best of men go past. I don't wanna be the last..."
|
|
|
|
|
Long answer: Yes, and you can use a typedef to make it simpler.
Try this sample code:
[edit]oops, forgot the darn formatting so the stdio.h got removed.[/edit]
#include <stdio.h>
typedef void (*PFN_YOURFUNCTION)(void);
void TestFunction1()
{
printf( "TestFunction1\n" );
}
void TestFunction2()
{
printf( "TestFunction2\n" );
}
int main()
{
PFN_YOURFUNCTION a_pfnFunction = NULL;
a_pfnFunction = TestFunction1;
a_pfnFunction();
a_pfnFunction = TestFunction2;
a_pfnFunction();
return 0;
}
The output will be:
TestFunction1
TestFunction2
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Great Thanks Chris Richardson!
I am sorry, I feel a little dumb not realizing that C++ must hade such a method.
But here’s another “function pointer” related question, which may be more advanced if that’s ok? Let’s say you have a function (in your own application) whose arguments are stored customable in variable arrays and the function name you don’t know. The only thing you have as a pointer of the function is a simple memory pointer/adress (no real function pointer) that points to a memory location where the function is stored during run-time. The following code example would demonstrate what I am trying to say.
<br />
<br />
struct FUNCSTRUCT {
MEMPTR *FunctPtr;
int VarCount;
MEMPTR *VarPtr[255];
int VarSize[255];
}<br />
<br />
MEMPTR CallFunc(FUNCSTRUCT *FunctData);
<br />
int TheRealFunc(int Argument){
return Argument;<br />
}<br />
<br />
int main(){<br />
FUNCSTRUCT FuncData;
int Value = 1;
<br />
FuncData.FunctPtr = TheRealFunc;
FuncData.VarCount = 1;
FuncData.VarPtr[0] = Value;
FuncData.VarSize[0] = 2;
<br />
return CallFunc(&FuncData);
}<br />
Is it possible to create such a function as “CallFunc” that could call a function, only knowing the custom arguments and a memory pointer/address? If so then how and how would the “MEMPTR” variable type look like? I presume that this question is a lot harder and I guess it includes some low-level knowledge. But is this possible and if it is then please explain how and demonstrate it in code please or could you point me to a tutorial on this.
|
|
|
|
|
No.
main() must return an int. CallFunc return a MEMPTR object.
Kuphryn
|
|
|
|
|
Yes I know this code doesn't compile it is only to demonstrate my words in code. And fourther more the MEMPTR return is pointing to an int variable and that was part of my question, How would MEMPTR look like, meaning how would it be constructed and operate, to get the memory location.
Please help me anyone.
Aidman » over and out
|
|
|
|
|
I guess you could handcraft then stack using assembler. I'm not that up to date
on calling conventions so you have to get the dirty details from someone else.
"You can stand all night at a redlight anywhere in town, hailing Marys left and right but none of them slow down. I've seen the best of men go past. I don't wanna be the last..."
|
|
|
|
|
Any ideas of where I could find some info on this type function pointer calls?
|
|
|
|
|
|
It looks you're trying to create a scripting engine. If that's the case then most scripting engines accept arguments as strings and then convert those arguments into the proper types.
int TheRealFunc(const char* argument)
{
int i = atoi(argument);
}
Or, for each "real" function there's a wrapper function for your scripting engine that parses the parameters and then does the real call
int TheRealFunc(int a1, int a2, float a3)
{
}
int TheRealFuncWrapper(const char* argument1, const char* argument2, const char* argument3)
{
int a1 = atoi(argument1);
int a2 = atoi(argument2);
float a3 = atof(argument3);
return TheRealFunc(a1, a2, a3);
}
Todd Smith
|
|
|
|
|
You guessed right, I am trying to create a simple script that can call function in the application that runs the script. But my "script engine", if you real can call it that, is declared as a class where you should be able to call a function named "AddFunction" and you simple add the pointer of the function you want to add into the script class so the script can call it. And also the script has to be able to call win32 API dll calls. Please help how can I make this work, how do I make the call and get the a pointer of the function I wanto add?
Aidman » over and out
|
|
|
|
|
If you are looking to create a scripting engine, take a look at the IDispatch interface in the MSDN. Specifically, the Invoke member of IDispatch. What it defines is sort of what you are looking for I think.
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Please understand that I don't wanto use any already designed scripts, escially an script object. I wanto create my own language and own engine
Aidman » over and out
|
|
|
|
|
Hey everyone!
Okay, I have the following code:
typedef int (*funcP) (int v1, int v2);
struct pStruc
{
funcP callback;
};
struct pStruc *callbacks;
int someFunc (int v1, int v2)
{
return v1 + v2;
}
int anotherFunc (int v1, int v2)
{
return v1 - v2;
}
struct pStruc *assignFuncs ()
{
return (struct pStruc []) {&someFunc, &anotherFunc};
}
void main ()
{
callbacks = assignFuncs ();
cout << callbacks->callback (10, 20);
}
Output will be 30.
You should get the idea: I have an array of function pointers, and I need to call the stored functions. Works so far. Now, what will I have to do if assignFuncs as well as the two refered functions are members of some class? I'd like to have it working in a very flexible way, without any hardcoded class name! Any idea?
tilli
|
|
|
|
|
I'd like to give you an exact answer but I'm afraid my experience with member function pointers is extremely limited (thank God). If you haven't already, you may want to search the C/C++ articles here for ways of accomplishing what you need.
What I'd like to know is why? Why go through this syntactic nightmare when OO techniques such as virtual functions may provide a more elegant and maintainable solution?
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
C++ is strongly typed, so you can't use member-function pointers with out a class name. Nor can you mix function pointers with member-function pointers. However, you can accomplish what you want (and a lot more) by giving up on function pointers and turning to higher level "function objects" with libraries like Loki or Boost.Function (and Boost.Bind is quite handy when using Boost.Function as well!). http://www.boost.org[^]
William E. Kempf
|
|
|
|
|
One solution is static declaration. Given assignFuncs points to a class member function, then that class member function must be static if you want to call it anytime and anywhere. Otherwise, you must call it via object.
Kuphryn
|
|
|
|
|
Great! Making them static is working. To answer the question about "why the hassle": I'm writing on an Win32 API OOP Wrapper, and I wanted the user to be able to overwrite a function (assignFunc in this case) which is called by the class during the initialization process. What it does then is returning a list of Window Messages with their corresponding callback functions. Once a message needs to be processed then, the window message loop checks if the message has a callback defined and launches it. The reason I wanna be able to use class member functions for this purpose is so the callback functions can use member variables.
tilli
|
|
|
|
|
Well...making them static did work for what I wanted. But I'm no more able to access non-static functions from these functions then, so the intended advantage over non-class-functions will be exactly void. So, anyone has an idea how to rewrite the original code? I'd like to keep something at least somewhat similiar to the current approach, because I found it to be quite convenient and flexible for the user!
Thanks
tilli
|
|
|
|
|
I inserted a tree control in a dialog box, and enabled the Check Boxes property. Then I load up the control with items, allow the user (me right now) to select some of them by placing checks next to the items of interest.
All is well and good. The only problem I have is... I can't figure out how to determine which items have been checked! Any help would be greately appreciated.
Thanks,
Gary H
|
|
|
|
|
I found it...
I was looking at the item state, and didn't realize there was a GetCheck member function of CTreeCtrl. I knew it was going to be simple.
Later.
|
|
|
|
|
I have a dialog based application in which I have two sources of events
1) On Timer
2) On User input(such as click of button)
Now on both events I will be calling a function, which acts on a single resource.
Calling the function while it is already executing is dangerous & can corrupt all my data.
Now I want to know do I need to put any synchronization code (SingleLock etc.) in my function.
** The important point here is that I am not creating any threads explicitly.
So my assumption is that my app is single threaded. In such case my function can never be called (from anywhere else) while it is running.
Am I correct? Am I missing anything here?
Please help me.
|
|
|
|
|
Since you only have one thread, you need not worry.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
you might not need SingleLock, etc.. but if there's a chance you could end up going through the same function twice at the same time (especially if you're doing message pumping) you might need to use a flag at the top of any functions where this could be dangerous.
bool g_alreadyInFuncA = false;
void funcA()
{
if (g_alreadyInFuncA)
{
error? return early? crash?
}
g_alreadyInFuncA = true;
blah
blah
MessagePump...
blah
g_alreadyInFuncA = false;
}
void OnMyMessage()
{
funcA()
}
-c
I'm not the droid you're looking for.
|
|
|
|