I've suddenly become quite stupid.
I'm passing a pointer to a function that can't change the pointer's address.
Can someone tell me why this is happening?

//I have a list of cGroup objects that I access using pointers
cGroup *pg;
pg=GetGroup(editPoly); //just returns a pointer to the group a polygon
belongs to
ChooseGroup(pg); //opens a dialog that displays the current group
and allows you to choose another group
//When ChooseGroup() returns pg hasn't changed!

cGroup *ChooseGroup(cGroup *pg){
INT_PTR ret;
if(ret=DialogBoxParam((HINSTANCE)ghInstance,
MAKEINTRESOURCE(IDD_CHOOSEGROUP),
ghWnd,(DLGPROC)ChooseGroupProc,
(LPARAM)pg)) //pg is passed here
just to display the current group in the dialog box
{
//at this point pg's address is the same as was passed in the
argument list
//and it is the same as it was before calling this function
pg=DlgGroup; //cGroup *DlgGroup is a file scope variable used to
record the choice of group in ChooseGroupProc
return pg; //pg's address has changed here but returns to
it's previous address after exiting the function.
}
else
return 0;
}

I thought I understood this stuff but it looks as though a few brain cells
have gone missing.
This should be simple but I can't see it.

Regards,
Ron Francis
www.RonaldFrancis.com

Re: function can't change a pointer's address by Ulrich

Ulrich
Tue Oct 23 01:27:29 PDT 2007

Ron Francis wrote:
> I'm passing a pointer to a function that can't change the pointer's
> address. Can someone tell me why this is happening?

Firstly, I assume you mean the pointer's value and not its address. You
can't change the address of objects.

Now, when you pass a pointer (or any other object) to a function, that
function in fact receives a copy of that pointer. Any modifications to one
copy are not reflected in other copies.

If it's something else you mean, please provide a minimal example. Your code
was unfortunately not readable because of linebreaks inserted by the
newsreader and it also seemed to contain too much stuff.

cheers

Uli


Re: function can't change a pointer's address by Giovanni

Giovanni
Tue Oct 23 01:33:54 PDT 2007


