flat assembler
Message board for the users of flat assembler.

Index > Windows > pointer to buffer

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 Jun 2010, 23:08
i alloc some space:
Code:
        invoke  VirtualAlloc,0,4*1024,MEM_RESERVE+MEM_COMMIT,PAGE_READWRITE
        mov     [hTextBuff],eax     

and try to put some values there:
Code:
        lea     esi,[hTextBuff]
        mov     byte[esi],'a'
        mov     byte[esi+1],'b'
        mov     byte[esi+2],0
        invoke  DrawText,ebx,esi,-1,rect,DT_NOCLIP     

whats wrong?

_________________
Sorry if bad english.
Post 28 Jun 2010, 23:08
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 28 Jun 2010, 23:35
Quote:


whats wrong?

lea should be mov instead.
Post 28 Jun 2010, 23:35
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 28 Jun 2010, 23:39
still the same with mov..

[edit]at least it show a rectangle char symbol, but only one.
Post 28 Jun 2010, 23:39
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 29 Jun 2010, 04:12
I would guess this rect is local to a procedure?

If so...
Code:
        mov     esi,[hTextBuff] 
        mov     byte[esi],'a' 
        mov     byte[esi+1],'b' 
        mov     byte[esi+2],0
        lea     edx,[rect]
        invoke  DrawText,ebx,esi,-1,edx,DT_NOCLIP
    


Also, what does DrawText return?
Post 29 Jun 2010, 04:12
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 29 Jun 2010, 04:43
Should not "VirtualUnlock" be used - as i know "mov [hTextBuff],eax" is a pointer to a record the system takes care of? I must confess that always using HeapRe/Alloc/Free/ thus do not know sure in your case.
Post 29 Jun 2010, 04:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20308
Location: In your JS exploiting you and your system
revolution 29 Jun 2010, 05:25
Teehee: Post all your code. We can't help you with such limited information.
Post 29 Jun 2010, 05:25
View user's profile Send private message Visit poster's website Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 29 Jun 2010, 07:22
Code:
        invoke  VirtualAlloc,0,4*1024,MEM_RESERVE+MEM_COMMIT,PAGE_READWRITE
        mov     [hTextBuff],eax                       ;check eax b4 this for error

        ...

        mov     esi,[hTextBuff]                       ; lea     esi,[hTextBuff]  is wrong

        mov     byte[esi],'a'                         ; or just    mov     dword[esi],'ab' ;equivalent to 'ab',0,0
        mov     byte[esi+1],'b'                       ;
        mov     byte[esi+2],0                         ;

        ...
                                                      ; esi ebx and rect set correctly before call
                                                      ;            mov     esi,[hTextBuff]
                                                      ;            mov     ebx,[hDC]    ; ? whatever your DC is called
        invoke  DrawText,ebx,esi,-1,rect,DT_NOCLIP    ;

        ...

        rect    dd 0,0,40,20                          ; Text area 40 long x 20 high from top left corner (0,0)     
Post 29 Jun 2010, 07:22
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 29 Jun 2010, 07:43
bitshifter Razz wrote:

Also, what does DrawText return?

http://msdn.microsoft.com/en-us/library/dd162498%28VS.85%29.aspx
Post 29 Jun 2010, 07:43
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 29 Jun 2010, 08:51
^ I don't think bitshifter was asking Teehee about the API's return values in general, but rather he was asking Teehee to add a cmp after invoking the API to get the return value and check if the API is successful or not. Wink


Last edited by ManOfSteel on 29 Jun 2010, 08:52; edited 1 time in total
Post 29 Jun 2010, 08:51
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 29 Jun 2010, 08:52
LOL Razz

no, i meant during runtime...
Post 29 Jun 2010, 08:52
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 29 Jun 2010, 12:54
here the entire code:
Code:
format PE GUI 4.0
entry start

include 'win32w.inc'

