Hey, once again I'm translating C code to ASM (this is getting boring huh?) and just found out a problem... I have this non null terminated string and I need to null terminate it (bet you didn't see that coming...)...

In C I used something like:
pszFileText = 0;

where dwFileSize is the size of the file, and pszFileText is string that will hold the file's content (once again I bet you didn't see that coming...)

I tried this in Assembly:
invoke lstrcat,pszFileText,0

But it dosen't seem to be working... Actually I don't know if the error is there :P The main problem is: I'm doing a notepad, when you open a file for the first time everything works fine, but at the second time the program just closes. If someone comes and tells me that the error is not there I'll post the full code and you guys can tell me where the error is.

Thanks for your patience.
Posted on 2005-09-18 20:53:16 by Lenin
pszFileText = 0;


mov eax, offset pszFileText
mov , 0
Posted on 2005-09-18 21:02:25 by ti_mo_n
I'm getting two weird errors on these lines:

Notepad.asm(172) : error A2098: invalid operand for OFFSET
Notepad.asm(173) : error A2206: missing operator in expression
Posted on 2005-09-18 21:11:24 by Lenin
Without seeing your code, I would assume that pszFileText is the address of the buffer where your text is located. If that is the case, try:

mov eax,pszFileText
add eax,dwFileSize
mov byte ptr,0


Raymond
Posted on 2005-09-18 22:17:26 by Raymond
Thanks :) It solved one problem, but got me another one... Sometimes the code works fine, sometimes nothing happens (most of the times), the same file that oppened one second ago dosen't open anymore... I'll be posting my code now to see if it helps:

Note: This function isn't mine, it's this guy's (modified a little).
LoadTextFileToEdit proc
    LOCAL hFile:HANDLE, dwFileSize:DWORD, pszFileText:LPSTR, dwRead:DWORD

    invoke CreateFile,addr buffer,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL
    .if eax != INVALID_HANDLE_VALUE
        mov hFile, eax
        invoke GetFileSize, hFile, NULL
        .if eax != 0
            mov dwFileSize, eax
            invoke GlobalAlloc, GPTR, dwFileSize + 1
            .if eax != NULL
                mov pszFileText, eax
                invoke ReadFile, hFile, pszFileText, dwFileSize, addr dwRead, NULL
                .if eax==TRUE
                    mov eax,pszFileText
                    add eax,dwFileSize
                    mov byte ptr, 0
                invoke SetWindowText, hwndEdit, pszFileText
                .endif
                invoke GlobalFree, pszFileText
            .endif
        .endif
        invoke CloseHandle, hFile
    .endif
    xor eax, eax
    ret
LoadTextFileToEdit endp


Since I'm already here I have two more problems to be solved: I once made a topic about reading binary files, f0dder said I need to use a bin2hex function, and that there was a speed contest of this function, I searched it but never found it, so if someone could tell me where that contest is...

Second: I'm modifying the edit's size each time a WM_SIZE message is sent so that it will cover the whole program's area, it works fine but when resizing it "blinks", you know, turns kinda brown for some miliseconds...

    .elseif uMsg==WM_SIZE
        mov eax,lParam
        mov edx,eax
        shr edx,16
        and eax,0ffffh
        invoke MoveWindow,hwndEdit,0,0,eax,edx,TRUE
Posted on 2005-09-18 23:11:49 by Lenin
Lenin,
    Read the documentation for GetFileSize.  .IF EAX  !=  0 is not a proper test for errors from this function.


        MOV EDX,lParam
        MOVZX EAX,DX
        SHR EDX,16
        INVOKE MoveWindow,hwndEdit,0,0,EAX,EDX,ESP ;ESP != 0 = TRUE


    The above code is smaller for parsing out lParam.  Pushing ESP for a TRUE value is only a one byte instruction vs pushing a TRUE  as a constant which is two bytes.  Ratch
Posted on 2005-09-19 00:25:54 by Ratch

Lenin,
    Read the documentation for GetFileSize.  .IF EAX  !=  0 is not a proper test for errors from this function.


I should check for 0xffffffff, but when I do
.if eax != 0xffffffff
I get:
Notepad.asm(162) : error A2206: missing operator in expression


The above code is smaller for parsing out lParam.  Pushing ESP for a TRUE value is only a one byte instruction vs pushing a TRUE  as a constant which is two bytes.  Ratch

That works aswell, but the window keeps blinking.
Posted on 2005-09-19 00:44:07 by Lenin



    invoke GlobalAlloc, GPTR, dwFileSize + 1


that's most likely not what you want. if local variable dwFileSize is for example, MASM will push content of instead.

You will need to code:


    mov eax, dwFileSize
    inc eax
    invoke GlobalAlloc, GPTR, eax


Posted on 2005-09-19 00:53:05 by japheth
Works perfectly :) One problem solved, three to go (shown above).
Posted on 2005-09-19 01:12:04 by Lenin

better use -1 (or 0ffffffffh) instead of 0xffffffff
Posted on 2005-09-19 01:19:07 by japheth
:) Now there's only one problem, the finding the bin2hex speed contest...