"Ron Francis" <rfrancis@senet.com.au> ha scritto nel messaggio
news:OxJTnkUFIHA.4956@TK2MSFTNGP06.phx.gbl...
> I've suddenly become quite stupid.
> I'm passing a pointer to a function that can't change the pointer's
> address.
> Can someone tell me why this is happening?
[...]
> ChooseGroup(pg); //opens a dialog that displays the current
> group and allows you to choose another group
> //When ChooseGroup() returns pg hasn't changed!
>
> cGroup *ChooseGroup(cGroup *pg){

If I understand your code correctly, you should do something like:

pg /* new group */ = ChooseGroup(pg /* old group */ );

If you want to pass "pg" as an input/output paramter, you can add one more
level of indirection (via pointer or reference) to change "pg" parameter,
e.g.:

... ChooseGroup( cGroup ** ppg /* [in, out] */ )
{
...
// Save new value of pg
*ppg = DlgGroup; // DlgGroup is a variable of type cGroup *

...
}

Giovanni




Re: function can't change a pointer's address by David

David
Tue Oct 23 02:57:56 PDT 2007

Ron Francis wrote:
> I've suddenly become quite stupid.
> I'm passing a pointer to a function that can't change the pointer's address.
> Can someone tell me why this is happening?
>
> //I have a list of cGroup objects that I access using pointers
> cGroup *pg;
> pg=GetGroup(editPoly); //just returns a pointer to the group a polygon
> belongs to
> ChooseGroup(pg); //opens a dialog that displays the current group
> and allows you to choose another group
> //When ChooseGroup() returns pg hasn't changed!
>
> cGroup *ChooseGroup(cGroup *pg){
> INT_PTR ret;
> if(ret=DialogBoxParam((HINSTANCE)ghInstance,
> MAKEINTRESOURCE(IDD_CHOOSEGROUP),
> ghWnd,(DLGPROC)ChooseGroupProc,
> (LPARAM)pg)) //pg is passed here
> just to display the current group in the dialog box
> {
> //at this point pg's address is the same as was passed in the
> argument list
> //and it is the same as it was before calling this function
> pg=DlgGroup; //cGroup *DlgGroup is a file scope variable used to
> record the choice of group in ChooseGroupProc
> return pg; //pg's address has changed here but returns to
> it's previous address after exiting the function.
> }
> else
> return 0;
> }
>
> I thought I understood this stuff but it looks as though a few brain cells
> have gone missing.
> This should be simple but I can't see it.

Ron:

I think what you want is

cGroup *ChooseGroup(cGroup*& pg);

--
David Wilkinson
Visual C++ MVP

I like: =?UTF-8?Q?=E2=80=9C?= ChooseGroup( cGroup & g ); =?UTF-8?Q?=E2=80=9D.?= by Jeff_Relf

Jeff_Relf
Tue Oct 23 03:51:56 PDT 2007

I like: â?? ChooseGroup( cGroup & g ); â??.

// GetGroup() returns the polygon's group.
cGroup g * GetGroup( editPoly );
// ChooseGroup() ppens a dialog that displays the current group
// and allows you to choose a different group.
ChooseGroup( & g );
.............
ChooseGroup( cGroup & g ) { INT_PTR ret ;

ret = DialogBoxParam( ( HINSTANCE ) ghInstance
, MAKEINTRESOURCE( IDD_CHOOSEGROUP )
// g is only read, not written to.
, ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) & g );

// cGroup * DlgGroup is a file scope variable used
// to record the choice of group in ChooseGroupProc
if ( ret ) g = * DlgGroup ; else memset( & g, 0, sizeof g ); }


Maybe Ron_Francis could do: =?UTF-8?Q?=E2=80=9C?= DlgGroup = GetGroup( editPoly ); =?UTF-8?Q?=E2=80=9D.?= by Jeff_Relf

Jeff_Relf
Tue Oct 23 04:04:35 PDT 2007

Jeffâ? Relf, Individual.NET
2 Minutes, X_Ba, Oct 23, 2007, 3.51 A, BSuRWk

Oops... typos... I meant:

// GetGroup() returns the polygon's group.
cGroup g = * GetGroup( editPoly );
// ChooseGroup() opens a dialog that displays the current group
// and allows you to choose a different group.
ChooseGroup( g );
.............
ChooseGroup( cGroup & g ) { INT_PTR ret ;

ret = DialogBoxParam( ( HINSTANCE ) ghInstance
, MAKEINTRESOURCE( IDD_CHOOSEGROUP )
// g is only read, not written to.
, ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) & g );

// cGroup * DlgGroup is a file scope variable used
// to record the choice of group in ChooseGroupProc
if ( ret ) g = * DlgGroup ; else memset( & g, 0, sizeof g ); }
.............

But, maybe, Ron_Francis could simply do this:

// cGroup * DlgGroup is a file scope variable used
// to record the choice of group in ChooseGroupProc
// GetGroup() returns the polygon's group.
DlgGroup = GetGroup( editPoly );
// Opens a dialog that displays the current group
// and allows you to choose a different group.
DialogBoxParam( ( HINSTANCE ) ghInstance
, MAKEINTRESOURCE( IDD_CHOOSEGROUP )
// DlgGroup is both read and written to.
, ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) DlgGroup );



Re: function can't change a pointer's address by Ron

Ron
Tue Oct 23 21:58:50 PDT 2007

Thank you all for responding.
I though I had done this sort of thing a thousand times, but obviously not.
Must be a senile moment.

Ulrich,
Yes, I wanted the pointer to point to a different address.
Sorry about the code, my newsreader did make a mess of it.

