News:

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

FileName Recognition Problem?

Started by BrWarburto, November 08, 2009, 06:24:26 PM

Previous topic - Next topic

BrWarburto

Hi Folks,

Working with Windows library CreateFileA, simple filenames viz 'infile' are recognised. However compound filenames containing a period viz 'infile.htm' are not recognised and cause EAX to be returned with -1!!
Any suggested fixes would be welcome. Thanks Brian

dedndave

use the GetLastError function to get more descriptive error information
it should open files with extensions
perhaps you are not using the right combination of flags - show us the code you are using

BrWarburto

Hi Dedndave.  Many thanks for your suggestions. I will certainly put up the code shortly and also investigate the the error code analysis.
Brian  :U

BrWarburto

The coding I use is in the subrtn 'infile' below. Prior to calling 'infile', ESI must point to the address of the file to be input.
         
e.g.

MOV ESI, ADDR SzInFile




InFile:
PUSH 0,80h,3
PUSH 0,1h,80000000h
PUSH ESI
;PUSH ADDR SzInFile
CALL CreateFileA
CMP EAX,-1
JNE > .fin
INVOKE wsprintfA, ADDR SzWorkStr,ADDR SzErrorFiNa
ADD esp,8d
CALL PRNTFL
RET
.fin
PUSH EAX
INVOKE wsprintfA, ADDR SzWorkStr,ADDR SzOkMess,EAX
ADD esp,12d
CALL PRNTFL
POP EAX
PUSH EAX
PUSH 0,ADDR RCKEEP
PUSH 1000000D,[AdSzBuffer]
PUSH EAX
CALL ReadFile
INVOKE wsprintfA, ADDR SzWorkStr,ADDR SzFileSize,[RCKEEP]
PUSH [RCKEEP]
ADD esp,12d
CALL PRNTFL
POP [RCKEEP]
POP EAX
PUSH EAX
CALL CloseHandle
RET



As I stated in previous posting, this work fine for a 'simple' filename, but seems to fail if the filename has an extension.


Brian

BlackVortex

#4

1) Use invokes for everything, not half of the stuff
2) Use frames
3) Use constants !!!!

Yuri

The call seems correct. As you are using the OPEN_EXISTING flag, make sure the file is where it should be, especially if the path is relative. And try calling GetLastError after CreateFileA, its return may clarify the situation.

BrWarburto

Yuri,

This is helpful advice. Many thanks. brwarburto

BlackVortex

Quote from: BrWarburto on November 10, 2009, 03:19:03 PM
Yuri,

This is helpful advice. Many thanks. brwarburto
Why did you PM me saying :
QuoteYour post was unfortunately insulting and useless. Why don't you try attending a Charm School Course?

I was trying to help you.

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

FlySky

My question is kind of based on the GetOpenFileName API. I can't get it to work after I converted the masm function from iczelion's tutorial.

I have defined the needed variables in the DATA section:

ofn                    OPENFILENAME <>            ;OpenFileName Structure
szFileFilter      db 'dll files (*.dll)',0,'*.dll',0,0   ;Used in OpenFileName to determine the files to filter
szSelectDll      db 'Select DLL to inject ...',0         ;Caption of OpenFileNameDialogBox
OpenFileNameBuffer   dd  255 dup (?)               ;Buffer for OpenFileNameStructure

Than from 1 dialogbox I open up another dialogbox which I called DLLProc which you can see below and from there when I press a button I want to open a file.

DLLProc FRAME hWndDLL, uMsg, wParam, lParam

Wm_1a:
            cmp D[uMsg],WM_INITDIALOG
            jnz >Wm_2a       
Wm_2a:
       cmp D[uMsg],WM_COMMAND
            jnz >Wm_3a
       cmp D[wParam],IDOK
            jnz >CheckSelectDLL
            invoke EndDialog, [hWndDLL],IDOK
       CheckSelectDLL:
            cmp D[wParam],105                              ;This is a PUSHBUTTON I defined
            jnz >Wm_3a
            mov D[ofn.lStructSize], sizeof ofn
       push [hWndDLL]
            pop  [ofn.hwndOwner]
       push [hInstance]
            pop  [ofn.hInstance]
       mov  [ofn.lpstrFilter], offset szFileFilter
       mov  [ofn.lpstrFile], offset OpenFileNameBuffer
       mov D[ofn.nMaxFile], 256
            mov  [ofn.lpstrTitle], offset szSelectDll
            mov  [ofn.Flags], OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or   OFN_HIDEREADONLY
       invoke GetOpenFileName, offset ofn
            invoke CommDlgExtendedError

