flat assembler
Message board for the users of flat assembler.

Index > Windows > [reopen] simple GTK+ app, but with some weird error.

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 10 Jan 2010, 17:26
hi,
below code (which i retype from fasm board) will show a window,
but after you click the [X] button to close it,
it will produce error.



Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

GTK_WINDOW_TOPLEVEL  = 0

; --------------------------------------
section '.text' code readable executable
; --------------------------------------
proc callback_destroy widget, cbdata
       invoke  gtk_main_quit
               ret
endp

start:
       invoke  gtk_init,NULL,NULL
  invoke  gtk_window_new, GTK_WINDOW_TOPLEVEL
         mov     [winh],eax
  invoke  gtk_signal_connect_full,\
                  [winh],\
                   sigdestroy,\
                       callback_destroy,\
                 NULL,NULL,NULL,NULL,NULL
    invoke  gtk_widget_show_all,[winh]
  invoke  gtk_main
            xor     eax,eax
             ret




; -------------------------------------
section '.data' data readable writeable
; -------------------------------------
winh          dd ?
sigdestroy      db 'destroy',0


; ---------------------------------------------
section '.idata' import data readable writeable
; ---------------------------------------------
library  kernel32,       'KERNEL32.DLL',\
 user32,         'USER32.DLL',\
   libgtk20,       'libgtk-win32-2.0-0.dll'

      include '%fasminc%\api\kernel32.inc'
    include '%fasminc%\api\user32.inc'
      
    import  libgtk20,\
         gtk_init,               'gtk_init',\
             gtk_main_quit,          'gtk_main_quit',\
                gtk_signal_connect_full,'gtk_signal_connect_full',\
              gtk_widget_show_all,    'gtk_widget_show_all',\
          gtk_window_new,         'gtk_window_new',\
               gtk_window_set_title,   'gtk_window_set_title',\
         gtk_main,               'gtk_main'
    


Last edited by sleepsleep on 07 Feb 2014, 09:23; edited 4 times in total
Post 10 Jan 2010, 17:26
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 10 Jan 2010, 17:37
Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

GTK_WINDOW_TOPLEVEL     = 0

; --------------------------------------
section '.text' code readable executable
; --------------------------------------
proc callback_destroy c, widget, cbdata ; NOTE: "c" is not a parameter, it tells proc macro to behave like a cdecl function.
        cinvoke  gtk_main_quit

        ret
endp

start:
        cinvoke  gtk_init,NULL,NULL
        cinvoke  gtk_window_new, GTK_WINDOW_TOPLEVEL
        mov      [winh],eax
        cinvoke  gtk_signal_connect_full,\
                        [winh],\
                        sigdestroy,\
                        callback_destroy,\
                        NULL,NULL,NULL,NULL,NULL
        cinvoke  gtk_widget_show_all,[winh]
        cinvoke  gtk_main
        invoke   ExitProcess, 0




; -------------------------------------
section '.data' data readable writeable
; -------------------------------------
winh            dd ?
sigdestroy      db 'destroy',0


; ---------------------------------------------
section '.idata' import data readable writeable
; ---------------------------------------------
library kernel32,       'KERNEL32.DLL',\
        user32,         'USER32.DLL',\
        libgtk20,       'libgtk-win32-2.0-0.dll'

        include '%fasminc%\api\kernel32.inc'
        include '%fasminc%\api\user32.inc'
        
        import  libgtk20,\
                gtk_init,               'gtk_init',\
                gtk_main_quit,          'gtk_main_quit',\
                gtk_signal_connect_full,'gtk_signal_connect_full',\
                gtk_widget_show_all,    'gtk_widget_show_all',\
                gtk_window_new,         'gtk_window_new',\
                gtk_window_set_title,   'gtk_window_set_title',\
                gtk_main,               'gtk_main'    

You was using stdcall convention in a cdecl library.
Post 10 Jan 2010, 17:37
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 10 Jan 2010, 17:44
oh, thanks.
is there any utilities to detect if a dll export in cdecl or stdcall?
Post 10 Jan 2010, 17:44
View user's profile Send private message Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 10 Jan 2010, 18:00
It's easy to detect if function is stdcall/cdecl - disassemble it and check RET instruction. If it's RET xx, then it's stdcall, otherwise it's likely to be cdecl. IDA recognizes calling conventions rather well.
Post 10 Jan 2010, 18:00
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 10 Jan 2010, 18:15
To confirm my suspicion that the DLL was cdecl, what I did was just stepping over gtk_init in OllyDbg and observing that ESP was not restored to the value previous to the first PUSH.