Giovani,
I knew I could do this ...
pg=ChooseGroup(pg);
but if I was passing multiple pointers that wouldn't work.
I also knew that I could use an extra level of indirection like
void ChooseGroup(cGroup **pg )
and I have done in the past, but I couldn't really understand why I had to.
You have clarified what Ulrich said about it being a copy of the pointer.

David,
void ChooseGroup(cGroup*& pg);
This works perfectly and is much tidier for me than **pg .
I haven't used that syntax before and I had to go back to my Lippman book to
work out why it looked backwards to me.
Just to confirm, I'm passing a reference to a pointer to an object of type
cGroup.
Cheers, I will use that a lot.

Jeff,
I'm using pointers so that I don't have to copy chunks of data from one
place to another.
Small point but
cGroup g = * GetGroup( editPoly );
will initialise a cGroup object as well as copying data to it, and if I do
it in a loop it could get expensive.

But maybe you (or anyone else) can give me a good approach to something you
touched on ...

DlgGroup = GetGroup( editPoly );
DialogBoxParam( ( HINSTANCE ) ghInstance
, MAKEINTRESOURCE( IDD_CHOOSEGROUP )
// DlgGroup is both read and written to.
, ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) DlgGroup );

In my case DlgGroup is a file scope variable, (so I don't even need to pass
it in DialogBoxParam( )), but for arguments sake lets say that it is a local
variable.
The problem is that within the procedure, it only lasts for as long as the
call to WM_INITDIALOG: before it goes out of scope.
So if I want to set DlgGroup at case IDOK: I can only think of two ways to
do it.
1. Having a static variable within the procedure to store the lParam at
WM_INITDIALOG:
2. Using a global variable that I don't need to pass in the first place.
Both global and static variables take up memory for the life of the
application so I don't like doing it either way.
Is there a common practice here?

Regards,
Ron Francis
www.RonaldFrancis.com


"Ron Francis" <rfrancis@senet.com.au> wrote in message
news:OxJTnkUFIHA.4956@TK2MSFTNGP06.phx.gbl...
> I've suddenly become quite stupid.
> I'm passing a pointer to a function that can't change the pointer's
> address.
> Can someone tell me why this is happening?
>
> //I have a list of cGroup objects that I access using pointers
> cGroup *pg;
> pg=GetGroup(editPoly); ChooseGroup(pg); //When ChooseGroup()
> returns pg hasn't changed!
>
> cGroup *ChooseGroup(cGroup *pg){
> INT_PTR ret;
> if(ret=DialogBoxParam((HINSTANCE)ghInstance,
> MAKEINTRESOURCE(IDD_CHOOSEGROUP),
> ghWnd,(DLGPROC)ChooseGroupProc,
> (LPARAM)pg)) {
> pg=DlgGroup; return pg; }
> else
> return 0;
> }
>
> I thought I understood this stuff but it looks as though a few brain cells
> have gone missing.
> This should be simple but I can't see it.
>
> Regards,
> Ron Francis
> www.RonaldFrancis.com
>



I prefer using static globals. by Jeff_Relf

Jeff_Relf
Wed Oct 24 00:20:54 PDT 2007

I prefer using static globals myself; for example,
from my X.CPP ( my hand-rolled newsreader )
in â?? www.Cotse.NET/users/jeffrelf/Games.ZIP â??:

// WinProc() is X.CPP's one-and-only â?? Window Procedure â??.
long __stdcall WinProc ( HWND Wnd, uint _WM, uint C, long C2 ) {
PostMessage( Win, _WM, C, C2 ); return 1 ; }

// â?? VisLn â?? is what line to display at the top of the screen.
int ToolBarLines = 1 ;
_VisLn( int L ) { VisLn = Bound ( L, 0, Scro_Max ); }

