SetClassLongW

Hey,
While evaluating Xojo, I noticed that the “SetWindowIcon.xojo_binary_project” example did not worked as expected.
The problem was the use of the API SetClassLongW() instead of the SetClassLongPtrW() .
SetClassLongW() could be used only on 32 bit code. SetClassLongPtrW() will work with both 32 and 64bit applications.
There was also some 32-64bit confusion on parameters.
I changed GetModuleHandleA to GetModuleHandleW to be more coherent with the rest of the code.
I used GetTickCount() to get kind-of random icon selection from Shell32.dll.

I wanted to send this to Xojo directly but didn’t find any mail for that purpose.
Here it is…

#If TargetWindows Then
  
  'From [Choose a Project/Examples/Platform-Specific/Windows/SetWindowIcon.xojo_binary_project]
  '[Window1/Controls/SetIconButton/Action]
  
  Soft Declare Function GetTickCount LIB "Kernel32.dll" () AS UInt32
  '>>                                      32 or 64bit Window handle  , 32bit long,     32bit long      ) AS 32bit long
  Soft Declare Function SetClassLongPtrW Lib "User32" (hwnd As Integer, index As Int32, newLong As Int32) As Int32
  'Soft Declare Function SetClassLongW Lib "User32" (hwnd As Integer, index As Integer, newLong As Integer) As Integer
  Soft Declare Function ExtractIconW Lib "Shell32" (hinstance As Integer, absPath As WString, index As Int32) As Integer
  Declare Function GetModuleHandleW Lib "Kernel32" (path As CString) As Integer
  
  // Get a handle to the current application's HINSTANCE
  Dim moduleHandle As Integer
  moduleHandle = GetModuleHandleW("")
  
  // Now, extract a random icon from Shell32.dll
  Dim hIcon As Integer
  'hIcon = ExtractIconW(moduleHandle, SpecialFolder.System.Child("Shell32.dll").NativePath, -151 ) 
  hIcon = ExtractIconW(moduleHandle, SpecialFolder.System.Child("Shell32.dll").NativePath, GetTickCount() AND 255) 
  '>> GetTickCount() AND 255 simulate a random number, there is more tnan 300 icons in Shell32.dll
  '>> The menu need some cleanup to remoove invalid entrys.
  
  If hIcon <> 0 Then
    // Set the icon for the window only if we were
    // able to pull an icon from the dll
    
    'Call SetClassLongW(Self.Handle, -14, icon)
    Call SetClassLongPtrW(Self.Handle, -34, hIcon) // -34 is GCL_HICONSM (Small)
    Call SetClassLongPtrW(Self.Handle, -14, hIcon) // -14 is GCL_HICON
    '>> Call DestroyIcon(hIcon) on WM_DESTROY
  End If
#EndIf

Welcome, Pierre, and thanks for putting some effort into that. Most, if not all, of the examples were written when Xojo was 32-bit only. 64-bit support is fairly recent. The normal way to communicate something like this to Xojo is through the Feedback application, but that may not be available to you if you haven’t purchased a license yet. You might try the customer support email, which I think is hello@xojo.com. Or perhaps one of the engineers will notice your post here, but that’s not guaranteed.

Feedback is available to everyone who has a login ID. Just enter this as a bug report against the docs.

Thanks to both of you,
I’m evaluating the product so, I do not have a license yet. In the mean while, I may try the customer support email for my comment.

What is looking good to me, is this a well and alive forum.
If I may ask an out of subject question, does this site is have a low tolerance to trolls?
With time, I became a little allergic? Thank…

We don’t usually feed trolls, and conversations generally remain very civil. This community is generally very helpful, not usually snobbish. When I began (and even recently), the community helped me out with issues that I was having. There are many excellent (I refrained from using the word “expert”) developers in participating in the forum (@Tim Hare is one of them), and even Xojo engineers (@Greg O’Lone is one of them).

The key is to do your homework, provide clear information on what you tried and to express the questions as clearly as possible. Based on the above, I would say that you won’t have a problem!

And of course, welcome to the forum!