Thread: Passing Parameters in Win32 with C?

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Temecula, CA
    Posts
    4

    Question Passing Parameters in Win32 with C?

    I am trying to convert a C89 console program I wrote into a Win32 app. I'm new to Win32 and still learing the basics. Here's where I'm stuck, conceptually:

    My console program manages my data structures locally in main(). It passes structure pointers (and other relevant local variables) to various functions as it goes through its calculation algorithm. My program makes no use of global variables.

    Win32 operates through WinMain(), using the "message loop," which interacts with a WinProc(). If I were to maintain the same structures locally in WinMain(), I don't see how I could provide the underlying GUI functions access to my program data, save declaring them global.

    I'm guessing that simply declaring everything global isn't good programming practice in Win32 any more than it would be in console programming. This makes me think I'm missing something big.

    Note: I have a Petzold book on order, and I'm absolutely a WinAPI newbie. Any direction would be appreciated!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Welcome to the wacky world of event-driven programming!

    Windows programs often use global data and I would suggest that you go with that option at this point in your studies. There are alternatives. You can associate window objects with data, for instance. Look into the last parameter of CreateWindowEx.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I should mention another common practice which is to use static data in your WndProc function. Initialize the data in the WM_CREATE message handler and do any necessary clean up in the WM_DESTROY handler.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2012
    Location
    Temecula, CA
    Posts
    4
    oogbooga, Thanks a million for responding and keeping me on the right track! I like the idea of static variables in WndProc. Not really knowing where this Win32 stuff is taking me in the future, I wonder: If I later create a child window within WndProc, and that window also requires data access, do I run into the same scope problem? Maybe that's the point at which I associate specific data with the class? (Sorry if that doesn't make sense.) ... I'm imagining a scenario where a dialog box or child window pops up, allowing the user to modify a specified aspect of a structure, the whole of which can be re-processed through WndProc. Thanks!

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The function DialogBoxParam allows you to pass in a pointer to your data.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    "...I associate specific data with the class..."

    When you create a class you can set some extra data through the 'cbWndExtra' variable on the WNDCLASSEX struct; with this you will be able to store a struct (a pointer to a struct, a single variable, whatever) and recover it on the window scope; this is useful when you create custom controls to store colors, text and other stuff, but it is also useful if you want to set some window data without handling static variables. To manage it you have to use the functions GetWindowLong and SetWindowLong to save the window data (note that the value index on those functions will be 0 because of the extra window bytes that you reserved on the window class); also that will leave the GWL_USERDATA window area free for other purposes. Remembre that if you allocate memory for a struct and set its pointer to the extra window bytes, you will be the responsable to free that memory block.

    Hope that helps
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing on parameters
    By Allen in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2012, 12:55 AM
  2. passing functions as parameters?
    By dayalsoap in forum C Programming
    Replies: 8
    Last Post: 08-27-2010, 06:16 PM
  3. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM
  4. Passing parameters
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2002, 10:07 AM