// Given a screen click, Col_to_Index() returns the glyph.
// Ln.BB[] is the array of all lines, shown or not.
int C, C2 ;
ushort * P_C = ( ushort * ) & C, * P_C2 = ( ushort * ) & C2 ;
LnP Col_to_Index() {
LnP P = Ln.BB [ J_Sel ], E = P + int( * P_C2 / CharW );

While( Ch && P < ( E += Is_Color ? 1 : 1 - _Gy( Ch )->ColsCh ) );
return ! Ch && ! First_Sel ? 0 : P - ! Ch ; }

// â?? Getting( int TheGirl ) â?? is X.CPP's one-and-only event loop.
HWND Wnd ; double Due, Started ;
LnP ButDwn, ButDwnE, ButWas ;
int WM, WasInX, OnScroll, ButUp, OnUp
, OnDwn, OnLD, OnMD, OnRD, LD, MD, RD, OnLU, OnMU, OnRU ;

#define IncY ShowCursor( 1 )
#define DecY ShowCursor( 0 )

SetCur( int X ) { int Y = DecY ;
if ( Y > X ) while ( DecY > X );
else if ( Y < X ) while ( IncY < X ); }

#define WantsOut ( \
WM == WM_APPCOMMAND && P_C2[ 1 ] == APPCOMMAND_CLOSE \
|| WM == WM_SYSKEYDOWN && C == VK_F4 \
|| WM == WM_KEYDOWN && ( C == 27 || C == VK_F4 ) )

const int Any = 1, Spun = 0; MSG Msg;
// #include "Debug.CPP" // To track messages, uncomment.
int Getting( int TheGirl ) {
static int MayReactivate, OnStage = -1, rv ;
static double PaintAlarm ;

Wnd = 0, WM = C = C2 = OnScroll = OnUp = OnDwn
= OnLD = OnMD = OnRD = OnLU = OnMU = OnRU = 0 ;
GetCursorPos( & XY );

int MouseX = XY.x, MouseY = XY.y
, InX = MouseY >= ScrTop && MouseY <= ScrBot ;
if ( PeekMessage( & Msg, 0,0,0, PM_REMOVE ) ) {

static float Y_Was ;
WM = Msg.message, C = Msg.wParam, C2 = Msg.lParam, Wnd = Msg.hwnd ;
// ShWM // From Debug.CPP

if ( WantsOut ) exit( 1 );
if ( WM == WM_MOUSEWHEEL )
OnScroll = short( P_C [ 1 ] ) < 0 ? 1 : -1 ;

Yada, Yada, Yada...


Re: function can't change a pointer's address by Giovanni

Giovanni
Wed Oct 24 01:22:09 PDT 2007


"Ron Francis" <rfrancis@senet.com.au> ha scritto nel messaggio
news:OwW9pufFIHA.1056@TK2MSFTNGP03.phx.gbl...

> But maybe you (or anyone else) can give me a good approach to something
> you touched on ...
>
> DlgGroup = GetGroup( editPoly );
> DialogBoxParam( ( HINSTANCE ) ghInstance
> , MAKEINTRESOURCE( IDD_CHOOSEGROUP )
> // DlgGroup is both read and written to.
> , ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) DlgGroup );
>
> In my case DlgGroup is a file scope variable, (so I don't even need to
> pass it in DialogBoxParam( )), but for arguments sake lets say that it is
> a local variable.
> The problem is that within the procedure, it only lasts for as long as the
> call to WM_INITDIALOG: before it goes out of scope.
> So if I want to set DlgGroup at case IDOK: I can only think of two ways to
> do it.
> 1. Having a static variable within the procedure to store the lParam at
> WM_INITDIALOG:
> 2. Using a global variable that I don't need to pass in the first place.
> Both global and static variables take up memory for the life of the
> application so I don't like doing it either way.

You may consider developing object-oriented code, using a Dialog class.

You could use ATL/WTL or MFC, so you already have dialog classes from which
you can derive your own classes, and add data member to them, to store state
information (like DlgGroup or whatever).
This is the simplest approach.

Or you can build the infrastructure yourself. In this case, you can define a
Dialog class, and pass the "this" pointer to class instance as custom
parameter in DialogBoxParam. When you receive WM_INITDIALOG, you extract
this "this" parameter from lParam, and store it into dialog, using
SetWindowLong (or SetWindowLongPtr) and the DWL_USER slot.
In this way, you can retrieve this value (identifiying your particular
dialog-box instance) in other message handlers, just using GetWindowLong (or
GetWindowLongPtr) and DWL_USER slot again.

Giovanni



Re: function can't change a pointer's address by Giovanni

Giovanni
Wed Oct 24 06:53:29 PDT 2007


"Giovanni Dicanio" <giovanni.dicanio@invalid.it> ha scritto nel messaggio
news:OCFInchFIHA.2004@TK2MSFTNGP06.phx.gbl...
>
> "Ron Francis" <rfrancis@senet.com.au> ha scritto nel messaggio
> news:OwW9pufFIHA.1056@TK2MSFTNGP03.phx.gbl...
>
>> But maybe you (or anyone else) can give me a good approach to something
>> you touched on ...
>>
>> DlgGroup = GetGroup( editPoly );
>> DialogBoxParam( ( HINSTANCE ) ghInstance
>> , MAKEINTRESOURCE( IDD_CHOOSEGROUP )
>> // DlgGroup is both read and written to.
>> , ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) DlgGroup );
>>
>> [...]
>> 1. Having a static variable within the procedure to store the lParam at
>> WM_INITDIALOG:
>> 2. Using a global variable that I don't need to pass in the first place.
>> Both global and static variables take up memory for the life of the
>> application so I don't like doing it either way.
>
> You may consider developing object-oriented code, using a Dialog class.
>
> [...]
> Or you can build the infrastructure yourself.

