Author Topic: Help OPENFILENAME  (Read 1413 times)

Grincheux

  • Member
  • ***
  • Posts: 328
  • Never be pleased, Always improve
    • Asm for fun
Help OPENFILENAME
« on: November 30, 2015, 11:33:36 PM »
In the source there are no errors, but when running it crashes when GetOpenFileName calls lstrlen.
I suppose this is because the alignment of the structure is bad, I tried with a buffer too the result is the same.
Code: [Select]
.X64
    option casemap:none
    option frame:auto    ;generate SEH-compatible prologues and epilogues
    option PROCALIGN:16
    OPTION WIN64:1
    OPTION FIELDALIGN:4

; porc64 /v "Test.rc"
; jwasm -win64 -Zp4 -W4 -Sn -Sg -Sa -Zlf -Zlp -Zls -IC:\JWAsm\Include -Fl=Test.lst Test.asm
; jwlink.exe format windows pe runtime windows file Test.obj LibPath c:\JWasm\Lib64 op MAP, quiet, res=Test.res

WINVER EQU 0a00h

INCLUDE C:\JWAsm\Include\windows.inc
INCLUDE C:\JWAsm\Include\commdlg.inc
INCLUDE Test.inc

;     libraries
;     ~~~~~~~~~

    ; ------------------------------------------
    ; import libraries for Windows API functions
    ; ------------------------------------------

INCLUDELIB C:\JWAsm\Lib64\gdi32.lib
INCLUDELIB C:\JWAsm\Lib64\user32.lib
INCLUDELIB C:\JWAsm\Lib64\kernel32.lib
INCLUDELIB C:\JWAsm\Lib64\Comctl32.lib
INCLUDELIB C:\JWAsm\Lib64\comdlg32.lib
INCLUDELIB C:\JWAsm\Lib64\shell32.lib
INCLUDELIB C:\JWAsm\Lib64\oleaut32.lib
INCLUDELIB C:\JWAsm\Lib64\ole32.lib
INCLUDELIB C:\JWAsm\Lib64\MSVCRT.lib
INCLUDELIB C:\JWAsm\Lib64\Shlwapi.lib
INCLUDELIB libad64.lib

WPARAM TYPEDEF QWORD
UINT TYPEDEF DWORD

NULL EQU 0
WS_OVERLAPPEDWINDOW EQU 0CF0000h
CW_USEDEFAULT EQU 80000000h
SW_SHOWDEFAULT EQU 10
SW_SHOWNORMAL EQU 1
IDC_ARROW EQU 32512
IDI_APPLICATION EQU 32512
WM_DESTROY EQU 0002h
WM_COMMAND EQU 0111h
CS_VREDRAW EQU 1
CS_HREDRAW EQU 2
COLOR_WINDOW EQU 5
MAX_PATH EQU 260

IDM_FILE_OPEN EQU 10002
IDM_FILE_EXIT EQU 10006

WNDPROC TYPEDEF ptr proto_WNDPROC
comment @
OPENFILENAMEA STRUCT
lStructSize DWORD ?
hwndOwner HWND ?
hInstance HINSTANCE ?
lpstrFilter LPSTR ?
lpstrCustomFilter LPSTR ?
nMaxCustFilter DWORD ?
nFilterIndex DWORD ?
lpstrFile LPSTR ?
nMaxFile DWORD ?
lpstrFileTitle LPSTR ?
nMaxFileTitle DWORD ?
lpstrInitialDir LPSTR ?
lpstrTitle LPSTR ?
Flags DWORD ?
nFileOffset WORD ?
nFileExtension WORD ?
lpstrDefExt LPSTR ?
lCustData LPARAM ?
lpfnHook LPOFNHOOKPROC ?
lpTemplateName LPSTR ?
pvReserved DWORD ?
dwReserved DWORD ?
FlagsEx DWORD ?
OPENFILENAMEA ENDS

OPENFILENAME typedef OPENFILENAMEA
LPOPENFILENAME typedef LPOPENFILENAMEA
@
WNDCLASSEXA STRUCT 8
cbSize DWORD ?
style DWORD ?
lpfnWndProc WNDPROC ?
cbClsExtra DWORD ?
cbWndExtra DWORD ?
hInstance HINSTANCE ?
hIcon HICON ?
hCursor HCURSOR ?
hbrBackground HBRUSH ?
lpszMenuName LPSTR ?
lpszClassName LPSTR ?
hIconSm HICON ?
WNDCLASSEXA ENDS

WNDCLASSEX typedef WNDCLASSEXA

POINT STRUCT
x SDWORD ?
y SDWORD ?
POINT ENDS

WinMain PROTO :HINSTANCE,:HINSTANCE,:LPSTR,:UINT