Wm_3a:
            xor   eax, eax
            ret
ENDF

I included the CommDlgExtendedError API to determine the error. The GetOpenFileName API returns 0 meaning it fails. The CommDlgExtendedError API returns 1 when catching it in OLLY does this mean:

That this is my error? It's the first ERROR from MSDN.
CDERR_DIALOGFAILURE The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle.

I have no idea how to fix my problem, it might be small it might be big I have no idea what could be wrong.

Thanks in advance.

Yuri

Not sure I understand where your problem is. But in the following test case, your code compiles and works for me.


#define LINKFILES
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>


DATA SECTION

ofn                  OPENFILENAME <>            ;OpenFileName Structure
szFileFilter         db 'dll files (*.dll)',0,'*.dll',0,0   ;Used in OpenFileName to determine the files to filter
szSelectDll          db 'Select DLL to inject ...',0         ;Caption of OpenFileNameDialogBox
OpenFileNameBuffer   dd  256 dup ?               ;Buffer for OpenFileNameStructure

hInstance            dd 0


CODE SECTION

Start:
    invoke DLLProc, NULL, WM_COMMAND, 105, 0
    invoke MessageBox, 0, addr OpenFileNameBuffer, "Test", 0    ; See the DLL you selected.
    invoke ExitProcess, 0
    ret


DLLProc FRAME hWndDLL, uMsg, wParam, lParam

Wm_1a:
    cmp D[uMsg],WM_INITDIALOG
    jnz >Wm_2a       
Wm_2a:
    cmp D[uMsg],WM_COMMAND
    jnz >Wm_3a
        cmp D[wParam],IDOK
        jnz >CheckSelectDLL
            invoke EndDialog, [hWndDLL],IDOK
CheckSelectDLL:
    cmp D[wParam],105                              ;This is a PUSHBUTTON I defined
    jnz >Wm_3a
        mov D[ofn.lStructSize], sizeof ofn
        push [hWndDLL]
        pop  [ofn.hwndOwner]
        push [hInstance]
        pop  [ofn.hInstance]
        mov  [ofn.lpstrFilter], offset szFileFilter
        mov  [ofn.lpstrFile], offset OpenFileNameBuffer
        mov D[ofn.nMaxFile], 256
        mov  [ofn.lpstrTitle], offset szSelectDll
        mov  D[ofn.Flags], OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY
        invoke GetOpenFileName, offset ofn
        invoke CommDlgExtendedError
Wm_3a:
    xor   eax, eax
    ret

ENDF


FlySky

Hey there Yuri,

This works perfectly fine yeah, telling me what I created works. Although I think my problem has to do with this:

I have a MAIN dialogprocedure. This first dialog is having the gfx and some buttons.

In here I have an button to create another dialogbox. This new dialogbox is just simple. It has another button which can Open a File and store the link to that file in a editbox control.

Somehow when having another dialogbox controlling the GetOpenFileName API it refuses to work. Without that second dialogbox it works just like Yuri said. I think there is something going wrong with the handle to that dialogbox?

*EDIT*

I figured out the problem.

;this links in necessary imports
#DEFINE LINKFILES

;comment this if building for 32bit
;#Define WIN64

If I uncomment this line and create an 32 bit exe it does not work. If I comment it it works perfect. Silly mistake actually

donkey

Hi Flysky,

When you use the WIN64 flag there are quite a few changes to structures and definitions, I will look into generating an assembly time error when you attempt to use 64 bit structures in a 32 bit application (and vice versa). It shouldn't be too difficult and most likely will be in the next release of the headers. My only question is whether I should make the error fatal and stop assembly or a warning and allow the assembly to continue, any thoughts would be appreciated, I am leaning towards a fatal error.
"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

FlySky

Yeah I totally overlooked I had it enabled. A warning error would seem to fit. I am switching between 32 bit and 64 bit a lot learning the differences between the use of API's on both systems. I totally forgot I had it enabled. A warning message like I said would seem to fit. The person programming could than take a look and decided what to do. I wouldn't be bothered if you would add a check to completely stop assembling that is fine by me aswell.