[RESOLVED] How to pass a struct to a dialog (winapi)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] How to pass a struct to a dialog (winapi)

  1. #1
    Join Date
    Oct 2012
    Posts
    15

    [RESOLVED] How to pass a struct to a dialog (winapi)

    I want to pass some parameters to a dialog. I am trying to pass a struct as LPARAM in CreateDialogParam.

    How to access this structure from ONOK for example?

    Code:

    struct DlgParam{
    std::wstring IEDllPath;
    CString folderName;
    };

    ...

    DlgParam param;
    param.folderName = folderName;
    param.folderPath = folderPath;

    hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc, reinterpret_cast<LPARAM>(&param));

    ...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    Re: How to pass a struct to a dialog (winapi)

    Didn't you read the CreateDialogParam documentation?
    It describes it very clear:
    dwInitParam [in]
    Type: LPARAM
    The value to be passed to the dialog box procedure in the lParam parameter in the WM_INITDIALOG message.
    So handle WM_INITDIALOG message and check the LPARAM!
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2012
    Posts
    15

    Re: How to pass a struct to a dialog (winapi)

    I think I am missing something basic here. Maybe I describe what I am trying to do:

    I want to display a dialog with 2-3 buttons and some text. When the user clicks one of the buttons I want to display to him as text the contents of a struct. I have the struct in main and I do not know how to access it in the dialog's onButtonClick.

    I thought it is done via lparam. So I expected to create the dialog with the struct in lparam and then the callback function will pass it along to onButton clicked. (Maybe this is not the right way to do that)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    Re: How to pass a struct to a dialog (winapi)

    Quote Originally Posted by krs0 View Post
    So I expected to create the dialog with the struct in lparam and then the callback function will pass it along to onButton clicked. (Maybe this is not the right way to do that)
    According to MSDN it is passed in not "along to onButton clicked" notification but as LPARAM with WM_INITDIALOG message!
    So handle WM_INITDIALOG message, cast the passed in LPARAM to your struct pointer and save somewhere to data of this structure. Then you'll be able to use this data for your dialog.
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2012
    Posts
    15

    Re: How to pass a struct to a dialog (winapi)

    OK. Now I can access struct's members when I catch WM_INITDIALOG in my custom onInitDialog().

    I still do not understand how to access/pass this struct from/to onButtonClicked().

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    Re: How to pass a struct to a dialog (winapi)

    You don't need WM_INITDIALOG or LPARAM. Looks like you were headed in the right direction in your first post. You can create and set member variables in the dialog and set them after you instantiate but before you Create the dialog. Once you do that, you can access them at any time inside the dialog. You could create individual members and copy them from the struct, or just declare a pointer to your struct, and populate it.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    Re: How to pass a struct to a dialog (winapi)

    Quote Originally Posted by krs0 View Post
    OK. Now I can access struct's members when I catch WM_INITDIALOG in my custom onInitDialog().

    I still do not understand how to access/pass this struct from/to onButtonClicked().
    Do you or don't use any C++ framework? If you do, Arjay is right, and you never need it be a separate structure. But if you don't, you need to invent some mechanism that allows the structure address be found by dialog window handle. Global map, or SetProp/GetProp typically do the trick.
    Best regards,
    Igor

  8. #8
    Join Date
    Oct 2012
    Posts
    15

    Re: How to pass a struct to a dialog (winapi)

    I do not use any c++ framework.

    What I settled with, in the end is using a global variable that I can access from my Dialog.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center