EX_DDT_MenuWithIcons_01 - fails @64bit - warning with pointer to scalar using

Started by hajubu, August 10, 2025, 06:21:17 PM

Previous topic - Next topic

hajubu

Hi, as testing 32/64 compiling various option with adding icons to the title bar
found the cause in 64bit of

SendMessage (hDlg, WM_SETICON, ICON_SMALL, Cast( Dword, _
             LoadImage (GetModuleHandle (NULL), "MyIcon.ICO", _
                                IMAGE_ICON, 0, 0, LR_LOADFROMFILE ) ) )
andalso

SendMessage (hDlg, WM_SETICON, ICON_SMALL,
             Cast( Dword, LoadImage (GetModuleHandle (NULL), "My_Icon", _
                   IMAGE_ICON, 0, 0, LR_COPYFROMRESOURCE ) ) )

my first try was to use instead of
 .....,Cast( Dword, LoadImage (GetModuleHandle (NULL), "My_Icon",   ...... ))
 --> :
 ................ .,LoadImage (GetModuleHandle (NULL), "My_Icon",   ...... ))
--->
which worked

here my 'PATCH'
' //    Menu_icon.inc  -- can be included after
 ' //    Callback Line 'CASE WM_INITDIALOG' and before return
#ifdef __FB_64BIT__
   '' ---(1)--- Icon in Title Bar
   '' with ICON - LOADFROMFILE
   '' ...instructions for 64bit OSes...
 MsgBox "64bit ->  SET_ICON in Title Bar  with LOADFROMFILE",48,"Message@64bit - adapted" 
 SendMessage (hDlg, WM_SETICON, ICON_SMALL, _  'Cast( Dword,_'
     (LoadImage (GetModuleHandle (NULL), "MyIcon.ICO", _
               IMAGE_ICON, 0, 0, LR_LOADFROMFILE ) ) )           
#else
  '' ...instructions for 32bit OSes...
 MsgBox "32bit -> SET_ICON in Title Bar  with LOADFROMFILE",64,"Message@32bit - original"
   SendMessage (hDlg, WM_SETICON, ICON_SMALL, Cast( Dword, _
        LoadImage (GetModuleHandle (NULL), "MyIcon.ICO", _
                IMAGE_ICON, 0, 0, LR_LOADFROMFILE ) ) )       
      ' // #endif
      ' //

or the resource method


#ifdef __FB_64BIT__
  '' ---(2)--- Icon in Title Bar ....
  '' or with COPYFROMRESOURCE File
  '' i.e. Ressource.rc with (My_ICON) and/or <->(FB_PROGRAM_ICON)
  '' MY_ICON ICON ".\Resources\MyIcon.ico"
  '' ...instructions for 64bit OSes...
 MsgBox "64bit->SET_ICON in Title Bar with COPYFROMRESOURCE",48,"Message@64bit-adapted" 
 dim hicons  as HICON
 hicons =(LoadImage (GetModuleHandle (NULL), "My_Icon", _
              IMAGE_ICON, 0, 0, LR_COPYFROMRESOURCE ) )
 ''     
 SendMessage (hDlg, WM_SETICON, ICON_SMALL,_ ' Cast( Dword, _'
             val(str(LoadImage (GetModuleHandle (NULL), "My_Icon", _
                  IMAGE_ICON, 0, 0, LR_COPYFROMRESOURCE ) ) ))
#else
 '' ...instructions for 32bit OSes...
 MsgBox "32bit->SET_ICON in Title Bar with COPYFROMRESOURCE",64,"Message@32bit-original"
      SendMessage (hDlg, WM_SETICON, ICON_SMALL, Cast( Dword, _
                LoadImage (GetModuleHandle (NULL), "My_Icon", _
                IMAGE_ICON, 0, 0, LR_COPYFROMRESOURCE ) ) )
#endif

José Roca

Your casting is wrong. You should use CAST(LPARAM, ...), not CAST(DWORD...).

' =====================================================================================
' Sets the big and small icons of the dialog.
' =====================================================================================
PRIVATE SUB DialogSetIconEx (BYVAL hDlg AS HWND, BYVAL hIconBig AS HICON, BYVAL hIconSmall AS HICON)
   SendMessageW(hDlg, WM_SETICON, ICON_BIG, cast(LPARAM, hIconBig))
   SendMessageW(hDlg, WM_SETICON, ICON_SMALL, cast(LPARAM, hIconSmall))
END SUB
' =====================================================================================

José Roca

You can also use DialogSetIcon (hDlg, wszImage), that replicates the DDT statement DIALOG SET ICON. It calls this method:

' ========================================================================================
' Changes both the dialog icon in the caption, and the icon shown in the ALT+TAB task list.
' The old image handle is released.
' ========================================================================================
PRIVATE SUB CDialog.DialogSetIcon (BYREF wszImage AS WSTRING)
   ' // Loads the icon from resource
   DIM hImage AS HANDLE, wID AS WORD, dwID AS DWORD
   IF LEFT(wszImage, 1) = "#" THEN
      wID = VAL(MID(wszImage, 2))
      dwID = MAKELONG(wID, 0)
      hImage = LoadImageW(m_hInstance, MAKEINTRESOURCEW(dwID), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)
   ELSE
      ' // Loads the icon from file
      IF UCASE(RIGHT(wszImage, 4)) = ".ICO" THEN
         hImage = LoadImageW(NULL, wszImage, IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
      ELSE
         ' // Loads the icon from resource
         hImage = LoadImageW(m_hInstance, wszImage, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)
      END IF
   END IF
   ' // Sets the images and deletes the old ones
   IF hImage THEN DeleteObject(CAST(HGDIOBJ, SendMessageW(m_hDlg, WM_SETICON, ICON_BIG, CAST(LPARAM, hImage))))
   IF hImage THEN DeleteObject(CAST(HGDIOBJ, SendMessageW(m_hDlg, WM_SETICON, ICON_SMALL, CAST(LPARAM, hImage))))
END SUB
' =====================================================================================

wszImage parameter:

As stated in the PowerBasic documentation: A string expression which specifies the name of the icon in the resource file. If the icon resource uses an integral identifier, newicon$ should begin with a Number symbol (#), followed by the integral identifier in ASCII format. For example, "#998". Otherwise, the text identifier name should be used.


Note: If you use the proper casting, you don't need to have different code for 32 and 64 bit.


hajubu


José Roca

In DDT.inc you will find replicates of the DDT statements. They use the same names that the DDT statements, but without spaces, and are functions or procedures, instead of statements. Exact syntax can't be replicated. Anyway, I prefer to use functions that statements.

José Roca

The trick to make the same code to work with 32 and 64 bit is casting. Therefore, don't use LONG or DWORD when casting the WPARAM or LPARAM parameters of SendMessage, but cast(LPARAM...).

LPARAM is defines AS LONG_PTR, which evaluates as LONG in 32 bit and as LONGINT in 64 bit.

The best way to know which data type or alias you have to use in the CAST, refer to the MSDN documentation for C++ and use the same data types than for the C++ declares, e g. HWND(window handle), HICON (Icon handle), and so on.