section '.text' code readable executable

  start:

        ; main window regclass

        invoke  GetModuleHandle,0
        mov     [hInstance],eax
        mov     [wc.hInstance],eax
        invoke  LoadIcon,0,IDI_APPLICATION
        mov     [wc.hIcon],eax
        invoke  LoadCursor,0,IDC_ARROW
        mov     [wc.hCursor],eax
        invoke  RegisterClass,wc
        test    eax,eax
        jz      error

        ; CodeBox regclass

        invoke  LoadCursor,0,IDC_IBEAM
        mov     [wc.hCursor],eax
        mov     [wc.style],CS_GLOBALCLASS+CS_DBLCLKS
        mov     [wc.lpfnWndProc],CodeBoxProc
        mov     [wc.cbWndExtra],4
        mov     [wc.hbrBackground],1
        mov     [wc.lpszClassName],_codebox_class
        invoke  RegisterClass,wc
        test    eax,eax
        jz      error

        ; center window

        invoke  GetSystemMetrics,SM_CXSCREEN
        sub     eax,600
        shr     eax,1
        push    eax
        invoke  GetSystemMetrics,SM_CYSCREEN
        sub     eax,700
        shr     eax,1
        pop     ebx

        ; create main window

        invoke  CreateWindowEx,0,_mainWnd_class,_mainWnd_title,WS_VISIBLE+WS_SYSMENU,ebx,eax,600,700,NULL,NULL,[hInstance],NULL
        test    eax,eax
        jz      error

        ; main loop

  msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        cmp     eax,1
        jb      end_loop
        jne     msg_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop

  error:
        invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

  end_loop:
        invoke  ExitProcess,[msg.wParam]


; ===================== MainWindowProc  ===============================

proc MainWindowProc hwnd,wmsg,wparam,lparam
        push    ebx esi edi
        mov     eax,[wmsg]
        cmp     eax,WM_SETFOCUS
        je      .wm_setfocus
        cmp     eax,WM_CREATE
        je      .wm_create
        cmp     eax,WM_DESTROY
        je      .wm_destroy
  .defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .ret1

  .wm_setfocus:
        invoke  SetFocus,[hCodeBox]
        jmp     .ret0

  .wm_create:

        ; create CodeBox font
                                                                                                                                      ;FF_DONTCARE
        invoke  CreateFont,16,0,0,0,0,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_RASTER_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH+FF_MODERN,_CourierNewFont
        mov     [hCodeFont],eax

        ; create CodeBox

        invoke  CreateWindowEx,0,_codebox_class,NULL,WS_CHILD+WS_VISIBLE+WS_HSCROLL+WS_VSCROLL+ES_NOHIDESEL,0,0,400,450,[hwnd],NULL,[hInstance],NULL
        mov     [hCodeBox],eax

        ; init text buffer

        invoke  VirtualAlloc,0,4*1024,MEM_RESERVE+MEM_COMMIT,PAGE_READWRITE
        mov     [hTextBuff],eax

        jmp     .ret0

  .wm_destroy:
        invoke  VirtualFree,[hTextBuff],4*1024,MEM_RELEASE
        invoke  PostQuitMessage,0

  .ret0:xor     eax, eax
  .ret1:pop     edi esi ebx
        ret
endp


; ===================== CodeBoxProc ===============================

proc CodeBoxProc hwnd,wmsg,wparam,lparam
        push    ebx esi edi
        cmp     [wmsg],WM_PAINT
        je      .cb_paint
        cmp     [wmsg],WM_CHAR
        je      .cb_char
        cmp     [wmsg],WM_DESTROY
        je      .cb_destroy
  .defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .ret1

  .cb_paint:
        invoke  BeginPaint,[hwnd],codeBoxPS
        mov     ebx,eax ; eax = hDC
        invoke  SelectObject,ebx,[hCodeFont]
        mov     esi,[hTextBuff]
        mov     byte[esi],'a'
        mov     byte[esi+1],'b'
        mov     byte[esi+2],0
        invoke  DrawText,ebx,esi,-1,rect,DT_NOCLIP
        ;invoke  TextOut,ebx, 0, 0, _mainWnd_title, 7 ; size
        invoke  EndPaint,[hwnd],codeBoxPS
        jmp     .ret0

  .cb_char:
        mov     eax,[wparam]  ; char code
        ;lea     edi,[hTextBuff]
        ;mov     byte[edi],'a'
        ;mov     byte[edi+1],'b'
        ;mov     byte[edi+2],0
        jmp     .ret0

  .cb_destroy:
        invoke  PostQuitMessage,0
  .ret0:xor     eax, eax
  .ret1:pop     edi esi ebx
        ret
