Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
this continues the question "help me CallWindowProc Exception".

--------------> first code

C#
pfnWndProc = (FN_WINDOWPROC *)SetWindowLongA(hWnd,  GWL_WNDPROC, (int)MyWindowProc);            //here SetWindowLongA

LRESULT __stdcall MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_RBUTTONDOWN:
               ......................
        }

     return CallWindowProcA((WNDPROC)pfnWndProc , hWnd, uMsg, wParam, lParam);
}

-----------------> second code

C#
pfnWndProc = (FN_WINDOWPROC *)SetWindowLongW(hWnd,  GWL_WNDPROC, (int)MyWindowProc);                 //here SetWindowLongW

LRESULT __stdcall MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_RBUTTONDOWN:
               ......................
        }

     return CallWindowProcA((WNDPROC)pfnWndProc , hWnd, uMsg, wParam, lParam);
}



two problems.

1. if it uses SetWindowLongA, i have to call CallWindowProcA not CallWindowProcW.

2. if first is right then why?

plz plz...
Posted

In addition to what Sergey already told you, the Ansi or Widechar versions (suffixed by A or W respectively) should be used consistently with the program. So unless you have real necessity to use one or another, and you are well aware of what you are doing, you should always use the general version of the function, i.e. CallWindowProc, and let the compiler choose which is appropriate.

Anyway the errors in your code are in order:

1. Use of deprecated function
SetWindowLong
. Use instead the newer
SetWindowLongPtr
.
2. You don't have to cast the function to an (int), especially if you're compiling on x64 system it will cause address violation because an int is 32 bits, while the pointer is 64. cast the value as a (LONG_PTR) as specified in the function reference[^].
3. you may wan t use the function SetWindowSubclass[^] for subclassing instead of SetWindowLongPtr.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Sep-15 12:55pm    
Good points, a 5.
—SA
Frankie-C 2-Sep-15 13:17pm    
Thanks Sergey
Your question is not quite clear, but probably you need to know this: 'W' version is the functions is the one supporting "wide-char", which is used for Unicode support, 'A' is for using legacy 8-bit char representation. If your strings can be all ASCII, you can use 'A' versions, but it's better (or, in many cases, absolutely required) to do it consistently: either all 'A' or all 'W'.

If this does not help you, please explain the problem if further detail.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900