But... More code needs to be checked :) (don't be mad with me), this time the problem is with the SaveTextFileFromEdit function... When you use it a new file is created (as you specified it) but it comes totally empty, nothing in it.

SaveTextFileFromEdit proc FilePath:DWORD
    LOCAL hFile:HANDLE, dwTextLength:DWORD, dwBufferSize:DWORD, dwWritten:DWORD, pszText:LPSTR
   
    invoke CreateFile,FilePath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
    .if eax != INVALID_HANDLE_VALUE
        mov hFile, eax
        invoke GetWindowTextLength, hwndEdit
        .if eax > 0
            mov dwTextLength, eax
            inc eax
            mov dwBufferSize, eax
            invoke GlobalAlloc, GPTR, dwBufferSize
            .if eax != NULL
                mov pszText, eax
                invoke GetWindowText,hwndEdit, pszText, dwBufferSize
                .if eax==TRUE
                    invoke WriteFile,hFile, pszText, dwTextLength, addr dwWritten, NULL
                .endif
            invoke GlobalFree, pszText
            .endif
        .endif
        invoke CloseHandle, hFile
    .endif
xor eax, eax
ret
SaveTextFileFromEdit endp


And one more really basic question, how do I add a line break to a messagebox? I've tried \n, \n\n, \r\n without any results...
Posted on 2005-09-19 21:19:01 by Lenin
Have you tried using 0dh,0ah instead of '\n\r'? ;)
Posted on 2005-09-19 21:33:59 by roticv
Thanks :)
Posted on 2005-09-19 21:48:15 by Lenin

    .if eax != INVALID_HANDLE_VALUE
        mov hFile, eax
        invoke GetFileSize, hFile, NULL
        .if eax != 0
            mov dwFileSize, eax
            invoke GlobalAlloc, GPTR, dwFileSize + 1
            .if eax != NULL
                mov pszFileText, eax
                invoke ReadFile, hFile, pszFileText, dwFileSize, addr dwRead, NULL
                .if eax==TRUE
                    mov eax,pszFileText
                    add eax,dwFileSize
                    mov byte ptr, 0
...


Each time you compare eax to NULL, it's another change for your code to bail.  Try putting MessageBoxes in your code  to see if you are even getting to the section where you add the null termination to your string.  It can't add the null, if the program bails before hand.

-#2pencil-
Posted on 2005-09-20 10:41:22 by number2pencil
Oh thanks for the hint, just found where the error was...

The error was that GetWindowText dosn't return TRUE on succes, but the window lenght instead, so just had to change from .if eax == TRUE to .if eax

In case you have the link to that bin2hex speed contest link please post it :)
Posted on 2005-09-20 17:10:09 by Lenin
Now I'm having problems with font choosing...

When someone clicks the font menuitem:
    .elseif ax==ID_FORMAT_FONT
        invoke DoSelectFont
       
        invoke InvalidateRect, hWnd, NULL, TRUE
        invoke UpdateWindow, hWnd


And the DoSelectFont function:
DoSelectFont proc
    LOCAL cf:CHOOSEFONT, lf:LOGFONT

    invoke RtlZeroMemory, addr cf, sizeof cf
   
    mov  cf.lStructSize, sizeof cf
    push hwnd
    pop  cf.hwndOwner
    lea  ebx, lf
    mov  cf.lpLogFont, ebx
    push g_rgbText
    pop  cf.rgbColors
    mov  cf.Flags, CF_EFFECTS or CF_INITTOLOGFONTSTRUCT or CF_SCREENFONTS

    invoke ChooseFont, addr cf
    .if eax != 0
        invoke CreateFontIndirect, addr lf
        .if eax != NULL
            mov g_hfFont, eax
        .endif
        push cf.rgbColors
        pop g_rgbText
    .endif
    xor eax, eax
    ret
DoSelectFont endp


The dialog is created finely, but no font is selected after that.
Posted on 2005-09-24 11:50:58 by Lenin
but no font is selected after that


If you mean you cannot see the new font on the monitor, you then have to select that new font in whatever window or control where it needs to be used. See SelectObject.

If you meant something else, please clarify.

Raymond
Posted on 2005-09-24 12:33:56 by Raymond
By "selected" I meant applied to the screen.

I tried changing my code to this but it didn't work out:
    .if eax != NULL
        mov g_hfFont, eax
        invoke SelectObject, hwnd, g_hfFont
    .endif
Posted on 2005-09-24 18:01:16 by Lenin
A kind word of advice: Whenever you need to use one of the Windows functions, you should inform yourself of its requirements; it will save you many headaches.

The very first line of this one is (bold and underline added):

The SelectObject function selects an object into the specified device context.

Raymond
Posted on 2005-09-24 22:58:14 by Raymond
I'm sorry saying that but your post didn't rang any bell to me...

From what you've said I got these two codes:

    .if eax != NULL
        mov g_hfFont, eax
        invoke SelectObject, hwndEdit, g_hfFont
    .endif


    .if eax != NULL
        mov g_hfFont, eax
        invoke BeginPaint, hwnd, addr ps
        invoke SelectObject, eax, g_hfFont
        invoke EndPaint, hwnd, addr ps
    .endif


As you may have figured out none of them work...
Posted on 2005-09-24 23:58:35 by Lenin