Jump to content
Tuts 4 You

Writing File


Recommended Posts

hi all

i create a small app just for a test i don't why he got a crashes when write to file :dunno:

FILENAME DB "key.lic",0.data?
hInstance HINSTANCE ?
read db ?
write db ?.const
IDD_DLG1 equ 1000
IDC_BTN1 equ 1001
IDC_EDT1 equ 1002DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
invoke SendMessage, hWnd, WM_SETICON, 1, eax
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if eax==IDC_BTN1
invoke GetDlgItemText,1000,1002,addr read,512
invoke SetFilePointer,addr FILENAME,0,0,FILE_BEGIN
invoke WriteFile,addr FILENAME,addr read,32,32,NULL
invoke CloseHandle, eax
.elseif uMsg == WM_CLOSE
invoke EndDialog, hWnd, 0
invoke ExitProcess,0
xor eax,eax
DlgProc EndP
end start

is there any suggest or advice? :help

Link to comment

hello ...

since its a single line work ... i suggest u do this :


hdir db 256 dup(?)

htemp dd ?

invoke GetDlgItemText,hWnd,IDC_EDT1,addr read,512 ; Copy value from Editbox to buffer [read]

invoke GetCurrentDirectory,sizeof hdir ,addr hdir ; Get Current Directory

invoke lstrcat,addr hdir,offset FILENAME

invoke CreateFile,addr hdir,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0 ; File Attributes

push eax

invoke WriteFile,eax,addr read,sizeof read,addr htemp,0 ; WrireFile

pop eax

invoke CloseHandle,eax ; CloseHandle

Link to comment

i think the error is here:

invoke WriteFile,addr FILENAME,addr read,32,32,NULL

maybe it would be:

dwBytesWritten dw 0

invoke lstrlen, addr read

invoke WriteFile, addr FILENAME, addr read, eax, addr dwBytesWritten, NULL


Link to comment

first mistake is here:

invoke GetDlgItemText,1000,1002,addr read,512

and sould be:

invoke GetDlgItemText,hWnd,1002,addr read,512


invoke SetFilePointer,addr FILENAME,0,0,FILE_BEGIN

from win32api help file

The SetFilePointer function moves the file pointer of an open file. DWORD SetFilePointer(	HANDLE hFile,	// handle of file 
LONG lDistanceToMove, // number of bytes to move file pointer
PLONG lpDistanceToMoveHigh, // address of high-order word of distance to move
DWORD dwMoveMethod // how to move
ParametershFileIdentifies the file whose file pointer is to be moved. The file handle must have been created with GENERIC_READ or GENERIC_WRITE access to the file. lDistanceToMoveSpecifies the number of bytes to move the file pointer. A positive value moves the pointer forward in the file and a negative value moves it backward. lpDistanceToMoveHighPoints to the high-order word of the 64-bit distance to move. If the value of this parameter is NULL, SetFilePointer can operate only on files whose maximum size is 2^32 - 2. If this parameter is specified, the maximum file size is 2^64 - 2. This parameter also receives the high-order word of the new value of the file pointer. dwMoveMethodSpecifies the starting point for the file pointer move. This parameter can be one of the following values: Value Meaning
FILE_BEGIN The starting point is zero or the beginning of the file. If FILE_BEGIN is specified, DistanceToMove is interpreted as an unsigned location for the new file pointer.
FILE_CURRENT The current value of the file pointer is the starting point.
FILE_END The current end-of-file position is the starting point.


invoke WriteFile,addr FILENAME,addr read,32,32,NULL

from the same document ...

The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished. BOOL WriteFile(	HANDLE hFile,	// handle to file to write to 
LPCVOID lpBuffer, // pointer to data to write to file
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped I/O

the rest findit :bomb: .....

Edited by seby
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...