Create Window with UNICODE defined

New to FreeBASIC? Post your questions here.
Post Reply
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Create Window with UNICODE defined

Post by Juergen Kuehlwein »

This code works for 32 and 64 bit without problems. But as soon as UNICODE is defined the window fails to be created! It keeps telling me, that the window class could not be found - why ?

Did i miss something? The corresponding code (of course with adapted types for variables) for PowerBASIC works as expected - with and without UNICODE defined.

Code: Select all

'#compiler freebasic
'#Define UNICODE
#include once "windows.bi"


declare function FB_MAIN as uinteger
declare FUNCTION WindowProc (BYVAL hWnd AS HWND, BYVAL wMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT


END FB_MAIN


'***********************************************************************************************
'***********************************************************************************************


FUNCTION FB_MAIN AS uinteger
'***********************************************************************************************
' Main Program entry point
'***********************************************************************************************
dim uMsg        AS TAGMSG
dim hinst       AS hinstance
dim wce         AS WNDCLASSEX

#ifdef unicode
dim szClassName AS wSTRing * 64
dim szCaption   AS wstring * 64
#else
dim szClassName AS zSTRing * 64
dim szCaption   AS zstring * 64
#endif

dim hMenu       AS hmenu
dim hwnd        AS hwnd
dim wstyle      AS DWORD
dim xstyle      AS DWORD


  hinst = GetModuleHandle(byval 0)

  szClassName = "NEWTEST"
  wce.cbSize = SIZEOF(wce)
  wce.STYLE = CS_HREDRAW OR CS_VREDRAW              'or CS_DBLCLKS
  wce.lpfnWndProc = procptr(WindowProc)
  wce.hInstance = hInst
  wce.hCursor = LoadCursor(NULL, BYVAL IDC_ARROW)
  wce.hbrBackground = GetSyscolorbrush(COLOR_3DFACE)
  wce.lpszClassName = strptr(szClassName)
  wce.hIcon = LoadIcon(0, BYVAL IDI_APPLICATION)
  wce.hIconSm = LoadIcon(0, BYVAL IDI_APPLICATION)


  RegisterClassEx(@wce)

  szCaption = " New Window"
  wstyle = WS_POPUP OR WS_BORDER OR WS_DLGFRAME OR WS_THICKFRAME OR WS_SYSMENU OR WS_MAXIMIZEBOX OR WS_CLIPSIBLINGS OR WS_CLIPCHILDREN OR WS_VISIBLE OR WS_CAPTION
  xstyle = WS_EX_WINDOWEDGE OR WS_EX_LEFT OR WS_EX_LTRREADING OR WS_EX_RIGHTSCROLLBAR
  hwnd = CreateWindowEx(xstyle, szClassName, szCaption, wstyle, 100, 100, 200, 100, byval 0, byval 0, hinst, byval 0)


  if hwnd = 0 then
    dim szh AS zstring * 1024
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM OR FORMAT_MESSAGE_IGNORE_INSERTS, _
    BYVAL NULL, getlasterror, 0, @szh, 1023, BYVAL NULL )

    messageboxA(0, szh, "Error", MB_OK)
    
    exit function
  end if


  ShowWindow hwnd, SW_SHOWDEFAULT                    'show main window
  UpdateWindow hwnd                                   'draw main window


  DO WHILE GetMessage(@uMsg, BYVAL NULL, 0, 0)       'message loop
    IF IsDialogMessage(hwnd, @uMsg) = 0 THEN          'dialog processing
      TranslateMessage @uMsg
      DispatchMessage @uMsg
    END IF
  LOOP


  FUNCTION = uMsg.wParam


END FUNCTION


'***********************************************************************************************


FUNCTION WindowProc (BYVAL hWnd AS HWND, BYVAL wMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
'***********************************************************************************************
' Main window procedure 
'***********************************************************************************************    


  SELECT CASE wMsg
    CASE WM_CREATE                                   'creation of the main window
      FUNCTION = 0                                    'success
      EXIT FUNCTION


    CASE WM_DESTROY                                  
      PostQuitMessage 0                               'exit message loop in MAIN
      EXIT FUNCTION

  END SELECT


  FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)                  'default processing


END FUNCTION


'***********************************************************************************************
'***********************************************************************************************
'***********************************************************************************************

Any help appreciated - thanks


JK
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Create Window with UNICODE defined

Post by Josep Roca »

Instead of

Code: Select all

wce.lpszClassName = strptr(szClassName)
use

Code: Select all

wce.lpszClassName = @szClassName
From the help file:
Note that when passed a WString, Operator Strptr still returns a ZString Ptr, which may not be the desired result.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Create Window with UNICODE defined

