Help Debuging

Started by J P Eskdale, December 04, 2013, 09:15:30 PM

Previous topic - Next topic

José Roca

Sorry, but I can't find what causes the hang. I have written the test below, that works perfectly, but when I replace the FRMWEB_Show function and callback with mine in your application, it hangs. Can't figure why. I'm using similar code in my CSED editor and it works.


#COMPILE EXE
#DIM ALL
'%UNICODE = 1
%USEOLECON = 1
%USEWEBBROWSER = 1

' // Include files for external files
#INCLUDE ONCE "CWindow.inc"   ' // CWindow class

%IDC_WB_CONTROL = 101
%IDC_WB_PRINT   = 102
%IDC_WB_PREVIEW = 103

' ========================================================================================
FUNCTION FRMWEB_Show ( _
                    ByVal hWndParent As Dword, _
                    ByVal ShowModalFlag As Long, _
                    Optional ByVal UserData As Long _
                    ) AS LONG

   ' // Create an instance of the class
   LOCAL pPrint AS IWindow
   pPrint = CLASS "CWindow"
   IF ISNOTHING(pPrint) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pPrint.CreateWindow(hWndParent, "Print Preview", 0, 0, 0, 0, _
          %WS_VISIBLE OR %WS_CAPTION OR %WS_POPUPWINDOW, %WS_EX_WINDOWEDGE, _
          CODEPTR(CSED_WB_PrintPreview_WindowProc))
   pPrint.SetClientSize 700, 440
   pPrint.CenterWindow(hwnd, hWndParent)

   LOCAL hCtl AS DWORD
   hCtl = pPrint.AddWebBrowserControl(hwnd, %IDC_WB_CONTROL, "www.bbc.co.uk", NOTHING, 0, 0, pPrint.ClientWidth, pPrint.ClientHeight - 40, -1)

   ' // Add the buttons
   pPrint.AddButton(hwnd, %IDC_WB_PRINT, "&Print", pPrint.ClientWidth - 280, pPrint.ClientHeight - 30, 75, 23, -1)
   pPrint.AddButton(hwnd, %IDC_WB_PREVIEW, "Pre&view", pPrint.ClientWidth - 185, pPrint.ClientHeight - 30, 75, 23, -1)
   pPrint.AddButton(hwnd, %IDCANCEL, "&Close", pPrint.ClientWidth - 90, pPrint.ClientHeight - 30, 75, 23, -1)

   ' // Default message pump (you can replace it with your own)
   pPrint.DoEvents

END FUNCTION
' ========================================================================================

' ========================================================================================
' Editor options callback function.
' ========================================================================================
FUNCTION CSED_WB_PrintPreview_WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL pIWebBrowser2 AS IWebBrowser2           ' // IWebBrowser2 interface reference
#IF %DEF(%UNICODE)
   LOCAL szText AS WSTRINGZ * 255                ' // General purpose variable
   LOCAL szPath AS WSTRINGZ * %MAX_PATH          ' // File path
#ELSE
   LOCAL szText AS ASCIIZ * 255                  ' // General purpose variable
   LOCAL szPath AS ASCIIZ * %MAX_PATH            ' // File path
#ENDIF

   SELECT CASE uMsg

      CASE %WM_CREATE
         ' // Disable the owner of the modal window
         EnableWindow GetWindow(hwnd, %GW_OWNER), %FALSE

      CASE %WM_COMMAND

         SELECT CASE LO(WORD, wParam)

            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF

            CASE %IDC_WB_PRINT
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // Gets a reference to the IWebBrowser2 interface
                  pIWebBrowser2 = OC_GetDispatch(GetDlgItem(hwnd, %IDC_WB_CONTROL))
                  IF ISOBJECT(pIWebBrowser2) THEN
                     pIWebBrowser2.ExecWB %OLECMDID_PRINT, %OLECMDEXECOPT_PROMPTUSER
                     ' // Releases the interface
                     pIWebBrowser2 = NOTHING
                  END IF
               END IF
               EXIT FUNCTION

            CASE %IDC_WB_PREVIEW
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // Gets a reference to the IWebBrowser2 interface
                  pIWebBrowser2 = OC_GetDispatch(GetDlgItem(hwnd, %IDC_WB_CONTROL))
                  IF ISOBJECT(pIWebBrowser2) THEN
                     pIWebBrowser2.ExecWB %OLECMDID_PRINTPREVIEW, %OLECMDEXECOPT_PROMPTUSER
                     ' // Releases the interface
                     pIWebBrowser2 = NOTHING
                  END IF
                  EXIT FUNCTION
               END IF

         END SELECT

      CASE %WM_CLOSE
         ' // The owner window is enabled in WM_CLOSE rather than WM_DESTROY to
         ' // prevent the application from losing the focus. In WM_DESTROY the
         ' // modal window has already been removed from the screen by the system.
         ' // Because the remaining windows are disabled, the system gives the
         ' // focus to another application.
         EnableWindow GetWindow(hwnd, %GW_OWNER), %TRUE

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
   IF AfxGetWindowsVersion => 6 THEN SetProcessDPIAware

   ' // Create an instance of the class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   ' // Note: CW_USEDEFAULT is used as the default value When passing 0's as the width and height
   pWindow.CreateWindow(%NULL, "CWindow with a button", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   ' // Set the client size
   pWindow.SetClientSize 500, 320
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a button
   pWindow.AddButton(pWindow.hwnd, %IDOK, "&View", 350, 250, 75, 23)

   

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   ' // Process window mesages
   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDOK
               FRMWEB_Show(hwnd, 0)
               EXIT FUNCTION
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================


José Roca

#16
Found it!


COUNINITIALIZE
COINITIALIZEEX BYVAL %NULL, %COINIT_MULTITHREADED


If you remove these lines, the WebBrowser control will work.

The WebBrowser control must run in a Single Threaded Apartment.

J P Eskdale

Hi Jose

Thank you so much - As I mentioned in an earlier post I've been away for a couple of days - Back to the PC today and yes removing those lines fixed the web viewer issue.

I'm not sure I understand the rules of how to use COINITIALIZEX as yet - another learning curve.  They were added as a possible fix for the problem with the threading I was having.  Need to go and do some testing now to see what does and doesn't work

I must just say how much I appreciate you spending the time to look at it for me - If there is anything I can do for you don't hesitate to ask.