Binding win32 window to a D class

freeagle freeagle at inmail.sk
Sat Sep 9 04:51:57 PDT 2006


freeagle wrote:
> Serg Kovrov wrote:
>> Hello All,
>>
>> This one should be beaten to death already, but still I do not aware 
>> of fast/elegant way.
>>
>> Maybe we could collect all possible solutions on that matter for D in 
>> one place. Starting in this thread and hopefully move to a wiki page.
>>
>> As I understand for D there even less available techniques then for 
>> C++. In case of C++ I am aware of following:
>>
>> 1. using static method as window procedure and store pointer to class 
>> instance in with SetWindowLongPtr. when dispatching messages retrieve 
>> pointer to class with GetWindowLongPtr and call class method. This 
>> can't be used in D since our pointer might become invalid as GC could 
>> swap / compact memory. This is covered in "Garbage Collection" article 
>> - http://digitalmars.com/d/garbage.html
>>
>> 2. Same as above, but stores class reference in static array/hash and 
>> used SetWindowLongPtr/GetWindowLongPtr to store index to retrieve 
>> class from that static array. This actually works for D. But it is 
>> most slow technique since there we need to call GetWindowLongPtr and 
>> lookup for class pointer/reference on every windows message.
>>
>> 3. So called thunking. A hack to store pointer to class method in 
>> WNDCLASS.lpfnWndProc. Uses inline assembler substitute addresses at 
>> runtime. Should be faster than other techniques since there is no need 
>> to constantly call GetWindowLongPtr. Used in ATL/WTL. Could not be 
>> used in D for same reason as first solution.
>>
>> Thats seems all. The only available solution (from listed above) for D 
>> is #2.
>>
>> I hope there is other methods to solve this problem, and perhaps there 
>> are completely different approaches to organize win32 window 
>> management, and I'd like to discuss them here.
>>
>> Thanks
>> -- 
>> serg.
> 
> the second approach, without the need for get/setPtr:
> 
> class Windows
> {
>     static Window[HWND] _windows;
>     .
>     .
>     .
>     int windowProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
>     {
>         .
>         .
>         .
>     }
> }
> 
> extern(Windows)
> int
> wndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
> {
>     Window* wnd = hwnd in Window._windows;
>     if(wnd !is null)
>     {
>         return wnd.windowProc(hwnd, message, wParam, lParam);
>     }
>     else
>     {
>         return DefWindowProc(hwnd, message, wParam, lParam);
>     }
> }

sry,

class Window, not Windows, typo



More information about the Digitalmars-d-dwt mailing list