TIP: Click on subject to list as thread! ANSI
echo: win32
to: ALL
from: `Herbert Menke`
date: 1997-11-17 00:00:00
subject: Re: WIN32 CALLBACK problems - PLEASE HELP

From: "Herbert Menke" 
Subject: Re: WIN32 CALLBACK problems    -   PLEASE HELP
Date: 1997/11/17
Message-ID: #1/1
References: 
Organization: NDH Netzwerkdienste Hoeger GmbH
Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.win32,fido.win32,microsoft.public.win32,microsoft.public.win32.programmer,microsoft.public.win32.programmer.kernel



>Please e-mail responses to:     puppy@dog.astra.co.uk
>your help would be greatly appreciated
>If you need help- just ask!!
>
>
>Here's the problem:
>
>I want to encapsulate my WIN32 application in a class "DbApp"
>However when I try to assign my "WndProc" function to the
>WindowsClass.lpfnWndProc variable for WindowsClass registration I get the
>ERROR:
>
>"Cannot convert 'long (__stdcall DbApp::*)(void *,unsigned int,unsigned
>int,long)' to 'long (__stdcall *)(void *,unsigned int,unsigned int,long)'"
>
>Notice the (__stdcall DbApp::*) to (__stdcall *) difference!
>I don't understand what I need to do to allow the Windows-API to access my
>"class member functions" without it being a global function,
>I tried many different combinations of pointer declarations with no avail
>PLEAS HELP!


This is quite easy do:

for windows you can get/set the pointer to your class with
Set/GetWindowLong( hWnd, GWL_USERDATA );


  MyWndClass wndClass;
  // Register and Create Window

  RegisterMyClass( hInstance );
  g_hWnd = CreateWindow( "MyClass", "Caption", MY_STYLE, 1, 1, 200, 200,
NULL, NULL, hInstance, (LPVOID)(MyWndClass*)&wndClass);

for DialogWindows you can get/set the pointer to your class with
Set/GetWindowLong( hWnd, DWL_USER );
  MyDialogClass dialogClass;
  // init Class
  // and call Dialog
  DialogBoxParam( hInstance, "DialogTemplate, hWnd, _MyDialogProc,
(LPARAM)(MyDialogClass *)&dialogClass );

Another method to save the class pointer at a window are the SetProp,
GetProp and RemoveProp functions, which stick a user 32bit value to a
window.

I hope this helps
merbert menke



class MyWndClass
{
  public:
  HWND _hWnd;
  LRESULT HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
class MyDialogClass
{
  public:
  HWND _hWnd;
  BOOL HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
LRESULT CALLBACK _MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam)
{
  MyWndClass * pClass;
  if( msg == WM_NCCREATE )
  {
    SetWindowLong( hwnd, GWL_USERDATA,
(LONG)((LPCREATESTRUCT)lParam)->lpCreateParams );
  }
  else
  {
    pClass = (MyWndClass*)GetWindowLong(hwnd, GWL_USERDATA);
    if( pClass )
    {
      LRESULT result = pClass->HandleMessage( hwnd, msg, wParam, lParam );
      if( result )
        return result;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
};
BOOL RegisterMyClass(HINSTANCE hinst)
{
  WNDCLASS cls;
  // set other parameters
  cls.lpfnWndProc     = _MyWindowProc;
  return RegisterClass(&cls);
}
BOOL CALLBACK _MyDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam)
{
  MyDialogClass * pClass;

  if( msg == WM_INITDIALOG )
  {
    SetWindowLong( hwnd, DWL_USER, (LONG)lParam );
    if( lParam )
      return ((MyDialogClass*) lParam)->HandleMessage( hwnd, msg, wParam,
lParam );
  }
  else
  {
    pClass = (MyDialogClass*)GetWindowLong(hwnd, DWL_USER);
    if( pClass )
    {
      BOOL result = pClass->HandleMessage( hwnd, msg, wParam, lParam );
      if( result )
        return result;
    }
  }
  return FALSE;
};

SOURCE: echoes via archive.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.