As a working code sample, you may find my code here to be interesting:

http://www.geocities.com/giovanni.dicanio/vc/#cppdialog

There is a working C++ project that you can download, showing in code the
ideas I was trying to explain in my previous post.

There is an excerpt here (however, I think that you should consider the
original code, which is also full of comments).


----[ Dialog.h ]-------------------------------------------

<code file="Dialog.h">
class CDialog
{
public:
...

// Shows the dialog-box.
INT_PTR Show( HWND hwndParent = NULL );

...

protected:

// Handle to dialog-box.
// Derived classes can access it
HWND m_hDlg;

// Dialog-box procedure,
// to be overridden by derived classes
virtual INT_PTR Proc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
) = 0;


private:
...

// Global dialog-box procedure.
// It routes messages to the instance-specific
// dialog-box procedure.
static INT_PTR CALLBACK GlobalProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);

};

</code>



----[ Dialog.cpp ]-----------------------------------------

<code file="Dialog.cpp">

INT_PTR CDialog::GlobalProc( HWND hwndDlg, UINT uMsg,
WPARAM wParam, LPARAM lParam )
{
// Pointer to dialog class instance
CDialog * theDialog = NULL;

//
// When we receive the WM_INITDIALOG message,
// we store "this" class instance pointer into
// dialog-box private slot (DWL_USER), so we can
// retrieve "this" pointer in next calls, and route
// the messages to instance-specific dialog procedure.
//
if ( uMsg == WM_INITDIALOG )
{
//
// lParam contains the "this" instance pointer,
// passed from ::DialogBoxParam.
// Store this pointer into DWL_USER slot,
// for retrieval in future.
//
::SetWindowLong( hwndDlg, DWL_USER, (LONG) lParam );

theDialog = (CDialog *)lParam;

//
// Save dialog-box handle into proper class
// data member
//
_ASSERTE(hwndDlg != NULL);
theDialog->m_hDlg = hwndDlg;
}
else
{
// Retrieve "this" instance pointer from DWL_USER slot
theDialog = (CDialog *) ::GetWindowLong( hwndDlg,
DWL_USER );
}

// Route message to particular dialog-box instance
if ( theDialog != NULL )
{
return theDialog->Proc( hwndDlg, uMsg, wParam, lParam );
}
else
{
// Default Windows message processing
return FALSE;
}
}