GetModuleHandle EQU <GetModuleHandleA>
GetModuleFileName EQU <GetModuleFileNameA>
GetCommandLine EQU <GetCommandLineA>
PathRemoveBackslash EQU <PathRemoveBackslashA>
PathFindFileName EQU <PathFindFileNameA>
PathRemoveBackslash EQU <PathRemoveBackslashA>
LoadIcon EQU <LoadIconA>
LoadCursor EQU <LoadCursorA>
RegisterClassEx EQU <RegisterClassExA>
CreateWindowEx EQU <CreateWindowExA>
GetMessage EQU <GetMessageA>
DispatchMessage EQU <DispatchMessageA>
DefWindowProc EQU <DefWindowProcA>
LoadMenu EQU <LoadMenuA>

.Data

szClassName Byte "SimpleWinClass",0
szAppName Byte "Our First Window",0
szFilters Byte "Images",0,"*.jpg;*.png;*.gif;*.bmp",0,"All Files",0,"*.*",0,0
szDefExt Byte "jpg",0

.Data?

hInstance HINSTANCE ?
lpszCommandLine LPSTR ?

szPgmDirectory Byte MAX_PATH dup(?)
szPgmFileName Byte MAX_PATH dup(?)
szCurrentFile         Byte MAX_PATH dup(?)

.Code

WinMainCRTStartup PROC FRAME

    INVOKE GetModuleHandle,NULL

    mov hInstance,rax

INVOKE GetModuleFileName,rax,ADDR szPgmFileName,MAX_PATH
INVOKE lstrcpy,ADDR szPgmDirectory,ADDR szPgmFileName
INVOKE lstrlen,rax

lea rcx,OFFSET szPgmDirectory
add rcx,rax

@Loop :

cmp Byte Ptr [rcx],'\'
je @EndLoop

sub rcx,1
sub rax,1
jnz @Loop

@EndLoop :

mov Byte Ptr [rcx],0

              call GetCommandLine

    mov lpszCommandLine,rax

    INVOKE WinMain,hInstance,NULL,lpszCommandLine,SW_SHOWDEFAULT
    INVOKE ExitProcess,eax
WinMainCRTStartup ENDP

WinMain PROC FRAME __hInst:HINSTANCE,__hPrevInst:HINSTANCE,__lpszCmdLine:LPSTR,__nCmdShow:UINT
              LOCAL _Wc:WNDCLASSEX

          mov _Wc.hInstance,rcx

sub rsp,32
xor rcx,rcx
mov rdx,IDC_ARROW
call    LoadCursor

    mov _Wc.hCursor,rax

xor rcx,rcx
mov rdx,IDI_APPLICATION
call    LoadIcon

        lea rcx,_Wc
        mov [rcx].WNDCLASSEX.hIcon,rax
mov [rcx].WNDCLASSEX.hIconSm,rax
mov [rcx].WNDCLASSEX.cbSize,SIZEOF WNDCLASSEXA
mov [rcx].WNDCLASSEX.style,CS_HREDRAW or CS_VREDRAW
lea rax,[WndProc]
mov [rcx].WNDCLASSEX.lpfnWndProc,rax
lea rax,szClassName
mov [rcx].WNDCLASSEX.lpszClassName,rax
xor rax,rax
mov [rcx].WNDCLASSEX.cbClsExtra,eax
mov [rcx].WNDCLASSEX.cbWndExtra,eax
mov [rcx].WNDCLASSEX.lpszMenuName,IDM_MENU
mov [rcx].WNDCLASSEX.hbrBackground,COLOR_WINDOW + 1

call    RegisterClassEx
add rsp,32

INVOKE CreateWindowEx,NULL,ADDR szClassName,ADDR szAppName,WS_OVERLAPPEDWINDOW,\
    CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
    NULL,NULL,_Wc.hInstance,NULL

test rax,rax
jnz @DisplayWindow

ret

@DisplayWindow :

mov rcx,rax

sub rsp,32
mov _Wc,rax
mov edx,SW_SHOWNORMAL
call    ShowWindow

mov rcx,_Wc
call         UpdateWindow
add rsp,32

@MsgLoop :

lea rcx,_Wc ;_Msg
push         rcx
    INVOKE GetMessage,rcx,NULL,0,0
    pop rcx

    test  rax,rax
    jnz @DecodeMsg

mov rax,_Wc + 10h
ret

@DecodeMsg :

sub rsp,32
push rcx
    call         TranslateMessage
    pop rcx
    call    DispatchMessage
    add rsp,32
    jmp @MsgLoop
WinMain ENDP

File_Select PROC FRAME __hWnd:HWND
LOCAL _Ofn:OPENFILENAME