Note however that all the mess was caused by callback_destroy since ExitProcess was never reached (the stack unbalance in start is not problematic here since you don't have ESP-referenced variables). With the previous XOR EAX, EAX/RET you would had a crash after fixing the callback only of course.
Post 10 Jan 2010, 18:15
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 10 Jan 2010, 18:57
sleepsleep, gtk_signal_connect_full() is deprecated in favor of g_signal_connect_data().
Post 10 Jan 2010, 18:57
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jan 2010, 00:52
thanks guys Smile

Quote:

sleepsleep, gtk_signal_connect_full() is deprecated in favor of g_signal_connect_data().

thanks.
but the g_signal_connect_data() is located in LIBGOBJECT-2.0-0.DLL for stable build.

maybe for current / latest unstable build, they are in libgtk-win32-2.0-0.dll, but i don't know much about using GIT to download the unstable build.

http://www.gtk.org/download.html
Post 11 Jan 2010, 00:52
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jan 2010, 19:51
i was trying to load a glade file (no way to code the GUI part manually)
so, i followed one tutorial.

http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html

his C language code
Code:
#include <gtk/gtk.h>
 
void 
on_window_destroy (GtkObject *object, gpointer user_data)
{
        gtk_main_quit();
}
 
int
main (int argc, char *argv[])
{
        GtkBuilder              *builder;
        GtkWidget               *window;
        
        gtk_init (&argc, &argv);
        
        builder = gtk_builder_new ();
        gtk_builder_add_from_file (builder, "tutorial.xml", NULL);
 
        window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
        gtk_builder_connect_signals (builder, NULL);          
        g_object_unref (G_OBJECT (builder));
        
        gtk_widget_show (window);       
        gtk_main ();
        
        return 0;
}
    


my converted code into fasm
Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

; --------------------------------------
section '.text' code readable executable
; --------------------------------------
proc on_window_destroy c, widget, cbdata
             cinvoke gtk_main_quit
               ret
endp

start:
       ; window creation
   ; ---------------
   cinvoke gtk_init,NULL,NULL

      cinvoke gtk_builder_new
                     mov     [builderh],eax
                      
    cinvoke gtk_builder_add_from_file, eax, tutorial, NULL
                      cmp     eax,0
                       jne     @f
  invoke  MessageBox,NULL,sigdestroy, sigdestroy, MB_OK
       @@:
     cinvoke gtk_builder_get_object, [builderh], windowname
                      mov     [windowh],eax
       cinvoke gtk_builder_connect_signals, [builderh], NULL
       cinvoke g_object_unref, [builderh]
  
    ; main
      ; ----
      cinvoke gtk_widget_show, [windowh]
  cinvoke gtk_main
    invoke  ExitProcess,0
                       ret




; -------------------------------------
section '.data' data readable writeable
; -------------------------------------
windowh               dd ?
builderh        dd ?

sigdestroy  db 'destroy',0

tutorial        db 'tutorial.xml',0
windowname     db 'window',0

; -----------------------------------
section '.edata' export data readable
; -----------------------------------
export      '02.EXE',\
       on_window_destroy,      'on_window_destroy'

; ---------------------------------------------
section '.idata' import data readable writeable
; ---------------------------------------------
library kernel32,       'KERNEL32.DLL',\
 user32,                 'USER32.DLL',\
   libgtk20,               'libgtk-win32-2.0-0.dll',\
       libobj20,               'LIBGOBJECT-2.0-0.DLL'

        include '%fasminc%\api\kernel32.inc'
    include '%fasminc%\api\user32.inc'
      
    import  libgtk20,\
         gtk_init,                                               'gtk_init',\
             gtk_main_quit,                                  'gtk_main_quit',\
                gtk_builder_new,                                'gtk_builder_new',\
              gtk_builder_add_from_file,              'gtk_builder_add_from_file',\
            gtk_builder_get_object,                 'gtk_builder_get_object',\
               gtk_builder_connect_signals,    'gtk_builder_connect_signals',\
          gtk_widget_show,                                'gtk_widget_show',\
              gtk_main,                                               'gtk_main'
        
    import  libobj20,\
         g_object_unref,                                 'g_object_unref'
    


i kept on getting error from the function gtk_builder_add_from_file.
Code:
guint               gtk_builder_add_from_file           (GtkBuilder *builder,
                                                         const gchar *filename,
                                                         GError **error);
    

Parses a file containing a GtkBuilder UI definition and merges it with the current contents of builder.

builder :
a GtkBuilder

filename :
the name of the file to parse

error :
return location for an error, or NULL

Returns :
A positive value on success, 0 if an error occurred

his tutorial.xml file can be downloaded from
http://www.micahcarrick.com/files/gtk-glade-tutorial/part-1/tutorial.glade

any ideas?
Post 11 Jan 2010, 19:51
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jan 2010, 20:42
fucking hell..
i think his XML file is somehow corrupted.

i tried another one here.
it works like heaven.

http://www.gtkforums.com/about4650.html&highlight=gtkbuilderaddfromfile


Description:
Filesize: 9.47 KB
Viewed: 7307 Time(s)

1.PNG


Post 11 Jan 2010, 20:42
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 11 Jan 2010, 20:59
sleepsleep wrote:
oh, thanks.
is there any utilities to detect if a dll export in cdecl or stdcall?

Or, look into C header whether function(s) have "__stdcall" in declaration.
Post 11 Jan 2010, 20:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jan 2010, 21:09
Quote:

Or, look into C header whether function(s) have "__stdcall" in declaration.

ic thanks vid.
so, if no __stdcall, usually it is cdecl right (is it default)?
Post 11 Jan 2010, 21:09
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 11 Jan 2010, 22:26
cdecl is default. However, sometimes people define their own name for all type modifiers, like:

Code:
#define MYAPI __stdcall
... many lines of code ...
void MYAPI SomeFunc();
    


So sometimes it can get more messy.
Post 11 Jan 2010, 22:26
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 12 Jan 2010, 03:22
thanks vid. i think i got it clear this time Smile
Post 12 Jan 2010, 03:22
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 23 Jun 2012, 00:52
just wanna add,

the correct way to exit is

Code:
mov eax,0
leave
ret
    

check with olly debug on a compiled c gtk app.

and regarding the glade file, there are 2 types,
one using gtkbuilder, and another type using libglade.
Post 23 Jun 2012, 00:52
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 07 Feb 2014, 09:02
coming back with GTK,
the example above was using gtk builder file, if you check the xml file,
<signal name="destroy" handler="gtk_main_quit"/>
so the window object destroy event was linked with gtk_main_quit directly, without calling our on_window_destroy c, widget, cbdata.

i got an issue with the following code, the window close, but window os detected error.
i use the following minimal gtk code
Code:
format PE GUI 4.0
entry start

include 'win32ax.inc'

; -------------------------------------------------
section '.data' data readable writeable
; -------------------------------------------------
evdestroy       db 'destroy',0
window1         db 'window1',0
window1h        dd ?



; -------------------------------------------------
section '.code' code readable executable
; -------------------------------------------------
start:
        cinvoke gtk_init,NULL,NULL
        cinvoke gtk_window_new,0
          mov   [window1h],eax
        cinvoke g_signal_connect_data,[window1h],evdestroy,cbSignalDestroy,NULL,NULL,NULL
        cinvoke gtk_widget_show_all,[window1h]
        cinvoke gtk_main
          mov   eax,0
          leave
          ret

proc cbSignalDestroy c, widget, cbdata
        cinvoke gtk_main_quit
          ret
endp

; -------------------------------------------------
section '.idata' import data readable
; -------------------------------------------------
library kernel32,'KERNEL32.DLL',\
        user32,'USER32.DLL',\
        msvcrt,'MSVCRT.DLL',\
        libgtk_3_0, 'libgtk-3-0.DLL',\
        libgobject_2.0_0, 'libgobject-2.0-0.dll'

include 'API\KERNEL32.INC'
include 'API\USER32.INC'
include 'API\MSVCRT32.INC'
include 'libgtk-3-0.inc'
include 'libgobject-2.0-0.inc'
    


files inside this zip, http://goo.gl/1pE47a
libgtk-3-0.inc
libgtk-3-0.dll
libgobject-2.0-0.inc
libgobject-2.0-0.dll

error received
Problem signature:
Problem Event Name: APPCRASH
Application Name: debug1.exe
Application Version: 0.0.0.0
Application Timestamp: 52f4a0c3
Fault Module Name: ntdll.dll
Fault Module Version: 6.1.7601.18229
Fault Module Timestamp: 51fb1072
Exception Code: 4000001f
Exception Offset: 00039f45
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: e8ad
Additional Information 2: e8adce1c2b9e7be834b4063ac3c53863
Additional Information 3: e8ad
Additional Information 4: e8adce1c2b9e7be834b4063ac3c53863

Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
Post 07 Feb 2014, 09:02
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 07 Feb 2014, 09:22
i tried this after i posted above,
Code:
        invoke  ExitProcess,0
          mov   eax,0
          leave
          ret
    

why i need to ExitProcess? i was thinking cinvoke gkt_main_quit should handle all the exit remaining tasks.
Post 07 Feb 2014, 09:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20356
Location: In your JS exploiting you and your system
revolution 07 Feb 2014, 10:19
In Windows the "start" entry point is not officially called as such so a single "ret" is not really supposed to work (although unofficially it does work on all versions of Windows currently available). However that is not the problem you had. The problem is the errant "leave" with no associated "enter" (or its equivalent).

Also after invoking ExitProcess any code put afterwards is redundant and never executed.
Post 07 Feb 2014, 10:19
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12846
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 07 Feb 2014, 10:28
thanks revolution,
so the last line gtk application must be invoke ExitProcess,0
no more mov eax,0 leave ret

=) thanks.
Post 07 Feb 2014, 10:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.