|
Basically, in what I'm programming, I use the WndProc function to direct input at my window, however, WndProc must be either a global or a static function. When a function is static, it can only access static members, unless they are created within the scope of the static function. There are two things I can do.
1 - Make the non-static members that I want to call static.
2 - Use static GetInstance() methods to get static instances of the classes which contain my members, and use those classes to access the members.
1 is a lot more arbitrary -- you can apply static to anything, and everything will become static. 2 is a lot cleaner, as I may have to access many members of my class and it would be easier to just get one static instance than make everything in that class static. I'm thinking of going with 2, as the classes of which I'd need to get an instance of, are singletons, so it's not even much of a problem, however, does anyone else have this obsessive anti-static dilemma, where they just want to get rid of everything that's static for the sake of object-oriented programming?
(Note: by the way, if anyone knows how to do these kinds of things without going static at all, that would be even better!)
|
|
|
|
|
The WindowProc needs to be static but that's it.
I try to find any way I can in callbacks to get a pointer passed in there. That pointer is always
a pointer to an object relevent to the callback. One cast, one call, and you're back in non-
static OOP land
It's no different with a windowproc - it's easy to add a pointer to an HWND.
I guess that's your method 2, except the GetInstance method IS the callback.
My 2 cents,
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Yes, there is a way to do that: register the pointer to your object (the this pointer) with SetWindowLong[^] (with the type GWL_USERDATA for the nIndex). Once your WndProc is called, you can retrieve your object pointer by calling GetWindowLong, cast it back to your class and you can call a specific non-static member function to handle the message.
|
|
|
|
|
Interesting, I'll give that a try.
Thanks.
|
|
|
|
|
The advice is good but you may run into a catch 22 when you call CreateWindow. Calls will be made to your WndProc before CreateWindows returns the HWnd to you for you to associate a pointer with. If you ignore those calls to WndProc because the HWnd passed doesn't have an associated pointer then window creation may actually fail and you'll never se your new window. A special case would seem to be required where if your WndProc recieves a call for an HWnd it knows nothing about. One option is to actaully create the Object at this point and do the association before processing the first message. I bet the first message you recieve won't be what you'd expect either
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
I almost can not understand such problems:
I declare a variable first and do some thing after this (especially using atoi() and memset() function).When it gothrough it,the variable would change surpriing.
The CString variable would become NULL and the int variable would become 0.
I know I did not do any work to these surprisingly changed variable.
Why?
GOOD LUCK
|
|
|
|
|
What does the code look like? Is your memset count correct?
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
yes.One condition is like this:
int curPlayer=12;
TCHAR Rools[12];
int index=0;
int temp;
TCHAR* q=Rools;
while(1)
{temp=5;
itoa(temp,q++,10);
if(index>=curPlayer)
break;
}
when the loop is brocken,the value of curPlayer became 0!
|
|
|
|
|
kcynic wrote: when the loop is brocken,the value of curPlayer became 0!
The way you've written the code, the loop will never exit in a controlled manner since the value of neither index nor curPlayer changes inside the loop.
The loop exits because the address space of the index variable will be overwritten in the call to itoa() when q points to that address space. Then index will get a value greater than curPlayer .
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
I am sorry that ,I forgot the code:"++index;" in the loop.
|
|
|
|
|
kcynic wrote: I am sorry that ,I forgot the code:"++index;" in the loop.
Ok, so where do you want to put it? Like this:
int curPlayer = 12;
TCHAR Rools[12];
int index = 0;
int temp;
TCHAR* q = Rools;
while(1)
{
temp = 5;
itoa( temp, q++, 10 );
if( index >= curPlayer )
{
break;
}
++index;
}
Actually I mixed up the order of which the variables are located on the stack in my previous post. What happens is that curPlayer gets overwritten by itoa() with the terminating zero.
In the code above curPlayer will be overwritten when index equals 11.
You have basically made two errors:- You have an array with 12 elements which means that the highest index is 11 and you are deliberately accessing index 12, which is out of bounds. In C/C++ everything is zero-based so the first index is 0 and not 1.
- You haven't accounted for the fact that
itoa() adds a terminating zero to the target string after the conversion.
I suggest you debug the code and step through the loop while watching the memory window so you can see how the array contents change when itoa() is called.
If you increase the Rools array size to 13 you may find extra bytes allocated on the stack due to variable alignment between the Rools array and curPlayer . Those bytes shall never be accessed.
In my opinion you should use a for-loop instead of a "while-forever-loop". It will make more sense and it's also easier to read and understand what the code is supposed to do.
If you want a working example to compare with it could look like this:
int curPlayer = 11;
TCHAR Rools[12];
int index = 0;
int temp = 5;
TCHAR* q = Rools;
for( index = 0; index < curPlayer; ++index )
{
itoa( temp, q++, 10 );
}
Hope this helps
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Thanks,I do know what error happened in the codes,now.
And I would be more carefully in future.
BEST WISHES.
|
|
|
|
|
How can i detect that if my process runs in remote desktop session when another PC connected to my PC ?
Additionally , how can i detect the situation if my PC connects to a remote desktop session ?
THANK YOU VERY MUCH
|
|
|
|
|
Akin Ocal wrote: How can i detect that if my process runs in remote desktop session when another PC connected to my PC ?
Read the %SessionName% environment variable and see if it contains the characters 'RDP'
Best Regards,
-Randor (David Delaune)
|
|
|
|
|
Hi all:
Scott Meyer once stated that:
In C++, when declaring a class which will involve dynamic memory allocation, an extra constructor and an extra operator should be included.
Can I ask what they are?
Thank you
|
|
|
|
|
Scott Meyers is referring to the "copy constructor" and the "copy assignment operator". If you don't create these yourself, the compiler will create default ones. However, these default methods will only do a memory copy. Thus, you will have pointers pointing to the same dynamic memory allocations.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
hi all. i face a very simple problem.
my app outputs two windows using glut. both windows have the same display function and i want to display different things on each window. therefore, within the display function, i want to switch between the windows and display accordingly...how do i go about doing that?
--
dg
|
|
|
|
|
Do you have two dialogs window?
|
|
|
|
|
no..these are two separate windows that are configured to play apps - they are not dialog boxes
--
dg
|
|
|
|
|
I was wondering does someone know any good place to learn about how to use the xloper structure that in inherently built into Excel.
I am trying to pass some Excel ranges into a C++ DLL to do some calculations. What should I do?
|
|
|
|
|
A7Zulu wrote: I am trying to pass some Excel ranges into a C++ DLL to do some calculations. What should I do?
I would use Excel Automation.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
so i actually need the excel data stored something like this
typedef struct {
unsigned short iRows, iCols;
double dArray[FPARRAY_SIZE];
} FPARRAY;
to pass to this financial package library...
i know excel has internal structures that look like this, i was just wondering how to get this...
|
|
|
|
|
I was wondering I created a dll in C++ and a function is called from Excel
typedef struct {
unsigned short iRows, iCols;
double dArray[FPARRAY_SIZE];
} FPARRAY;
I want to return a pointer to this struct back to Excel, so that I can access that new data created in my dll.
If this is impossible, how do I get data set/created in C++ back into Excel so it can be viewed?
Thanks
|
|
|
|
|
|
Hi,
I would like my VC++ client application to dynamically instantiate a class defined in a DLL knowing only (1) a DLL file name and (2) the name of a class defined in the DLL. Is this possible without using .NET?
Here's what I am currently doing...
An exported function in a DLL returns an instance of the class MyDerivedClass (which is a class derived from MyBaseClass):
extern "C" <br />
{<br />
__declspec(dllexport) MyBaseClass* getMyBaseClass()<br />
{<br />
AFX_MANAGE_STATE(AfxGetStaticModuleState())<br />
MyBaseClass* answer = (MyBaseClass*)new MyDerivedClass();<br />
return (answer);<br />
}<br />
}
My client application loads the DLL at the appropriate time and calls the known, exported function to retrieve a pointer to a newly instantiated MyBaseClass object:
CString libraryFileName = ...<br />
<br />
HINSTANCE library = ::LoadLibrary(libraryFileName);<br />
<br />
typedef MyBaseClass* (*ExternalFunction)();<br />
ExternalFunction function = (ExternalFunction)::GetProcAddress(library, "getMyBaseClass");<br />
<br />
MyBaseClass* aBaseClass = function();
This approach works fine but I think it would be cleaner for developers of the DLL to only have to define their MyBaseClass-derived class in the DLL and not have to implement the 'C' function to return it.
Any and all help appreciated!
Peter
|
|
|
|