Post by Juergen Kuehlwein »

Thanks José,

i did try this before posting, it doesn´t help either, just as these:

Code: Select all

  wce.lpszClassName = cast (wstring ptr, @szClassName)
  wce.lpszClassName = cast (wstring ptr, strptr(szClassName))

Maybe i´m blind, but for more than two hours now i have not been able to get it running with UNICODE defined...


JK
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Create Window with UNICODE defined

Post by Josep Roca »

This compiles (and runs) in both 32/64 bit, ansi or unicode.

Code: Select all

'#compiler freebasic
#Define UNICODE
#include once "windows.bi"


declare function FB_MAIN as uinteger
declare FUNCTION WindowProc (BYVAL hWnd AS HWND, BYVAL wMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT


END FB_MAIN


'***********************************************************************************************
'***********************************************************************************************


FUNCTION FB_MAIN AS uinteger
'***********************************************************************************************
' Main Program entry point
'***********************************************************************************************
dim uMsg        AS TAGMSG
dim hinst       AS hinstance
dim wce         AS WNDCLASSEX

#ifdef unicode
dim szClassName AS wSTRing * 64
dim szCaption   AS wstring * 64
#else
dim szClassName AS zSTRing * 64
dim szCaption   AS zstring * 64
#endif

dim hMenu       AS hmenu
dim hwnd        AS hwnd
dim wstyle      AS DWORD
dim xstyle      AS DWORD


  hinst = GetModuleHandle(byval 0)

  szClassName = "NEWTEST"
  wce.cbSize = SIZEOF(wce)
  wce.STYLE = CS_HREDRAW OR CS_VREDRAW              'or CS_DBLCLKS
  wce.lpfnWndProc = procptr(WindowProc)
  wce.hInstance = hInst
  wce.hCursor = LoadCursor(NULL, BYVAL IDC_ARROW)
  wce.hbrBackground = GetSyscolorbrush(COLOR_3DFACE)
  wce.lpszClassName = @szClassName
  wce.hIcon = LoadIcon(0, BYVAL IDI_APPLICATION)
  wce.hIconSm = LoadIcon(0, BYVAL IDI_APPLICATION)


  RegisterClassEx(@wce)

  szCaption = " New Window"
  wstyle = WS_POPUP OR WS_BORDER OR WS_DLGFRAME OR WS_THICKFRAME OR WS_SYSMENU OR WS_MAXIMIZEBOX OR WS_CLIPSIBLINGS OR WS_CLIPCHILDREN OR WS_VISIBLE OR WS_CAPTION
  xstyle = WS_EX_WINDOWEDGE OR WS_EX_LEFT OR WS_EX_LTRREADING OR WS_EX_RIGHTSCROLLBAR
  hwnd = CreateWindowEx(xstyle, szClassName, szCaption, wstyle, 100, 100, 200, 100, byval 0, byval 0, hinst, byval 0)


  if hwnd = 0 then
    dim szh AS zstring * 1024
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM OR FORMAT_MESSAGE_IGNORE_INSERTS, _
    BYVAL NULL, getlasterror, 0, @szh, 1023, BYVAL NULL )

    messageboxA(0, szh, "Error", MB_OK)
    
    exit function
  end if


  ShowWindow hwnd, SW_SHOWDEFAULT                    'show main window
  UpdateWindow hwnd                                   'draw main window


  DO WHILE GetMessage(@uMsg, BYVAL NULL, 0, 0)       'message loop
    IF IsDialogMessage(hwnd, @uMsg) = 0 THEN          'dialog processing
      TranslateMessage @uMsg
      DispatchMessage @uMsg
    END IF
  LOOP


  FUNCTION = uMsg.wParam


END FUNCTION


'***********************************************************************************************


FUNCTION WindowProc (BYVAL hWnd AS HWND, BYVAL wMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
'***********************************************************************************************
' Main window procedure 
'***********************************************************************************************    


  SELECT CASE wMsg
    CASE WM_CREATE                                   'creation of the main window
      FUNCTION = 0                                    'success
      EXIT FUNCTION


    CASE WM_DESTROY                                  
      PostQuitMessage 0                               'exit message loop in MAIN
      EXIT FUNCTION

  END SELECT


  FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)                  'default processing


END FUNCTION


'***********************************************************************************************
'***********************************************************************************************
'***********************************************************************************************
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Create Window with UNICODE defined

Post by Juergen Kuehlwein »

Forget about it!

When run in another IDE it works, so it must be something with my setup, i was searching in the wrong place :-(

JK
Post Reply