News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Self incrementing Counter info

Started by skywalker, March 02, 2006, 12:43:49 AM

Previous topic - Next topic

PBrennick

Skywalker,
Who are you asking, please.  If you are asking Donkey, I don't want to interfere.

zcoder,
Using an include file, as you are suggesting, is probably a lot easier to do, also, and is a very good option in my opinion.  There is always more than one way to solve a problem, right?

Paul
The GeneSys Project is available from:
The Repository or My crappy website

zcoder

PBrennick,
I used that idea on a program
after I found out that XP did not have
one of the reg keys I was using in 98

so I got feed up with MS changeing the rules
and MADE MY OWN RULE. :bg

Zcoder....
Back in 1979, My computer ran so fine.
And there was no such thing,
As a Microsoft Crashed Machine.
http://zcoder.110mb.com
http://www.dietzel.com/partner/idevaffiliate.php?id=345_6  Free Domain Names

PBrennick

zcoder,
Why don't you show Skywalker how to do that and, possibly, encrypt the value, also.  :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

skywalker

Quote from: PBrennick on March 05, 2006, 02:02:09 PM
Skywalker,
Who are you asking, please.  If you are asking Donkey, I don't want to interfere.

zcoder,
Using an include file, as you are suggesting, is probably a lot easier to do, also, and is a very good option in my opinion.  There is always more than one way to solve a problem, right?

Paul

Thanks.

I was asking Donkey for help with writing to win.ini.

Anything I learn is useful to me.

Andy



IAO

Hi to all:  My English is poor. But I try. :wink

Mr. skywalker.
Look in C or D:\MASM32\Example10\GETINI
This example can help you.
I think, that GETINI can help you.
You need ServPack2.

Thanks for your patience.
Bye ('_').
"There is no way to peace. Peace is the way."    Mahatma Gandhi

zcoder

Ok, good idea.

Here is some code to work on.



.data?
CONFIGSETUP STRUCT
Window       dd ?
System       dd ?
System32     dd ?
Cookies      dd ?
Startup      dd ?
Minimized    dd ?
RegCheck     dd ?
FirstRun     dd ?
CONFIGSETUP ENDS

.data
hFile       dd 0
byteswrote  dd 0
pcs         CONFIGSETUP<0,0,0,0,0,0,0,0>




        invoke CreateFile,txt("Options.cfg"),GENERIC_READ,FILE_SHARE_READ,
                             NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        .if hFile == INVALID_HANDLE_VALUE
            invoke MessageBox,hWnd,txt("Error, Opening Options.cfg File"),txt("Create File Error!"),MB_OK
            ret
        .endif
        invoke ReadFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
        invoke CloseHandle,hFile
;*****************************************************************
; And this is how you can update the file after you have made some
; changes
;*****************************************************************

        invoke CreateFile,txt("Options.cfg"),GENERIC_WRITE,FILE_SHARE_WRITE,
                              NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        .if hFile == INVALID_HANDLE_VALUE
            invoke MessageBox,hWnd,txt("Error, Creating File"),txt("Create File Error!"),MB_OK
            ret
        .endif
        invoke WriteFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
        invoke CloseHandle,hFile



I hope that helps it is generic but you get the idea I hope.


Zcoder....
Back in 1979, My computer ran so fine.
And there was no such thing,
As a Microsoft Crashed Machine.
http://zcoder.110mb.com
http://www.dietzel.com/partner/idevaffiliate.php?id=345_6  Free Domain Names

donkey

Hi Skywalker,

The APIs are pretty much self explanatory...
invoke GetProfileIntA,"MyApp","Counter",1
inc eax ; add 1 to the counter
mov [count],eax
invoke dw2a,offset counter,[count]
invoke WriteProfileStringA,"MyApp","Counter",offset counter

If the section and key do not exist they will be created.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

skywalker

Quote from: IAO on March 05, 2006, 02:28:15 PM
Hi to all:  My English is poor. But I try. :wink

Mr. skywalker.
Look in C or D:\MASM32\Example10\GETINI
This example can help you.
I think, that GETINI can help you.
You need ServPack2.

Thanks for your patience.
Bye ('_').

I'll read thru it and study it.

I just have service pack 1. Does the code use the Security Center ?

Thanks.

Mark Jones

Here's a "Save structure to INI file" example.

http://www.masmforum.com/simple/index.php?topic=1073.0