push  rcx
INVOKE RtlZeroMemory,ADDR _Ofn,SIZEOF OPENFILENAME
lea rcx,_Ofn
pop [rcx].OPENFILENAME.hwndOwner
mov [rcx].OPENFILENAME.lStructSize,SIZEOF OPENFILENAME
mov [rcx].OPENFILENAME.nFilterIndex,1
mov [rcx].OPENFILENAME.Flags,OFN_ENABLESIZING + OFN_EXPLORER + OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST + OFN_HIDEREADONLY + OFN_NONETWORKBUTTON
mov [rcx].OPENFILENAME.nMaxFile,MAX_PATH
lea rdx,szFilters
mov [rcx].OPENFILENAME.lpstrFilter,rdx
lea rdx,szDefExt
mov [rcx].OPENFILENAME.lpstrDefExt,rdx
lea rdx,szCurrentFile
mov [rcx].OPENFILENAME.lpstrFile,rdx

INVOKE GetOpenFileName,rcx

ret
File_Select ENDP

Proceed_WmCommand PROC FRAME __hWnd:HWND,__wParam:WPARAM,__lParam:LPARAM

mov rax,rdx
and rax,0000ffffh

cmp eax,IDM_FILE_OPEN
jne @L1

INVOKE File_Select,rcx

xor rax,rax
ret

@L1 :

cmp eax,IDM_FILE_EXIT
jne @Default

INVOKE DestroyWindow,rcx

xor rax,rax
ret

@Default :

mov r9,r8
mov r8,rdx
mov edx,WM_COMMAND

INVOKE DefWindowProc,rcx,edx,r8,r9

ret
Proceed_WmCommand ENDP

WndProc PROC FRAME __hWnd:HWND,__uMsg:UINT,__wParam:WPARAM,__lParam:LPARAM

cmp edx,WM_COMMAND
jne @L1

INVOKE Proceed_WmCommand,rcx,r8,r9
ret

@L1 :

    cmp edx,WM_DESTROY
    jne @Default

@WmDestroy :

INVOKE PostQuitMessage,NULL

xor eax,eax
ret

@Default :

    INVOKE DefWindowProc,rcx,edx,r8,r9

    ret
WndProc ENDP

END WinMainCRTStartup

It seems the problem is at offset of lCustData, to this place there is no string.
In the flag I dont want any custom data.

Any help is necessary. Thanks in advance.
Kenavo (Bye)
----------------------
Asm for Fun
My Links
"La garde meurt mais ne rend pas"
Cambronne à Waterloo

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Help OPENFILENAME
« Reply #1 on: December 01, 2015, 01:10:33 AM »
Quote
INVOKE   lstrcpy,ADDR szPgmDirectory,ADDR szPgmFileName
INVOKE   lstrlen,rax   ;<<<<<<<<<<<< Must be an adress not False return value of lstrcpy

return value of lstrcpy
Type: LPTSTR
If the function succeeds, the return value is a pointer to the buffer.
If the function fails, the return value is NULL and lpString1 may not be null-terminated
Miss
.if rax != 0
 INVOKE   lstrlen,rax   
.endif


Fa is a musical note to play with CL

Grincheux

  • Member
  • ***
  • Posts: 328
  • Never be pleased, Always improve
    • Asm for fun
Re: Help OPENFILENAME
« Reply #2 on: December 01, 2015, 01:29:19 AM »
Merci,

Pour une fois qu'on peut parler Français.
Le problème est lorsque GetOpenFileName appelle lstrlen, alors ca plante.
Je pense à un problème d'alignement de la structure OPENFILENAME.

Pour ce qui est de ta remarque je vais corriger.

Pour info je suis dans le Jura.


Sorry, for these few words in French Hutch... There are not many French here...
Kenavo (Bye)
----------------------
Asm for Fun
My Links
"La garde meurt mais ne rend pas"
Cambronne à Waterloo

Grincheux

  • Member
  • ***
  • Posts: 328
  • Never be pleased, Always improve
    • Asm for fun
Re: Help OPENFILENAME
« Reply #3 on: December 01, 2015, 02:06:02 AM »
Maybe many comdlg32.dll on my computer so I don't use the correct one.

Into SysWow64 its size is 733 Kb
and in System32 the size is 930 Kb

Kenavo (Bye)
----------------------
Asm for Fun
My Links
"La garde meurt mais ne rend pas"
Cambronne à Waterloo

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Help OPENFILENAME
« Reply #4 on: December 01, 2015, 03:39:34 AM »
No bug using the sdk64 translated for include,verify with windbg
http://masm32.com/board/index.php?topic=563.msg4563#msg4563
You can't mix 32 bits Library with 64 bits code,this generate an error at link
« Last Edit: December 01, 2015, 05:35:37 AM by ToutEnMasm »
Fa is a musical note to play with CL