Announcement

Collapse
No announcement yet.

When to use PBMAIN vs. WINMAIN

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • When to use PBMAIN vs. WINMAIN

    Under what circumstances would I want to use PBMAIN instead of WINMAIN (or, is it the other way around?)?

    Thanks,
    Dale

    ------------------

  • #2
    PBMAIN could be considered to operate similarly to a macro. That is, during compilation, the PBMAIN prototype actually gets replaced with a WINMAIN-like function prototype. Therefore, from the Operating System's perspective, there is no difference whether your app uses PBMAIN or WINMAIN.

    However, you would use WINMAIN() instead of PBMAIN if you need to utilize the parameters that are accessable via WINMAIN: hInstance&, hPreInstance&, lpCmdLine, or nCmdShow&.

    You can easily get the hInstance& handle from within your app with the GetModuleHandle() API, and COMMAND$ contains the commain-line parameters, so for these two, so the use of PBMAIN or WINMAIN is not really relevent.

    The hPrevInstance& handle is not used in Win32, so that parameter is never going to be used in a real application anyway.

    This leaves the nCmdShow& parameter...

    This one is important for GUI applications: when an application is started, it should honor the initial ShowWindow() state that is directed by the nCmdShow& parameter.

    For example, if you create a short-cut to an app, you can set the short-cut to "dictate" the app's initial window state for the app (Normal, Maximized, or Minimized). If your app ignores the nCmdShow& parameter, your app will not honor the window state it is "expected" to use.

    Another example is an app that is started via the SHELL statement/function - SHELL has an optional parameter to dictate the initial window state, and this gets passed to the target app as the nCmdShow& parameter. Therefore, if the app is launched as Minimized, the poor app user should reasonably expect the app to actually _be_ minimized!

    In my experience, programmers often make the "mistake" of ignoring the nCmdShow& parameter! (yes, II've been guilty of it too!)

    Clear as mud?

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      So Lance,

      Does that mean that using PBMain is the same as ignoring the nCmdShow& parameter? or should we go and get this information some other way? You went to a great deal of trouble to explain it's function but not how the PBMAIN() function handles it.


      ------------------

      Paul Dwyer
      Network Engineer
      Aussie in Tokyo
      (Paul282 at VB-World)

      Comment


      • #4
        Since PBMAIN does not make any parameters available to your code, you won't be able to access the nCmdShow& parameter. PBMAIN essentially "hides" these parameters from your code.

        So in that sense, using PBMAIN is a good way to ignore the nCmdShow& parameter.

        PBMAIN is often used in code on this BBS since it involves less typing and confusion, but it does not mean that it is always the best way to implement a GUI app that adheres to the MS GUI design guidelines.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Interesting knowledge, Lance. I always thought DIALOG SHOW MODAL, etc
          took care of this automatically, but obviously not. Good to have
          in mind in the future.

          ------------------

          Comment


          • #6
            Ahhhh, so to conclude

            "using PBMAIN is a good way to... " "make the 'mistake' of ignoring the nCmdShow& parameter" ??

            Gee, I'm in a poor mood for a friday night

            ------------------

            Paul Dwyer
            Network Engineer
            Aussie in Tokyo
            (Paul282 at VB-World)

            Comment


            • #7
              Paul, sounds like you need to hop a train to Roppongi or Shibuya and party on!


              ------------------
              Ron

              Comment


              • #8
                Love to, but I live on the other side of town and transport here sux, If I need to get home after midnight then the taxi is about US$130
                I'm trying to give up the "Get the first morning train home" lifestyle, must be getting old.



                ------------------

                Paul Dwyer
                Network Engineer
                Aussie in Tokyo
                (Paul282 at VB-World)

                Comment


                • #9
                  Lance,

                  I have a desktop shortcut to an executable compiled by PowerBasic and using
                  PBMAIN. When I change the shortcut to 'run minimized' the request is honored.
                  Although nCmdShow& cannot be accessed by my program, it obviously is being
                  accounted for somehow.

                  ------------------

                  Comment


                  • #10
                    Yes, just tested a simple app' myself. PBMAIN seems to do the job
                    like it should. Good!


                    ------------------

                    Comment


                    • #11
                      Your observations are a behavior of Windows, not your code or PBMAIN per se.

                      The API doc's state that the nCmdShow should be honored unless the parent process provides the appropriate STARTUPINFO structure when launching the app (Subsequent executions of ShowWindow() can use any particular %SW_ style so desired). If the STARTUPINFO structure is not provided at startup, then nCmdShow should (must) be used for the first call to ShowWindow().

                      In other words, creating a shortcut per your test (or using SHELL from PB) does provide such a structure to Windows, and this dictates how the initial app window will be shown by Windows.

                      Actually, if your initial window is not resizable, maximizing it this way makes the window look terrible!

                      Hmmm... the more I think about it, the less clear it becomes about how to "force" an app to have to honor nCmdShow. Any ideas?

                      ------------------
                      Lance
                      PowerBASIC Support
                      mailto:[email protected][email protected]</A>
                      Lance
                      mailto:[email protected]

                      Comment


                      • #12
                        The basic code to start a window before a WinMain has no method
                        of receiving information from the operating system as such.

                        This is MASM for the pre-WinMain code,
                        Code:
                        start:
                              invoke GetModuleHandle, NULL
                              mov hInstance, eax
                        
                              invoke GetCommandLine
                              mov CommandLine, eax
                        
                              invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
                              invoke ExitProcess,eax
                        The parameters you receive in a WinMain are what the code before
                        it sends to it, the action is in the style SW_SHOWDEFAULT.

                        Code built in this manner honours the operating system choice of
                        starting minimised, normal or maximised.

                        A PBmain() is a lower level method that a WinMain and you can do
                        the same things in it, get the instance handle (start address in
                        memory), a command line if you need it and you can set the
                        ShowWindow() to the style you need directly in the code.

                        It is a good alternative as a WinMain is a 16 bit windows leftover
                        that has a dead previous instance parameter.

                        To answer you original question, there is not a lot of reason to
                        use a WinMain these days, you can get all of the information without
                        it.

                        Regards,

                        [email protected]

                        [This message has been edited by Steve Hutchesson (edited February 17, 2001).]
                        hutch at movsd dot com
                        The MASM Forum

                        www.masm32.com

                        Comment

                        Working...
                        X