Once I wanted a file to "remember" the last time it was used without any .ini files. So I looked in the finished .EXE for some slack space at the end of the .text section and recorded that offset. Then I had the app spawn a second app before closing, which loaded the first app and wrote a FILETIME value to the offset. Each time the original app is run, it looked for the FILETIME at that offset and read the value, then converted it to SYSTEMTIME and displayed it. Blindly modifying a PE section is not a good idea really, but it worked. "Good morning Dave... Your last login was January 13, 2006..." Sheesh I'm a dork! :toothy
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

zooba

Quote from: Mark Jones on March 05, 2006, 06:55:53 PM
Once I wanted a file to "remember" the last time it was used without any .ini files. So I looked in the finished .EXE for some slack space at the end of the .text section and recorded that offset. Then I had the app spawn a second app before closing, which loaded the first app and wrote a FILETIME value to the offset. Each time the original app is run, it looked for the FILETIME at that offset and read the value, then converted it to SYSTEMTIME and displayed it. Blindly modifying a PE section is not a good idea really, but it worked. "Good morning Dave... Your last login was January 13, 2006..." Sheesh I'm a dork! :toothy

Why didn't you just modify a value in the initialised data section (of the actual executable, obviously)? That way it's really simple to read the value back  :bg

Mark Jones

What can I say, I'm a dork. :green
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

skywalker

Quote from: zcoder on March 05, 2006, 02:28:54 PM
Ok, good idea.

Here is some code to work on.



.data?
CONFIGSETUP STRUCT
Window       dd ?
System       dd ?
System32     dd ?
Cookies      dd ?
Startup      dd ?
Minimized    dd ?
RegCheck     dd ?
FirstRun     dd ?
CONFIGSETUP ENDS



I hope that helps it is generic but you get the idea I hope.


Zcoder....


I was getting an error with these all statements using txt("Error, Opening Options.cfg File"),
so I fixed one of them by putting in a pointer to a null terminated file name.

Am I going in the right direction ?
Is the txt some macro. I have the latest masm installed.

Thanks.


; struct.asm
.486                       
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive


    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\Comctl32.inc
    include \masm32\include\comdlg32.inc
    include \masm32\include\shell32.inc
    include \masm32\include\oleaut32.inc
    include \masm32\macros\macros.asm

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\Comctl32.lib
    includelib \masm32\lib\comdlg32.lib
    includelib \masm32\lib\shell32.lib
    includelib \masm32\lib\oleaut32.lib


.data?

CONFIGSETUP STRUCT
Window       dd ?
System       dd ?
System32     dd ?
Cookies      dd ?
Startup      dd ?
Minimized    dd ?
RegCheck     dd ?
FirstRun     dd ?
CONFIGSETUP ENDS

.data

hFile       dd 0
byteswrote  dd 0
pcs         CONFIGSETUP<0,0,0,0,0,0,0,0>
fname1  db "c:\work\dont_look_inside.cfg",0

.code

start:

main proc

  invoke CreateFile,ADDR fname1,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        .if hFile == INVALID_HANDLE_VALUE
            invoke MessageBox,hWnd,txt("Error, Opening Options.cfg File"),txt("Create File Error!"),MB_OK
            ret
        .endif
        invoke ReadFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
        invoke CloseHandle,hFile
        jmp end
