|
You problem is that you are trying to store a pointer to a non-static function. You just can't.
There are ways around it though.
Imagine a class with a work thread function in it.
class CMyClass
{
virtual DWORD DoScan ();
static DWORD WINAPI DoScanStatic (LPVOID lpParameter);
... etc.
};
main ()
{
...
DWORD dwID;
HANDLE hThread;
hThread = ::CreateThread (NULL, 0, DoScan, this, CREATE_SUSPENDED, &dwID);
hThread = ::CreateThread (NULL, 0, DoScanStatic, this, CREATE_SUSPENDED, &dwID);
...
}
You can make your life simpler by renaming DoScanStatic to DoScan and using overloading.
eg.
virtual DWORD DoScan ();
static DWORD WINAPI DoScan (LPVOID lpParameter)
{
CMyClass *my = (CMyClass *) lpParameter;
ASSERT(my);
if (my == NULL)
return 0;
return my->DoScan ();
}
This way you can use a static callback, and make in inheritable via indirection.
Did that help?
Iain.
|
|
|
|
|
|
Michael Dunn wrote:
Typedefs are not restricted to static functions, you're thinking of something else entirely.
I know. But callbacks are. I used to use typedefs quite alot so I
didn't have to type struct every few minutes in the old C days.
I use it to have templated containers of templated thingys so my
brain doesn't get confused by the >> / > >s.
I probably should have changed the post title thought, to make it
more explicit that I was commenting on his callback setup.
Iain.
|
|
|
|
|
I read your faq clickety in another post, and I retract my comments on callbacks a little, but you
still need to know the this the function goes with. But being able to choose between non-static
member functions (albeit with identical signatures) at runtime is quite cunning.
Iain.
|
|
|
|
|
Iain wrote: "You problem is that you are trying to store a pointer to a non-static function. You just can't."
Sorry, but that's not correct. Of course it's possible to store (and use) pointers to non-static member functions.
For a look at syntax, see http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]
|
|
|
|
|
When I got the notification about your posting, I was initially
embarassed. I usually try not to generalise so much. But then
I read the faq you pointed me to, and it outlined the method
I used in the code below my bald statement.
I perhaps could have explained it better, but if you look at the
code it has a static function recieving a pointer turning the
pointer to an object one, and calling a non-static member function.
Members of the jury, I rest my case.
Iain.
|
|
|
|
|
I don't intend to appear stubborn, but when discussing the code you're referring to, the FAQ very clearly says it's "a patch". It's meant to be used in situations where you need to supply a plain-C callback function to the API, like in EnumChildWindows() or others.
In fact, the FAQ istelf poses the question as follows: "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?" It's answer is a very direct one: "Don't"
Here's the precise link: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2[^]
The issue with plain-C callback functions and the inability to (directly) use C++ member functions is well-described at MSDN in "Calling All Members: Member Functions as Callbacks" at http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvc/html/msdn_callb.asp[^] I agree that the technique is a good one for the situation described in the FAQ.
But it's not correct to say that you can't store (and use) a pointer to member functions.
It's also not correct to say that member functions can't be used in ways very similar to regular callbacks. Here's one example from MSDN, entitled "Pointers to Member Functions and Templates" at http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvc/html/msdn_callb.asp[^]
So, maybe the jury's still out ....
Regards,
Mike
|
|
|
|
|
After Mike Dunns FAQ posting, I posted a mini-retractment. In a general windows callback (ie for
CreateThread etc) you have to use a static function. You can cast its void * parameter to the object
in question and call a "normal" member function, getting around the issue.
The mdsn_callb.asp page you pointed to also spells this out. It does include a lookup method which
may well work in circumstances. But it is still using a static as the callback, which then derives
an opbject pointer before calling the member function.
Mike Dunn did point out how to create a pointer to class member function pointer which was a new one
to me. This is a new one on me, and I can see it being very useful. But you now need to store two
pointers. Which object, and which member of that object! You can get around it, but not without non-
trivial bookkeeping. So I would consider it too much hard work, and too prone to mistakes for use as
a windows callback function.
So, to misquote myself:
"... You can't. But you can cheat, and get around the problem!".
In my experience, there's never a hard and fast CAN'T. But there is a "way too much like hard work when
much easier, simpler and safer ways exist".
In next weeks episode, "How many angels can line-dance on the side of a pin".
Iain.
|
|
|
|
|
|
Thanks Michael, this solved my issue.
i mixed up the usage on "pointer-to-function" and "pointer-to-member"
|
|
|
|
|
hi,
where to find unicows.lib for download?
thanks
|
|
|
|
|
Grab a recent platform SDK (contains DLL and lib file), or google for it.
Your sincerity about keeping the soapbox organized and civilized is so obvious. I solute your effort.
-- Anonymous, 10/18/03
|
|
|
|
|
hi,
can somebody,please tell me how to change for example,scales or width of 2D bars for tihis activeX control within the code?
|
|
|
|
|
Is there a limit on the size a key value or registry tree can have? If so, what is the maximum? To be more precise, I would like to know which key type to use (REG_DWORD, REG_SZ, etc.) to store large binary values, say more than 20 MB in size.
|
|
|
|
|
No, do not use the registry to store that much data. Use a data file in the user's Application Data dir. In 9x a single value has a limit - either 32K or 64K, I forget which. In NT the size of the entire registry has a limit, which defaults to something less than 20 MB.
--Mike--
Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber
#include "witty-quote.h"
|
|
|
|
|
Michael Dunn wrote:
In NT the size of the entire registry has a limit, which defaults to something less than 20 MB.
My Windows 2000 box is currently sitting at 40MB.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
as far as i know there is no "real" limit for the registry size on NT.. read the ReactOS source, to get information about that!
Don't try it, just do it!
|
|
|
|
|
dafunkt wrote:
to store large binary values, say more than 20 MB in size
Why not just store them as binaries in a folder location?? And then store the string of that folder in the registry???
:walks away shocked at the concept of storing multiple 20 meg binaries in the registry:
The kindest thing you can do for a stupid person, and for the gene pool, is to let him expire of his own dumb choices.
[Roger Wright on stupid people]
We're like private member functions
[John Theal on R&D]
We're figuring out the parent thing as we go though. Kinda like setting up Linux for the first time ya' know...
[Nitron]
|
|
|
|
|
Thank you for your help first. I'd like to get the application icon (small icon on title bar) of any application. I used the API function SendMessage with WM_GETICON message to the window of that application, some applications return the handle of its application icon (eg: Windows Explorer, ...), but some do not (eg: MS Outlook, NotePad,...). Please tell me other ways to do that. Thanks a lot.
chaunguyen
|
|
|
|
|
You could go about it laterally. Assuming WM_GETICON fails...
Get the file of the executable (GetModuleFileNameEx), and pick the first icon resource in it.
Iain.
|
|
|
|
|
Thanks Iain for your early help. Your solution works, but in some case it return an icon which is not the current application icon. Anyway, that's fine, thanks.
Chau Nguyen
|
|
|
|
|
Try using SHGetFileInfo .
Software Zen: delete this;
|
|
|
|
|
Thanks a lot, Gary. Your suggestion is great. It works.
chaunguyen
|
|
|
|
|
I'm building a mfc custom control, and it includes some custom messages that are sent out to a designated CWnd when some events are triggered. for now, i've defined them as something like
#define WM_BLAH_1 WM_USER+100
#define WM_BLAH_2 WM_USER+101
....
...
what's a recommended/proper way to define my own custom messages? I'm hoping to make this so that other people can use the control without worrying about custom window message colliding with other custom messages.
thanks in advance,
|
|
|
|
|