endp



section '.data' data readable writeable

  wc  WNDCLASS 0,MainWindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_mainWnd_class
  msg MSG
  codeBoxPS  PAINTSTRUCT
  rect       RECT 0,0,100,100

  hInstance dd ?
  hCodeFont dd ?
  hCodeBox  dd ?
  hTextBuff dd ?
  char_id   dd 0

  _mainWnd_class TCHAR 'FASMIDE',0
  _mainWnd_title TCHAR 'fasmIDE',0
  _codebox_class TCHAR 'CODEBOX',0
  _CourierNewFont TCHAR 'Courier New',0

  _error TCHAR 'Startup failed.',0


section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL',\
          comctl32,'COMCTL32.DLL',shell32,'SHELL32.DLL'

  include 'api\kernel32.inc'
  include 'api\user32.inc'
  include 'api\gdi32.inc'
  include 'api\comctl32.inc'
  include 'api\SHELL32.inc'
    

_________________
Sorry if bad english.
Post 29 Jun 2010, 12:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20308
Location: In your JS exploiting you and your system
revolution 29 Jun 2010, 13:13
Here is the problem:
Code:
include 'win32w.inc'
;...
        mov     byte[esi],'a'
        mov     byte[esi+1],'b'
        mov     byte[esi+2],0
    
You selected UNICODE as the API calls, but your text is ASCII.
Post 29 Jun 2010, 13:13
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 29 Jun 2010, 14:55
Shocked thanks rev.

how that unicode code works? it uses different char values?
Post 29 Jun 2010, 14:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20308
Location: In your JS exploiting you and your system
revolution 29 Jun 2010, 15:02
UNICODE uses 16-bit characters.

http://en.wikipedia.org/wiki/Unicode
Post 29 Jun 2010, 15:02
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 29 Jun 2010, 15:09
I'll take this topic to ask how can I map mouse X,Y position to character ID, since character buffer is linear, and not only this, the text is not displayed evenly.
Post 29 Jun 2010, 15:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20308
Location: In your JS exploiting you and your system
revolution 29 Jun 2010, 17:02
Read up on GetTextExtentPoint32
Post 29 Jun 2010, 17:02
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 04 Jul 2010, 19:34
i don't know how that can help me, it only returns letter_width_in_pixels*string_size and letter_height_inpixels Sad
Post 04 Jul 2010, 19:34
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 04 Jul 2010, 22:35
The img below ilustrates what i need to do: map X,Y mouse click to character index. But i don't know how.

Legends:

  • 19 is the cliked character index (red filled cell).*
  • D is a <enter> char representation.
  • 32 is the max caracteres showned by line.
  • 12 is the max lines showned.
  • buffer has length 24 + 1
  • I'm assuming each letter has 8x16 in pixels.


*Clicking there must return the space char ' '.


Description:
Filesize: 894 Bytes
Viewed: 7214 Time(s)

imagem.PNG



_________________
Sorry if bad english.
Post 04 Jul 2010, 22:35
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 04 Jul 2010, 23:42
Teehee wrote:
I'm assuming each letter has 8x16 in pixels.

Are you using a monospace font? If not, you can't even assume two different characters are the same size.
Post 04 Jul 2010, 23:42
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 04 Jul 2010, 23:54
yep, Courier New.
Post 04 Jul 2010, 23:54
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.