;*****************************************************************
; And this is how you can update the file after you have made some
; changes
;*****************************************************************

        invoke CreateFile,txt("Options.cfg"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        .if hFile == INVALID_HANDLE_VALUE
            invoke MessageBox,hWnd,txt("Error, Creating File"),txt("Create File Error!"),MB_OK
            ret
        .endif
        invoke WriteFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
        invoke CloseHandle,hFile

end:

    invoke ExitProcess,0


main endp

end start



PBrennick

Skywalker,
1. I do not seem to have the txt macro so I replaced all occurences of txt with CTXT (keep the uppercase)
2. You have a line that says end:
    You cannot do the because end is a reserved word and it will cause nesting errors.
    Change end to TheEnd and the jump line to jmp TheEnd
3. A window has not been created so replace all occurrences of hWnd with NULL (keep the uppercase)

Once you make these changes, it will assemble without errors and when run it will show a message box that says options.cfg does not exist.  This is correct.  Once I got that far, I made some changes to the WriteFile line and was able to create options.cfg and store the string fname1 in it but you may want to try that yourself first.  If you have more questons just ask.  I am just helping until zcoder logs in.  I thought you should tackle this one thing at a time until eventually you will write the structure there.  If you want me to post the first set of changes as a complete program, I will.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

zcoder


      .386
      .model flat, stdcall  ; 32 bit memory model
      option casemap :none  ; case sensitive

;**********************************************************************
;     include files
;**********************************************************************     
      include \Masm32\INCLUDE\windows.inc
      include \Masm32\INCLUDE\user32.inc
      include \Masm32\INCLUDE\kernel32.inc
      include \Masm32\INCLUDE\comdlg32.inc
      include \Masm32\include\Comctl32.inc

;**********************************************************************
;     libraries
;**********************************************************************
      includelib \Masm32\LIB\user32.lib
      includelib \Masm32\LIB\kernel32.lib
      includelib \Masm32\LIB\comdlg32.lib
      includelib \Masm32\lib\Comctl32.lib

include\masm32\macros\macros.asm

;**********************************************************************
; Local prototypes
;**********************************************************************
        WinMain                 PROTO :DWORD
        WinProc                 PROTO :DWORD,:DWORD,:DWORD,:DWORD
        wsprintfA               PROTO C :DWORD,:VARARG

        wsprintf equ <wsprintfA>

;**********************************************************************
; Place all your Constance here.
;**********************************************************************
.const
ICO_MAINICON     equ 500

;**********************************************************************
; Inishilized DATA
;**********************************************************************
.data
Wwidth           dd 350
Wheight          dd 315
szClassName      db "Template_Class",0
szAppName        db "Template",0

;**********************************************************************
; Un-Inishilized DATA
;**********************************************************************
.data?
hWnd             dd ?
hInstance        dd ?
hIcon            dd ?

CONFIGSETUP STRUCT
Window       dd ?
System       dd ?
System32     dd ?
Cookies      dd ?
Startup      dd ?
Minimized    dd ?
RegCheck     dd ?
FirstRun     dd ?
CONFIGSETUP ENDS

.data

hFile       dd 0
byteswrote  dd 0
pcs         CONFIGSETUP<0,0,0,0,0,0,0,0>
fname1  db "c:\work\dont_look_inside.cfg",0

.code
;**********************************************************************
; This is the start of the code section, here we get our Module Handle
; and start our Program.
;**********************************************************************
start:
      mov hInstance,FUNC(GetModuleHandle,NULL)
      invoke WinMain,hInstance
      invoke ExitProcess,eax

;**********************************************************************
; This section creates the MAIN CLASS and the MAIN window.
; Then loops sending messages to our WinProc for Proccessing.
;**********************************************************************
WinMain proc hModule:DWORD

        LOCAL wc   :WNDCLASSEX
        LOCAL msg  :MSG

      mov hIcon,FUNC(LoadIcon,hModule,ICO_MAINICON)    ; icon ID
      mov wc.cbSize,sizeof WNDCLASSEX
      mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
      mov wc.lpfnWndProc,offset WinProc
      mov wc.cbClsExtra,NULL
      mov wc.cbWndExtra,NULL
      m2m wc.hInstance,hModule
      mov wc.hbrBackground,COLOR_BTNFACE+1
      mov wc.lpszMenuName,NULL
      mov wc.lpszClassName,offset szClassName
      m2m wc.hIcon,hIcon
      mov wc.hCursor,FUNC(LoadCursor,NULL,IDC_ARROW)
      m2m wc.hIconSm,hIcon
      invoke RegisterClassEx, addr wc
      mov hWnd,FUNC(CreateWindowEx,WS_EX_LEFT,addr szClassName,addr szAppName,WS_SIZEBOX or WS_CAPTION or WS_SYSMENU or WS_MINIMIZEBOX or WS_MAXIMIZEBOX or WS_POPUP or WS_VISIBLE or WS_BORDER or WS_DLGFRAME,10,10,Wwidth,Wheight,NULL,NULL,hModule,NULL)
      invoke ShowWindow,hWnd,SW_SHOWNORMAL
      invoke UpdateWindow,hWnd

      ;===================================
      ; Loop until PostQuitMessage is sent
      ;===================================

    StartLoop:
      invoke GetMessage,addr msg,NULL,0,0
      cmp eax,0
      je ExitLoop
      invoke TranslateMessage, addr msg
      invoke DispatchMessage,  addr msg
      jmp StartLoop
    ExitLoop:

      return msg.wParam

WinMain endp
;**********************************************************************
; This is the Main programs code area. all messages for this APP are
; recived here, or the main app.
;**********************************************************************
WinProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD


          .if uMsg == WM_CREATE
              invoke CreateFile,ADDR fname1,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
              mov hFile,eax
              .if hFile == INVALID_HANDLE_VALUE
                  invoke CreateFile,txt("Options.cfg"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
                  mov hFile,eax
                  invoke WriteFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
                  jmp Create_end
              .endif
              invoke ReadFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
              invoke CloseHandle,hFile
Create_end:


          .elseif uMsg == WM_PAINT


          .elseif uMsg == WM_COMMAND


          .elseif uMsg == WM_SIZE


          .elseif uMsg == WM_CLOSE
                  invoke CreateFile,txt("Options.cfg"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
                  mov hFile,eax
                  .if hFile == INVALID_HANDLE_VALUE
                      invoke MessageBox,hWnd,txt("Error, Creating File"),txt("Create File Error!"),MB_OK
                      ret
                  .endif
                  invoke WriteFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
                  invoke CloseHandle,hFile
                  invoke DestroyIcon,hIcon
                  invoke DestroyWindow,hWin

          .elseif uMsg == WM_DESTROY
                  invoke PostQuitMessage,0
                  return 0
          .endif


          invoke DefWindowProc,hWin,uMsg,wParam,lParam
          ret
WinProc endp

end start


The code above is a complete app
it starts out by trying to open the Options.cfg file
if it fails on this then this might be the first time
the program has run so it writes one to the disk.

At this point you could fill default stuff into it before
writing it to disk.

Finally when the app close's you write it to the file.
this is good for apps that may do stuff like store the
window position, and other stuff then saves this
just before exiting.

I am sure you can dream up alot of use's and also
make up your own items to place in the structure
for the type of app you want to create.

you could also create it to show a count just after WM_CREATE
where it already read the file in. and inc a count in the struct
so when your app close's it will be saved.

So each time you run it it will show the count. this will show
you that this works great for keeping track of many things
your imagination can create.


If you need more info or help I can help you in that direction.

Zcoder....
Back in 1979, My computer ran so fine.
And there was no such thing,
As a Microsoft Crashed Machine.
http://zcoder.110mb.com
http://www.dietzel.com/partner/idevaffiliate.php?id=345_6  Free Domain Names

skywalker

Quote from: PBrennick on March 05, 2006, 11:49:19 PM
Skywalker,
1. I do not seem to have the txt macro so I replaced all occurences of txt with CTXT (keep the uppercase)
2. You have a line that says end:
    You cannot do the because end is a reserved word and it will cause nesting errors.
    Change end to TheEnd and the jump line to jmp TheEnd
3. A window has not been created so replace all occurrences of hWnd with NULL (keep the uppercase)

Once you make these changes, it will assemble without errors and when run it will show a message box that says options.cfg does not exist.  This is correct.  Once I got that far, I made some changes to the WriteFile line and was able to create options.cfg and store the string fname1 in it but you may want to try that yourself first.  If you have more questons just ask.  I am just helping until zcoder logs in.  I thought you should tackle this one thing at a time until eventually you will write the structure there.  If you want me to post the first set of changes as a complete program, I will.

Paul


This is what I just completed. It creates the file OK.

I'm thinking now i need to learn how to fill the structure and how to store it in the created file.

Later, I can learn how to modify it and search for things. In 16 bit, I used to use debug and the d command to see where in the .exe stored data/structures were. I think I still have a 32 bit command line debugger somewhere.

It helps me a lot to sometimes place markers in the code to see where stuff is being stored.

Is there an easy way to jump around code that you haven't had time to correct without using ; all over the
place ?

start:

main proc

  invoke CreateFile,ADDR fname1,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        .if hFile == INVALID_HANDLE_VALUE
            invoke MessageBox,NULL,ADDR mess,ADDR box,MB_OK
            ret
        .endif
        invoke ReadFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
        invoke CloseHandle,hFile
       
;*****************************************************************
; And this is how you can update the file after you have made some
; changes
;*****************************************************************

;        invoke CreateFile,txt("Options.cfg"),GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
;        mov hFile,eax
;        .if hFile == INVALID_HANDLE_VALUE
;            invoke MessageBox,hWnd,txt("Error, Creating File"),txt("Create File Error!"),MB_OK
;            ret
;        .endif
;        invoke WriteFile,hFile,addr pcs,sizeof pcs,addr byteswrote,0
;        invoke CloseHandle,hFile

    invoke ExitProcess,0