🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13957

Re: Windows wierdness (SendMessage, Win98, Win2000) repeat

#13957
From: David
Date: Sun, 31 Aug 2003 03:01
121 lines
5060 bytes
Keith wrote:
>

...
> Yes sir, here's why:
> 
> void WinComboBox::add(char * str) {
>    SendMessage(hwndC, CB_ADDSTRING, 0, (LPARAM) str);
> }
> 
> In the call to SendMessage, you are actually getting a call to SendMessageA.  The four
> parameters are not being removed from the stack prior to returning to the caller at line:
> 
> cBoxC.add("A");  <<-----------this line produces the call (of course)
> 
> and here is the .add method code:
> 
> push ebp
> mov ebp,esp    ; standard function entry to establish addressibility of stack params.
> 
> push dword ptr[ebp+0x0c]   ;  push lparam str
> push 0            ; push your 3rd param
> push 0x143    ; push CB_ADDSTRING
> mov eax,[ebp+0x08] ; obtain address to owner of hwndC
> push dword ptr [eax+0x2c] ; push hwndC at offset 2c into owner of hwndC
> call USER32.SendMessageA
> 
> ; note that no parameters are removed from stack at this point
> ; also note that USER32.SendMessageA did NOT remove the parameters from the stack
> 
> pop ebp ; restore entry point addressibility ; (caused the stack frame reference to be lost)
> ret ; return to caller (return to incorrect address)

...


If that is the case then the following console program should also crash in Win98, yet it 
doesn't.  Also, the code posted should crash in 2000 and ME, yet the code has always been 
stable on those platforms.  Can you explain some more?

Thanks,
David


#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HINSTANCE hInst;
HWND comboBox;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE , PSTR , int show) {
    hInst = hInstance;
    static TCHAR appName[] = TEXT ("MinApplication") ;
    HWND         hwnd ;
    MSG          msg ;
    WNDCLASS     wndclass ;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = appName ;

    if (!RegisterClass (&wndclass)) {
       MessageBox (NULL, TEXT ("Oh no"), appName, MB_ICONERROR) ;
       return 0 ;
       }

    hwnd = CreateWindowEx(WS_EX_APPWINDOW, appName, TEXT ("MinApplication"),
                WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_OVERLAPPEDWINDOW | WS_BORDER,
                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                NULL, NULL, hInstance, NULL);

    HMENU menu = GetSystemMenu(hwnd,FALSE);
    DeleteMenu (menu, SC_MAXIMIZE, MF_BYCOMMAND);
    DeleteMenu (menu, SC_MINIMIZE, MF_BYCOMMAND);

    DWORD style = GetWindowLong(hwnd, GWL_STYLE);
    SetWindowLong(hwnd, GWL_STYLE, style & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX	 & ~WS_MINIMIZEBOX);

    MoveWindow(hwnd, 20, 20, 300, 100, TRUE);

    ShowWindow (hwnd, show) ;
    UpdateWindow (hwnd) ;

    while (GetMessage (&msg, NULL, 0, 0)) {
       TranslateMessage (&msg) ;
       DispatchMessage (&msg) ;
       }
    return msg.wParam ;
}


void add(char * str) {  //Examining asm code shows same output as Keith's code
    SendMessage(comboBox, CB_ADDSTRING, 0, (LPARAM) str);
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
       case WM_CREATE:
          comboBox = CreateWindow("COMBOBOX", " ",
                      WS_CHILD | WS_TABSTOP | ES_LEFT | CBS_DROPDOWNLIST | WS_VSCROLL,
                      22, 20, 250, 100,
                      hwnd, (HMENU)5, hInst, NULL);
          ShowWindow(comboBox, SW_SHOW);
          add("Test");
          return 0;

       case WM_DESTROY :
          PostQuitMessage (0) ;
          return 0 ;

       }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Technical Details
Message-ID: <3F5164FE.8000803@anonymous_one.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3F50A23C.1090706@anonymous_one.com> <3F50C002.901B2CC9@sbcglobal.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,d5c2d7b43b5c1d37
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-08-30 20:01:52 PST
Message-ID: <3F5164FE.8000803@anonymous_one.com>
Date: Sat, 30 Aug 2003 22:01:18 -0500
From: David <david@anonymous_one.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Windows wierdness (SendMessage, Win98, Win2000) repeat
References: <3F50A23C.1090706@anonymous_one.com> <3F50C002.901B2CC9@sbcglobal.net>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 67.30.0.213
X-Trace: newsgroups.borland.com 1062298748 67.30.0.213 (30 Aug 2003 19:59:08 -0700)
Lines: 122
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail