0

Please bare with my knowledge I am new to assembly language.

I have a code here that add the value of the two textbox and display the result on the third one when I hit the button.

I tried to construct argument using the GetWindowText command but it didn't display anything sometimes it crashes, I look into the web with the same program I am working, but I only found this one From stackoverflow the difference is he use the GetDlgItemTextas I read on the Microsoft Website it retrieves the title or text associated with a control in a dialog box, but I am not using a dialog box so maybe I will just stick to the GetWindowText function.

Here is the code I made, but to be honest I dunno what is going on here I just made it up because I have no idea how to construct an argument for GetWindowText function.

Some of the code you are seeing there is a recycled code from the program that I work a few days ago which is a simple addition operation, which is when I input two values in a console it add the number and display the result. Now, I am trying to do it again but with the use of textbox and button but I can't get it right.

The Deceleration:

.data?

    EditIn1ID db 10 dup(?)
    EditIn2ID db 10 dup(?)
    EditOutID db 10 dup(?)


    hButton HWND ?
    hEditIn1 HWND ?
    hEditIn2 HWND ?
    hEditOut HWND ?

The Textbox and the Button function

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    .if uMsg == WM_DESTROY
        invoke PostQuitMessage, 0

    .elseif uMsg == WM_CREATE


        invoke CreateWindowEx, NULL, addr ButtonClassName, addr ButtonAdd, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 225, 10, 120, 30, hWnd, ButtonID, hInstance, NULL
        mov hButton, eax

        invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE, 10, 10, 120, 30, hWnd, EditIn1ID, hInstance, NULL
        mov DWORD PTR [hEditIn1], eax
        invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE, 10, 50, 120, 30, hWnd, EditIn2ID, hInstance, NULL
        mov DWORD PTR [hEditIn2], eax
        invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE, 10, 110, 120, 30, hWnd, EditOutID, hInstance, NULL
        mov DWORD PTR [hEditOut], eax


    .elseif uMsg == WM_COMMAND   
        mov eax, wParam
            .if eax == ButtonID
                 shr eax, 16
                .if ax == BN_CLICKED

               invoke GetWindowText,EditIn1ID, eax, 10
               invoke atodw, addr EditIn1ID
               mov ebx, eax 

               invoke GetWindowText,EditIn2ID, ebx, 10
               invoke atodw, addr EditIn2ID
               add ebx, eax

               invoke dwtoa, ebx, addr res
               invoke SetWindowText,EditOutID, addr res


                    .endif
            .endif

Here's What I think of this line of code:

invoke GetWindowText,EditIn1ID, eax, 10

in invoke GetWindowText this line of code calls the function GetWindowText which copies the input value.

EditIn1ID This code is the unique ID for my textbox just so that the button has this specific value.

eax is for calculation operation, maybe it will be use in addition operation.

Some also use esi and edi but I never encounter this code I dunno if they are slightly the same with eax and ebx, I googled the meaning of it but I am skeptical if I can use it here in my code so, I refrain from using it.

Pls. advice me, thanks.

Community
  • 1
  • 1

1 Answers1

0

I see a number of problems in your code:

invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE, 10, 10, 120, 30, hWnd, EditIn1ID, hInstance, NULL

From MSDN:

HWND WINAPI CreateWindowEx(
  _In_     DWORD     dwExStyle,
  _In_opt_ LPCTSTR   lpClassName,
  _In_opt_ LPCTSTR   lpWindowName,
  _In_     DWORD     dwStyle,
  _In_     int       x,
  _In_     int       y,
  _In_     int       nWidth,
  _In_     int       nHeight,
  _In_opt_ HWND      hWndParent,
  _In_opt_ HMENU     hMenu,
  _In_opt_ HINSTANCE hInstance,
  _In_opt_ LPVOID    lpParam
);

For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

Based on the code you've shown in your question, the IDs are uninitialized at that point. What you should be passing as the ID is a 32-bit value that is unique for each child window that you create.

Something like:

EditIn1ID dd 1234
EditIn2ID dd 1235
EditOutID dd 1236

and then you need to add one or more new variables to hold the string(s) that you read.


invoke GetWindowText,EditIn1ID, eax, 10

The first argument for GetWindowText is supposed to be a HWND, and the second should be a pointer to the buffer that the text should be written to. EditIn1ID is not a HWND (the HWND is the one you stored in hEditIn1), and eax does not point to any buffer here as far as I can tell.


invoke atodw, addr EditIn1ID

atodw takes the address of a string as its argument. Again, EditIn1ID is supposed to be a child window ID, not a string.


I am not using a dialog box so maybe I will just stick to the GetWindowText function.

From the documentation for GetDlgItem:

You can use the GetDlgItem function with any parent-child window pair, not just with dialog boxes. As long as the hDlg parameter specifies a parent window and the child window has a unique identifier (as specified by the hMenu parameter in the CreateWindow or CreateWindowEx function that created the child window),

I would expect the same to be true for GetDlgItemText. Hence you should be able to do:

invoke GetDlgItemText, hMainWindow, EditIn1ID, ADDR my_string_buffer, 10
Michael
  • 57,169
  • 9
  • 80
  • 125