INT_PTR CDialog::Show( HWND hwndParent )
{
_ASSERTE( m_hInstance != NULL );

// Run the dialog-box
INT_PTR result = ::DialogBoxParam(
m_hInstance,
MAKEINTRESOURCE( m_templateID ),
hwndParent,
CDialog::GlobalProc,
(LPARAM) this
);

return result;
}

</code>

You can derive your class from CDialog, and add data members to store your
dialog state information. You must override the Proc() virtual method in
your derived class, and handle messages there (see the CTestDialog in sample
code).

This is how the code looks like in WinMain:

<code>
// Testing dialog-box
CTestDialog dlg( hInstance );

// Set string to be shown to the user
dlg.SetUserString( _T("<< enter text>>") );

// Run the dialog-box.
// (The user can modify the above string.)
dlg.Show();

// Retrieve and show user's string, if user pressed OK button
if ( dlg.UserOK() )
{
::MessageBox( HWND_DESKTOP, dlg.GetUserString(),
_T("User entered:"), MB_OK );
}

</code>


Giovanni



Re: function can't change a pointer's address by Ron

Ron
Wed Oct 24 15:48:35 PDT 2007

Giovanni,

That's something I hadn't thought of before, a wrap around class for dialog
boxes.
I have done something similar to your code with a normal set of windows, so
I'm reasonable familiar with what your code is doing.
Thanks so much for going to the trouble of cutting and pasting that for me.
Much appreciated.

Regards,
Ron Francis
www.RonaldFrancis.com


"Giovanni Dicanio" <giovanni.dicanio@invalid.it> wrote in message
news:eX7WnVkFIHA.748@TK2MSFTNGP04.phx.gbl...
>
> "Giovanni Dicanio" <giovanni.dicanio@invalid.it> ha scritto nel messaggio
> news:OCFInchFIHA.2004@TK2MSFTNGP06.phx.gbl...
>>
>> "Ron Francis" <rfrancis@senet.com.au> ha scritto nel messaggio
>> news:OwW9pufFIHA.1056@TK2MSFTNGP03.phx.gbl...
>>
>>> But maybe you (or anyone else) can give me a good approach to something
>>> you touched on ...
>>>
>>> DlgGroup = GetGroup( editPoly );
>>> DialogBoxParam( ( HINSTANCE ) ghInstance
>>> , MAKEINTRESOURCE( IDD_CHOOSEGROUP )
>>> // DlgGroup is both read and written to.
>>> , ghWnd , ( DLGPROC ) ChooseGroupProc , ( LPARAM ) DlgGroup );
>>>
>>> [...]
>>> 1. Having a static variable within the procedure to store the lParam at
>>> WM_INITDIALOG:
>>> 2. Using a global variable that I don't need to pass in the first place.
>>> Both global and static variables take up memory for the life of the
>>> application so I don't like doing it either way.
>>
>> You may consider developing object-oriented code, using a Dialog class.
>>
>> [...]
>> Or you can build the infrastructure yourself.
>
> As a working code sample, you may find my code here to be interesting:
>
> http://www.geocities.com/giovanni.dicanio/vc/#cppdialog
>
> There is a working C++ project that you can download, showing in code the
> ideas I was trying to explain in my previous post.
>
> There is an excerpt here (however, I think that you should consider the
> original code, which is also full of comments).
>
>
> ----[ Dialog.h ]-------------------------------------------
>
> <code file="Dialog.h">
> class CDialog
> {
> public:
> ...
>
> // Shows the dialog-box.
> INT_PTR Show( HWND hwndParent = NULL );
>
> ...
>
> protected:
>
> // Handle to dialog-box.
> // Derived classes can access it
> HWND m_hDlg;
>
> // Dialog-box procedure,
> // to be overridden by derived classes
> virtual INT_PTR Proc(
> HWND hwndDlg,
> UINT uMsg,
> WPARAM wParam,
> LPARAM lParam
> ) = 0;
>
>
> private:
> ...
>
> // Global dialog-box procedure.
> // It routes messages to the instance-specific
> // dialog-box procedure.
> static INT_PTR CALLBACK GlobalProc(
> HWND hwndDlg,
> UINT uMsg,
> WPARAM wParam,
> LPARAM lParam
> );
>
> };
>
> </code>
>
>
>
> ----[ Dialog.cpp ]-----------------------------------------
>
> <code file="Dialog.cpp">
>
> INT_PTR CDialog::GlobalProc( HWND hwndDlg, UINT uMsg,
> WPARAM wParam, LPARAM lParam )
> {
> // Pointer to dialog class instance
> CDialog * theDialog = NULL;
>
> //
> // When we receive the WM_INITDIALOG message,
> // we store "this" class instance pointer into
> // dialog-box private slot (DWL_USER), so we can
> // retrieve "this" pointer in next calls, and route
> // the messages to instance-specific dialog procedure.
> //
> if ( uMsg == WM_INITDIALOG )
> {
> //
> // lParam contains the "this" instance pointer,
> // passed from ::DialogBoxParam.
> // Store this pointer into DWL_USER slot,
> // for retrieval in future.
> //
> ::SetWindowLong( hwndDlg, DWL_USER, (LONG) lParam );
>
> theDialog = (CDialog *)lParam;
>
> //
> // Save dialog-box handle into proper class
> // data member
> //
> _ASSERTE(hwndDlg != NULL);
> theDialog->m_hDlg = hwndDlg;
> }
> else
> {
> // Retrieve "this" instance pointer from DWL_USER slot
> theDialog = (CDialog *) ::GetWindowLong( hwndDlg,
> DWL_USER );
> }
>
> // Route message to particular dialog-box instance
> if ( theDialog != NULL )
> {
> return theDialog->Proc( hwndDlg, uMsg, wParam, lParam );
> }
> else
> {
> // Default Windows message processing
> return FALSE;
> }
> }
>
>
> INT_PTR CDialog::Show( HWND hwndParent )
> {
> _ASSERTE( m_hInstance != NULL );
>
> // Run the dialog-box
> INT_PTR result = ::DialogBoxParam(
> m_hInstance,
> MAKEINTRESOURCE( m_templateID ),
> hwndParent,
> CDialog::GlobalProc,
> (LPARAM) this
> );
>
> return result;
> }
>
> </code>
>
> You can derive your class from CDialog, and add data members to store your
> dialog state information. You must override the Proc() virtual method in
> your derived class, and handle messages there (see the CTestDialog in
> sample code).
>
> This is how the code looks like in WinMain:
>
> <code>
> // Testing dialog-box
> CTestDialog dlg( hInstance );
>
> // Set string to be shown to the user
> dlg.SetUserString( _T("<< enter text>>") );
>
> // Run the dialog-box.
> // (The user can modify the above string.)
> dlg.Show();
>
> // Retrieve and show user's string, if user pressed OK button
> if ( dlg.UserOK() )
> {
> ::MessageBox( HWND_DESKTOP, dlg.GetUserString(),
> _T("User entered:"), MB_OK );
> }
>
> </code>
>
>
> Giovanni
>
>



Re: function can't change a pointer's address by Giovanni

Giovanni
Thu Oct 25 01:14:44 PDT 2007


"Ron Francis" <rfrancis@senet.com.au> ha scritto nel messaggio
news:egrQcBpFIHA.700@TK2MSFTNGP05.phx.gbl...

> Thanks so much for going to the trouble of cutting and pasting that for
> me.
> Much appreciated.

You're welcome.

Giovanni