GunnerInc's Profile
Reputation: 719
Enlightened
- Group:
- Moderators
- Active Posts:
- 2,076 (2.65 per day)
- Joined:
- 28-March 11
- Profile Views:
- 24,289
- Last Active:
Today, 09:08 PM- Currently:
- Offline
Previous Fields
- Country:
- US
- OS Preference:
- Windows
- Favorite Browser:
- Opera
- Favorite Processor:
- AMD
- Favorite Gaming Platform:
- Classic
- Your Car:
- Who Cares
- Dream Kudos:
- 1425
- Expert In:
- Assembly
Latest Visitors
-
modi123_1 
16 May 2013 - 08:27 -
CY5 
15 May 2013 - 21:38 -
jjl 
12 May 2013 - 19:43 -
yny 
11 May 2013 - 20:36 -
BenignDesign 
06 May 2013 - 20:08 -
Narek Babadja... 
04 May 2013 - 04:42 -
Gungnir 
02 May 2013 - 05:21 -
Atli 
30 Apr 2013 - 05:11 -
Reclaimer78 
23 Apr 2013 - 21:33 -
xSouthpaw 
23 Apr 2013 - 14:59
Posts I've Made
-
In Topic: write data from textbox to txt file
Posted 18 May 2013
There is a lot wrong here, and some things can be changed to make the code "tidier".
First, you do not need to repeat the strings for the class names, this:
EditClassName db "edit", 0 EditClassName1 db "edit", 0 EditClassName2 db "edit", 0 ButtonClassName db "button", 0 ButtonClassName1 db "button", 0 StaticClassName db "static", 0 StaticClassName1 db "static", 0 StaticClassName2 db "static", 0
can be rewritten as:
szWndClsEdit db "edit", 0 szWndClsBtn db "button", 0 szWndClsLbl db "static", 0
Now, everyplace you create an Edit control, you use szWndClsEdit, szWndClsBtn for all buttons, szWndClsLbl for all Static controls. The way you are doing it is unnecessary and adds 31 extra bytes to your exe.
Second, you define a WinMain procedure, and use the passed parameter - hInst once and use a global variable - hInstance. Since we need that global variable for the life of the program, we can change WinMain since passing parameters is not needed.
WinMain proc hInst:HINSTANCE, hPrevInst:HINSTANCE, CmdLine:LPSTR, CmdShow:DWORD
So, we can change the WinMain proc, to say StartUp.
invoke GetModuleHandle, NULL mov hInstance, eax call StartUp invoke ExitProcess, eax StartUp proc local wc:WNDCLASSEX local msg:MSG ;# local hwnd:HWND ; <------ Not needed mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, offset WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInstance pop wc.hInstance mov wc.hbrBackground, COLOR_APPWORKSPACE+2 mov wc.lpszMenuName, NULL mov wc.lpszClassName, offset ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_WAIT ; <----- Shouldn't this be IDC_ARROW?? mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx,0, addr ClassName, addr AppName, WS_SIZEBOX or WS_VISIBLE, 230, 230, 230, 230, NULL, NULL, hInstance, NULL ;# mov hwnd, eax ; <------ Not needed .while TRUE invoke GetMessage, addr msg, NULL, 0, 0 .break .if (!eax) invoke TranslateMessage, addr msg invoke DispatchMessage, addr msg .endw mov eax, msg.wParam ret StartUp endp
Now, onto the WndProc. I have a wide monitor and your CreateWindowEx code, runs off my screen, we can reformat all those CreateWindowEx calls from this:
invoke CreateWindowEx, NULL, addr StaticClassName, addr StatText, WS_CHILD or WS_VISIBLE, 10, 10, 100, 20, hWnd, StaticID, hInstance, NULL
to this:
invoke CreateWindowEx, \ NULL, \ addr StaticClassName, \ addr StatText, \ WS_CHILD or WS_VISIBLE, \ 10, 10, \ 100, 20, \ hWnd, StaticID, \ hInstance, NULL
Notice the line continuation character at the end of each line - "\"
You create 2 buttons, but assign the handle of the second button to both vars hButton and hButton1. You need to save the returned handle after each call to Createwindow.
This is not correct:
invoke CreateWindowEx, NULL, addr ButtonClassName, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 10, 130, 80, 20, hWnd, ButtonID, hInstance, NULL invoke CreateWindowEx, NULL, addr ButtonClassName1, addr ButtonText1, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 110, 130, 80, 20, hWnd, ButtonID1, hInstance, NULL mov hButton, eax mov hButton1, eax
This is correct:
invoke CreateWindowEx, NULL, addr ButtonClassName, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 10, 130, 80, 20, hWnd, ButtonID, hInstance, NULL mov hButton, eax invoke CreateWindowEx, NULL, addr ButtonClassName1, addr ButtonText1, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 110, 130, 80, 20, hWnd, ButtonID1, hInstance, NULL mov hButton1, eax
Same for your Edit controls, not correct:
invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE,110, 10, 100, 20,hWnd, EditID, hInstance, NULL invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName1, NULL, WS_CHILD or WS_VISIBLE,110, 50, 100, 20,hWnd, EditID1, hInstance, NULL invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName2, NULL, WS_CHILD or WS_VISIBLE,110, 90, 100, 20,hWnd, EditID2, hInstance, NULL mov hEdit, eax mov hEdit1, eax mov hEdit2, eax
Correct:
invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName, NULL, WS_CHILD or WS_VISIBLE,110, 10, 100, 20,hWnd, EditID, hInstance, NULL mov hEdit, eax invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName1, NULL, WS_CHILD or WS_VISIBLE,110, 50, 100, 20,hWnd, EditID1, hInstance, NULL mov hEdit1, eax invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr EditClassName2, NULL, WS_CHILD or WS_VISIBLE,110, 90, 100, 20,hWnd, EditID2, hInstance, NULL mov hEdit2, eax
Constants/Equates/Defines are normally all Uppercase, so when looking at code, you can tell right away those are equates:
EditID2 can be changed to EDIT_ADDRESS, EditID1 > EDIT_AGE, EditID > EDIT_NAME. Notice how I use names that make sense, you can look at the code and know what it is for. Same goes for your variables that hold the hWnds - hEdit > hEditName, hEdit1 > hEditAge, hEdit2 > hEditAddress
The "+" operator is NOT the same as OR'ing!!!!
invoke MessageBox, NULL, addr p, addr o, MB_ICONINFORMATION + MB_YESNO
Should be:
invoke MessageBox, NULL, addr p, addr o, MB_ICONINFORMATION OR MB_YESNO
On code readability, know when to use ADDR and OFFSET; use ADDR for all LOCAL variables, and use OFFSET for all variables defined in the .data and .data? sections. It helps make code self documenting, so when you or someone else looks at your code, they can tell what variable is local to the procedure, and which are global.
Not really sure what you are trying to do when a button is clicked, maybe save Name, Age, and Address to file? Before we get to that, let's clean up/change your WM_COMMAND handler:
.elseif eax == WM_COMMAND mov edx,wParam movzx eax,dx shr edx,16 .if edx == BN_CLICKED .if eax == ButtonID ; Ok clicked .else ; Exit clicked .endif .endif
Now your code for opening, and writing to a file is really whacked out.
First, it not good to hard code a path to a file, instead use the open file dialog to get the path and name for the file - GetOpenFileName
It will be easier for me to show how I would do it and comment my code. But first a few errors:
invoke CreateFile, addr efren, GENERIC_READ or GENERIC_WRITE,0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL mov hEdit,eax
You just overwritten the handle for your edit control with the file handle, shouldn't that be mov hFile, eax??
invoke WriteFile,hFile,addr efren,FileSize,ADDR BytesRead,0
mov hFile,eax
WriteFile, returns status codes, you just overwritten the handle to the file with a status code.
mov hFile,eax is not needed.
Here is how I would do it: Your code is still in there:
include masm32rt.inc EditControlsStruct struc hEdtName DWORD ? hEdtAge DWORD ? hEdtAddy DWORD ? EditControlsStruct ends .data dwBytesWritten dw "0", 0 BytesToWrite dw "30", 0 ClassName db "WinClass", 0 AppName db "FINAL_PROJECT", 0 szWndClsEdit db "edit", 0 szWndClsBtn db "button", 0 szWndClsStc db "static", 0 Msg db "You Clicked the Button!", 0 ButtonText db "OK", 0 ButtonText1 db "CANCEL", 0 p db "PLEASE CONFIRM EXIT ?", 0 o db "PROMPT", 0 StatText db "NAME:", 0 StatText1 db "AGE:", 0 StatText2 db "ADDRESS:", 0 efren db "C:\Users\Lau\Desktop\ASS\nn\DATA.txt", 0 szCRLF db 13, 10, 0 szFilePath db "D:\Projects\Dream In Code User Code\olracselanreb\EditControlToFile\Data.txt", 0 .data? EditConrols EditControlsStruct <?> hInstance HINSTANCE ? hEdit HWND ? hButton1 HWND ? hEdit1 HWND ? hEdit2 HWND ? hButton HWND ? hStatic HWND ? hStatic1 HWND ? hStatic2 HWND ? hFile dd ? FileSize dd ? hMem dd ? BytesRead dd ? hHeap dd ? dwTextLen dd ? .const EditID equ 12345 ButtonID equ 67890 EditID1 equ 1 ButtonID1 equ 6 EditID2 equ 2 StaticID equ 123 StaticID1 equ 456 StaticID2 equ 789 MEMORYSIZE equ 65535 .code start: invoke GetModuleHandle, NULL mov hInstance, eax invoke GetProcessHeap mov hHeap, eax call StartUp invoke ExitProcess, eax StartUp proc local wc:WNDCLASSEX local msg:MSG ;local hwnd:HWND ; <------ Not needed mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, offset WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInstance pop wc.hInstance mov wc.hbrBackground, COLOR_APPWORKSPACE+2 mov wc.lpszMenuName, NULL mov wc.lpszClassName, offset ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_WAIT ; <----- Shouldn't this be IDC_ARROW?? mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx,0, addr ClassName, addr AppName, WS_SIZEBOX or WS_VISIBLE, 230, 230, 230, 230, NULL, NULL, hInstance, NULL ;~ mov hwnd, eax ; <------ Not needed .while TRUE invoke GetMessage, addr msg, NULL, 0, 0 .break .if (!eax) invoke TranslateMessage, addr msg invoke DispatchMessage, addr msg .endw mov eax, msg.wParam ret StartUp endp WndProc proc uses esi edi ebx hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM .if uMsg == WM_DESTROY invoke PostQuitMessage, 0 .elseif uMsg == WM_CREATE invoke CreateWindowEx, \ NULL, \ addr szWndClsStc, \ addr StatText, \ WS_CHILD or WS_VISIBLE, \ 10, 10, \ 100, 20, \ hWnd, StaticID, \ hInstance, NULL invoke CreateWindowEx, NULL, addr szWndClsStc, addr StatText1, WS_CHILD or WS_VISIBLE, 10, 50, 100, 20, hWnd, StaticID, hInstance, NULL invoke CreateWindowEx, NULL, addr szWndClsStc, addr StatText2, WS_CHILD or WS_VISIBLE, 10, 90, 100, 20, hWnd, StaticID, hInstance, NULL invoke CreateWindowEx, NULL, addr szWndClsBtn, addr ButtonText, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 10, 130, 80, 20, hWnd, ButtonID, hInstance, NULL mov hButton, eax invoke CreateWindowEx, NULL, addr szWndClsBtn, addr ButtonText1, WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, 110, 130, 80, 20, hWnd, ButtonID1, hInstance, NULL mov hButton1, eax invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr szWndClsEdit, NULL, WS_CHILD or WS_VISIBLE,110, 10, 100, 20,hWnd, EditID, hInstance, NULL mov EditConrols.hEdtName , eax invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr szWndClsEdit, NULL, WS_CHILD or WS_VISIBLE,110, 50, 100, 20,hWnd, EditID1, hInstance, NULL mov EditConrols.hEdtAge, eax invoke CreateWindowEx, WS_EX_CLIENTEDGE, addr szWndClsEdit, NULL, WS_CHILD or WS_VISIBLE,110, 90, 100, 20,hWnd, EditID2, hInstance, NULL mov EditConrols.hEdtAddy, eax .elseif uMsg == WM_COMMAND mov edx,wParam movzx eax,dx shr edx,16 .if edx == BN_CLICKED .if ax == ButtonID ; Ok clicked ; Use GetOpenFileName here ; invoke CreateFile, \ offset szFilePath, \ GENERIC_WRITE, \ NULL, \ NULL, \ OPEN_ALWAYS, \ FILE_ATTRIBUTE_NORMAL, \ NULL .if eax == INVALID_HANDLE_VALUE ; Uh oh, could not open or create file!!! ; inform user here jmp COMMAND_DONE .endif mov hFile, eax xor ebx, ebx xor edi, edi lea esi, EditConrols ; Get text length of all controls .while ebx <= sizeof EditConrols / 4 - 1 invoke SendMessage, [esi + 4 * ebx], WM_GETTEXTLENGTH, 0, 0 add edi, eax inc ebx .endw add edi, 7 ; space for 3 CRLF pairs and NULL terminator invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, edi mov edi, eax ; Get text from all controls and append to buffer xor ebx, ebx lea esi, EditConrols ; Get text length of all controls .while ebx <= sizeof EditConrols / 4 - 1 invoke SendMessage, [esi + 4 * ebx], WM_GETTEXTLENGTH, 0, 0 inc eax invoke SendMessage, [esi + 4 * ebx], WM_GETTEXT, eax, edi invoke szCatStr, edi, offset szCRLF ; add crlf to text invoke szLen, edi ; len of test push eax invoke SetFilePointer, hFile, NULL, NULL, FILE_END ; move file pointer to end of file pop eax invoke WriteFile, hFile, edi, eax, offset BytesRead, 0 ; write text to file inc ebx .endw invoke WriteFile, hFile, offset szCRLF, 2, offset BytesRead,0 ; write crlf to file to seperate entries invoke CloseHandle, hFile invoke HeapFree, hHeap, 0, edi .else ; Exit clicked invoke MessageBox, NULL, addr p, addr o, MB_ICONINFORMATION or MB_YESNO .if eax == IDYES invoke SendMessage, hWnd, WM_DESTROY, NULL, NULL .endif .endif COMMAND_DONE: .endif .else invoke DefWindowProc, hWnd, uMsg, wParam, lParam ret .endif xor eax, eax ret WndProc endp end start
I entered info twice, and this is the result in data.txt
Quote
Me
21
Here
You
25
There -
In Topic: write data from textbox to txt file
Posted 18 May 2013
Moved to Assembly, and added code tags. -
In Topic: Linux script skipping function (I think)
Posted 17 May 2013
Not too sure, but following the sample directions:
ls > crocodile.foo echo bark > dingo.bar ./chExt.sh dat crocodile.foo bogusName.foo dingo.bar
Using your supplied code, all works:
TestScript.png (54.19K)
Number of downloads: 3 -
In Topic: suggested framework to program text-based-games ( like mafia wars, tra
Posted 17 May 2013
please don't open duplicate topics.
closed -
In Topic: Finding the minium value of an array
Posted 16 May 2013
No, do some research on your own - look up Int 21/AH=4Ch since you are using 16bit code - http://ctyme.com/rbrown.htm
32 bit apps, use ExitProcess - that can be found on MSDN. Don't think you will be hitting *nix any time soon.
If you are going to access vars in the data section, then you must set up the segment registers, ds is the Data Segment register.
And I will say again, RET is NOT the proper way to exit a program, smack the teacher that is teaching that!!!
My Information
- Member Title:
- "Hurry up and wait"
- Age:
- 38 years old
- Birthday:
- June 30, 1974
- Gender:
-
- Location:
- In my head
- Interests:
- Assembly
- Forum Leader:
- Assembly
- Full Name:
- Rob
- Years Programming:
- 25
- Programming Languages:
- x86 Win32 Assembly (MASM, FASM, NASM)
Contact Information
- E-mail:
- Click here to e-mail me
- Website URL:
-
http://www.gunnerinc.com
- Skype:
-
gunner.inc
- Facebook:
- http://www.facebook.com/gunnerinc
|
|


Find Topics
Find Posts
View Reputation Given

|
Comments
BenignDesign
06 May 2013 - 06:44BenignDesign
02 Nov 2012 - 20:34codeprada
25 Jun 2012 - 07:11GunnerInc
26 May 2012 - 18:21no2pencil
26 May 2012 - 18:12DimitriV
20 Nov 2011 - 19:22ishkabible
17 Oct 2011 - 13:33Gungnir
17 Oct 2011 - 06:42ishkabible
12 Oct 2011 - 18:36GunnerInc
08 Oct 2011 - 08:04ishkabible
07 Oct 2011 - 21:05