Announcement

Collapse
No announcement yet.

Instance?

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

  • Instance?


    Code:
    FUNCTION WINMAIN(BYVAL Instance      AS LONG, _
                     BYVAL hPrevInstance AS LONG, _
                     lpszCmdLine         AS ASCIIZ PTR, _
                     BYVAL nCmdShow      AS LONG) AS LONG
    
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
    
    END FUNCTION
    When using PBMain instead of WINMAIN in PB, how would I retrieve
    the Instance (which is available in the WINMAIN Function)?


    Thanks
    MWM


    ------------------
    mwm
    mwm

  • #2
    Michael:

    In PBMAIN...
    Code:
       DIM hCurInst AS LONG
    
    
       hCurInst = GetWindowLong(Dlg, %GWL_HINSTANCE)
    Timm
    mailto:[email protected]
    Tsunami Record Manager

    Comment


    • #3
      Thanks Tim,

      GetWindowLong <---- couldn't remember that one!


      Thanks Again!

      MWM


      ------------------
      mwm
      mwm

      Comment


      • #4
        Michael,

        once you have a window created, you can use:
        Code:
        Instance = GetWindowLong( hWindow, %GWL_HINSTANCE )
        of course hWindow is the window handler

        A question: if you need the Instance, why do you use PBMAIN instead of WINMAIN?

        [added later]
        I was writing this post while Timm riplied. But the question is still valid...
        ------------------

        [This message has been edited by Aldo Cavini (edited June 25, 2001).]
        Rgds, Aldo

        Comment


        • #5
          Thanks Aldo,

          In response to your question:

          Mainly, I use PBMain for DDT - and WIMAIN with standard
          SDK Coding! I also use PBMain for DLL's, much easier for
          me to read!

          However, in some rare cases, I do need to know what the
          instance of the application when working with imbeded resources!

          Thanks Again

          MWM




          ------------------
          mwm
          mwm

          Comment


          • #6
            GetWindowLong is enough exotic way.
            Native method - GetModuleHandle("") [ or, if you like this more, GetModuleHandle(ByVal 0&) ]

            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              The problem with WinMain is all the arguments. But you can
              actually do this:
              Code:
              function WinMain(byval hInstance as long) as long
              Regards
              Peter

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


              [This message has been edited by Peter P Stephensen (edited June 26, 2001).]
              [email protected]
              www.dreammodel.dk

              Comment


              • #8
                Not prudent.

                ------------------
                Tom Hanlin
                PowerBASIC Staff

                Comment


                • #9
                  I agree with Semen... GetModuleHandle is the appropriate function to use. It does not require the creation of a window.

                  Consider this: In order to create a window with any of the CreateWindow*, CreateDialog* or DialogBox* APIs, you need to use an instance handle for one of the parameters. So you can't really create a window in order to get the instance handle. True, if you use DDT it takes care of the instance handle internally, but it's pretty likely that it uses GetModuleHandle under the hood, (or maybe it captures the WinMain value, I don't know) so you are really going "the long way around" if you create a window in order to get the instance.

                  And it's pretty inconvenient to create a window in a PB/CC program just to get the instance.

                  -- Eric


                  ------------------
                  Perfect Sync Development Tools
                  Perfect Sync Web Site
                  Contact Us: mailto:[email protected][email protected]</A>



                  [This message has been edited by Eric Pearson (edited June 26, 2001).]
                  "Not my circus, not my monkeys."

                  Comment


                  • #10
                    Code:
                      function WinMain(byval hInstance as long) as long
                    Not prudent.
                    Pray, wither not?

                    Wouldst seem indifferentiable from using PBMAIN() which ignores four (4) passed parameters.


                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Michael --

                      "PBMain" isn't an official Windows function name. I'd speculate that when you type PBMain, the compiler subsitutes WinMain with the appropriate parameters. But if you type WinMain it uses the parameters that you specify. Then, if Windows puts the standard four parameters on the stack when it starts the app, but your program only declares (and thereby automatically removes) one of them... trouble's a-brewing.

                      It might be perfectly ok to use a single-parameter WinMain, but your code would be responsible for popping the other three off the stack.

                      -- Eric


                      ------------------
                      Perfect Sync Development Tools
                      Perfect Sync Web Site
                      Contact Us: mailto:[email protected][email protected]</A>



                      [This message has been edited by Eric Pearson (edited June 26, 2001).]
                      "Not my circus, not my monkeys."

                      Comment


                      • #12
                        Semen & Eric, I totally agree with your first comments!

                        GetModuleHandle does what I need it to do!

                        It's interesting to see the different approaches here!
                        Although I did have a solution, the problem is that PB
                        (being so powerful), allows me to program without knowing
                        the faults/traps that await me! which is the
                        programmers responsiblity in the end.!

                        However, how can one know without asking? (just what I did)...
                        Although I knew enough in getting an application (Instance),
                        what I didn't know, was my coding correct?

                        In this case, I would have been totally wrong, even though for
                        the moment, the program function'd correctly!

                        I'm not gonna butter you all up, but you guys are great!

                        Later
                        MWM




                        ------------------
                        mwm
                        mwm

                        Comment


                        • #13
                          I find it weird that it's poss. to use PBMain as libmain()
                          Libmain can be called several times, by using threads or on close..

                          ??

                          ------------------
                          hellobasic

                          Comment


                          • #14
                            Edwin --

                            Do you mean PBLIBMAIN? (PBMain = WinMain, PBLibMain = LibMain.)

                            In simple DLLs, all you have to do is set FUNCTION = 1 whenever LibMain is called. So there's no need for any parameters.

                            -- Eric


                            ------------------
                            Perfect Sync Development Tools
                            Perfect Sync Web Site
                            Contact Us: mailto:[email protected][email protected]</A>
                            "Not my circus, not my monkeys."

                            Comment


                            • #15
                              Guys,

                              I think something has been missed here, PBmain() is a lower level
                              function than WinMain(). WinMain is a leftover from 16 bit windows
                              but with 32 bit windows, a start function like PBmain is free of the
                              assumptions of a WinMain.

                              The instance handle is the actual start address of the PE file
                              so if you need the instance handle for certain API calls, the correct
                              way to get it is to use GetModuleHandle(). In PowerBASIC you
                              usually don't use the API to get the command line but use COMMAND$
                              instead.

                              With pseudo code like as follows,
                              Code:
                                  GLOBAL hInstance as LONG
                                  GLOBAL cmd$
                                
                              FUNCTION PBmain() as LONG
                                
                                  LOCAL returnval as LONG
                                
                                  hInstance = GetModulehandle(ByVal %NULL)
                                  cmd$ = command$
                                
                                  ' allocate memory etc ... here
                                
                                  returnval = MyApplication_Code()
                                
                                  ' deallocate memory etc ... here
                                
                                  FUNCTION = returnval
                                
                              END FUNCTION
                              You simply do not need a WinMain() function.

                              Regards,

                              [email protected]

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

                              www.masm32.com

                              Comment


                              • #16
                                Hutch --

                                Are you sure? Win32.HLP says...

                                The WinMain function is called by the system as the initial entry point for a Win32-based application.

                                It lists the four standard parameters. It may be a leftover from the days of 16 bits, and at least one of the parameters no longer does anything, but it is still a required function.

                                Isn't it?

                                -- Eric


                                ------------------
                                Perfect Sync Development Tools
                                Perfect Sync Web Site
                                Contact Us: mailto:[email protected][email protected]</A>
                                "Not my circus, not my monkeys."

                                Comment


                                • #17

                                  I thought so too, Eric...
                                  http://msdn.microsoft.com/library/de...nprocedure.asp

                                  However, doesn't the PB-Compiler - when faced with options - trigger which
                                  point of entry ---> WinMain or DllMain etc...
                                  that made when we compile our program EXE or DLL ect...
                                  that's what determines the actual call???

                                  Where's Tom when you need him?

                                  Interesting

                                  MWM




                                  ------------------
                                  mwm
                                  mwm

                                  Comment


                                  • #18
                                    > The WinMain function is called by the system as the initial entry point for a Win32-based application.

                                    Correct, but MS lost some words as always.
                                    Compile following
                                    Code:
                                      #Compile Exe
                                       Function PbMain
                                          MsgBox "Ok"
                                       End Function
                                    I will be very surprise, if you will find in Exe any export function, especially WinMain.
                                    So, I fully agree with Steve.


                                    ------------------
                                    E-MAIL: [email protected]

                                    Comment


                                    • #19
                                      Eric,

                                      From memory Microsoft C at the time of the change to 32 bit code
                                      had to use WinMain as its entry point as it was still working in
                                      both 16 and 32 bit but at a lower level, 32 bit code does not have
                                      to be done that way.

                                      In MASM you construct the WinMain if you wish to code it that way.
                                      Code:
                                        ; #########################################################################
                                        
                                        .code
                                        
                                        start:
                                              invoke GetModuleHandle, NULL
                                              mov hInstance, eax
                                        
                                              invoke GetCommandLine
                                              mov CommandLine, eax
                                        
                                              invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
                                              invoke ExitProcess,eax
                                        
                                        ; #########################################################################
                                        
                                        WinMain proc hInst      :DWORD,
                                                     hPrevInst  :DWORD,
                                                     CmdLine    :DWORD,
                                                     CmdShow    :DWORD
                                      
                                          ; further code etc ....
                                      PowerBASIC gives you the option to do the same if you want to do it
                                      that way, you would of course have to call it something else as WinMain
                                      is a reserve word in PowerBASIC.

                                      Regards,

                                      [email protected]


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

                                      www.masm32.com

                                      Comment


                                      • #20
                                        I have used the form:
                                        Code:
                                        function WinMain(byval hInstance as long) as long
                                        for a while. I have not experienced any problems. What problems
                                        could I experience in the future by using this method?

                                        Regards
                                        Peter

                                        ------------------
                                        Peter Stephensen
                                        [email protected]
                                        [email protected]
                                        www.dreammodel.dk

                                        Comment

                                        Working...
                                        X