Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
801 connectés 

  FORUM HardWare.fr
  Programmation
  C

  [C/GTK+]Ggetip , problème GLib-ERROR

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C/GTK+]Ggetip , problème GLib-ERROR

n°1180132
psyphi
Posté le 20-08-2005 à 12:19:43  profilanswer
 

Bon voila je débute à peine en GTK, j'ai fait une petite application qui retourne l'ip à partir d'un hostname. Voici le code, tout se compile avec qques warning, je pense que j'ai des problèmes de pointeurs, notamment avec char * hostname, mais je n'arrive pas à les corriger. D'autre part j'ai un bug, si on rentre un nom d'host qu'on appuis sur le bouton, qu'on sélectionne le nom d'host et qu'on fait CTRL+C CTRL+V j'ai un popup qui apparait et qui dit: "GLib-ERROR **: gmem.c:174 failed to allocate 32 bytes aborting..." Si vous pourriez m'éclairé la dessus. Sinon si vous avez des remarques dites!
PS: Bien entendu cela nécessite le module GTK+ (dispo ici pour win: /http://www.gtk-fr.org/wakka.php?wiki=Download)
le main.c

Code :
  1. /*
  2. * program name : Ggetip v 1.0
  3. * description : Retourne l'ip à partir d'un nom d'host
  4. * author : psyphi
  5. * contact : psyphi@gmail.com
  6. * last update : August 12 2005
  7. * compile : gcc -o Ggetip main.c (use -lwsock32 for windows)
  8. */
  9. #include <gtk/gtk.h>
  10. #include <stdlib.h>
  11. #if defined (WIN32)
  12. #include <winsock2.h>
  13. #pragma comment(lib, "ws2_32.lib" )
  14. #else
  15. #include <unistd.h>
  16. #include <sys/socket.h> /* pour avoir AF_INET */
  17. #include <netinet/in.h> /* pour inet_ntoa() */
  18. #include <netdb.h> /* pour gethostbyname() & struct hostent */
  19. #include <sys/types.h>
  20. #include <arpa/inet.h>
  21. #endif
  22. #define INTRO "<span font_family=\"verdana\"><b>Ggetip v 1.0</b> <i>August 2005</i>\nBy: psyphi@gmail.com</span>"
  23. #define NOTICE "Entrer IP or Hostname:"
  24. /* Prototypes des fonctions */
  25. void OnUpdate(GtkWidget *pEntry, gpointer data);
  26. char * get_ip(struct hostent * host);
  27. /* Fonction qui retourne l'ip */
  28. char * get_ip(struct hostent * host)
  29. {
  30.   return inet_ntoa(*((struct in_addr * )host->h_addr));
  31. }
  32. struct _MainWindow
  33. {
  34.   /*
  35.   * Composants de la fenêtre principale
  36.   */
  37.   GtkWidget * pWindow; // Fenêtre
  38.   GtkWidget * pVBox; // Boite verticale qui va contenir les Labels et boutons
  39.   GtkWidget * pLabelIntro; // Texte d'intro
  40.   GtkWidget * pLabelNotice; // Texte de notice
  41.   GtkWidget * pEntryHost; // Zone de saisie de l'host
  42.   GtkWidget * pButtonScan; // Bouton de scan
  43.   GtkWidget * pLabelInfoHost; // Texte retournant l'ip
  44. };
  45. typedef struct _MainWindow MainWindow;
  46. int main(int argc, char * argv[])
  47. {
  48.   MainWindow * pApp;
  49.    gtk_init(&argc, &argv);
  50.   pApp = g_malloc(sizeof(MainWindow));
  51.   /*
  52.   * Propriétées de la fenêtre
  53.   */
  54.    pApp->pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); // Création de la fenêtre
  55.   gtk_window_set_position(GTK_WINDOW(pApp->pWindow),GTK_WIN_POS_CENTER); // Définition de la position
  56.   gtk_window_set_default_size(GTK_WINDOW(pApp->pWindow),320,200); // Définition de la taille de la fenetre
  57.   gtk_window_set_title(GTK_WINDOW(pApp->pWindow),"Ggetip" ); // Titre de la fenêtre
  58.   /*
  59.   *  Propriétées de la VBox
  60.   */
  61.   pApp->pVBox = gtk_vbox_new(TRUE, 0); // Création de la VBox
  62.   gtk_container_add(GTK_CONTAINER(pApp->pWindow),pApp->pVBox); // Ajout de la GtkVBox dans la fenêtre
  63.   /*
  64.   * Propriétées du label d'intro
  65.   */
  66.   pApp->pLabelIntro=gtk_label_new(INTRO);
  67.   gtk_label_set_use_markup(pApp->pLabelIntro,TRUE); // On utilise les balises de formatage Pango
  68.   gtk_box_pack_start(GTK_BOX(pApp->pVBox),pApp->pLabelIntro,FALSE,FALSE,0); // On ajoute le label a l'interieur de la VBox
  69.   /*
  70.   * Propriétées du label de notice
  71.   */
  72.   pApp->pLabelNotice=gtk_label_new(NOTICE);
  73.   gtk_box_pack_start(GTK_BOX(pApp->pVBox),pApp->pLabelNotice,FALSE,FALSE,0);
  74.   /*
  75.   * Propriétées de la zone de saisie de l'host
  76.   */
  77.   pApp->pEntryHost=gtk_entry_new(); // On crée la zone de saisie
  78.   gtk_box_pack_start(GTK_BOX(pApp->pVBox),pApp->pEntryHost,FALSE,FALSE,0); // On l'attache à la VBox
  79.   g_signal_connect(G_OBJECT(pApp->pEntryHost),"activate",G_CALLBACK(OnUpdate),(gpointer) pApp); // Connexion du signal "activate"
  80.   /*
  81.   * Propriétées du bouton de scan
  82.   */
  83.   pApp->pButtonScan=gtk_button_new_with_label("Get IP !" );
  84.   gtk_box_pack_start(GTK_BOX(pApp->pVBox),pApp->pButtonScan,FALSE,FALSE,0);
  85.   g_signal_connect(G_OBJECT(pApp->pButtonScan),"clicked",G_CALLBACK(OnUpdate),(gpointer*) pApp);
  86.   /*
  87.   * Propriétées de la zone d'affichage IP
  88.   */
  89.   pApp->pLabelInfoHost=gtk_label_new(NULL);
  90.   gtk_box_pack_start(GTK_BOX(pApp->pVBox),pApp->pLabelInfoHost,FALSE,FALSE,0);
  91.   /* Connexion du signal "destroy" */
  92.    g_signal_connect(G_OBJECT(pApp->pWindow),"destroy",G_CALLBACK(gtk_main_quit),NULL);
  93.  
  94.   gtk_widget_show_all(pApp->pWindow); // Affichage de la fenêtre
  95.    /* Demarrage de la boucle evenementielle */
  96.   gtk_main();
  97.   g_free(pApp);
  98.    return EXIT_SUCCESS;
  99. }
  100. /* Fonction callback execute lors du signal "activate" */
  101. void OnUpdate(GtkWidget *pEntry, gpointer data)
  102. {
  103.   #if defined(WIN32)
  104.   WSADATA WSAData;
  105.   WSAStartup(MAKEWORD(2,0), &WSAData);
  106.   #endif
  107.   char * hostname;
  108.   struct hostent * host;
  109.   MainWindow * pApp;
  110.  
  111.   /* Recuperation de data */
  112.   pApp = (MainWindow*) data;
  113.   /* Recuperation du texte */
  114.   hostname=gtk_entry_get_text(GTK_ENTRY(pApp->pEntryHost));
  115.   if(strcmp(hostname,"" ))
  116.   {
  117.     host=gethostbyname(hostname);
  118.     if(!host)
  119.     {
  120.       hostname="Can't find host !";
  121.     }
  122.     else
  123.     {
  124.       strncat(hostname,"\nIP: ",5);
  125.       strcat(hostname,get_ip(host)); 
  126.     }
  127.   }
  128.   else
  129.     hostname="Please enter hostname !";
  130.   /* Affichage de l'Host */
  131.   gtk_label_set_text(GTK_LABEL(pApp->pLabelInfoHost),hostname);
  132.   free(hostname);
  133.   #if defined(WIN32)
  134.   WSACleanup();
  135.   #endif
  136. }

mood
Publicité
Posté le 20-08-2005 à 12:19:43  profilanswer
 

n°1180356
jagstang
Pa Capona ಠ_ಠ
Posté le 20-08-2005 à 20:02:30  profilanswer
 

détail des erreurs de compil ?

n°1180409
psyphi
Posté le 20-08-2005 à 23:05:45  profilanswer
 

J'ai mis à jour le Runtime GTK à la version 2_6_8 pour voir si j'avais toujours l'erreur.
Mes warnings de compilations ont disparu mais l'erreur d'éxécution est toujours là.
Le programme se lance bien, il donne l'ip une fois mais dès qu'on recommencer ca plante.

n°1184617
tpierron
Posté le 25-08-2005 à 20:44:40  profilanswer
 

Mouais, au hasard ton problème se situe à ce niveau :
 
hostname=gtk_entry_get_text(GTK_ENTRY(pApp->pEntryHost));
 
Puis plus loin :
 
strncat(hostname,"\nIP: ",5);
strcat(hostname,get_ip(host));    
 
T'as vu où qu'il y avaiy assez de place dans hostname pour ajouter des caractères ? La première fois tu dois avoir une corruption de ton espace mémoire, et la seconde ça plante.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  [C/GTK+]Ggetip , problème GLib-ERROR

 

Sujets relatifs
Probleme Border:solid et background-color sur select sous firefoxprobleme d'export en jar avec eclipse et fichier sonore
Probleme listboxProblème d'accent
probleme de transfert site vers le webProbleme avec un devis qui doit contenir deux actions
[hibernate] Problème avec les requetesprobleme avec ClassPathResource
[RESOLU] probleme compil opengl / glut sous linuxProblème avec exec et la redirection vers un fichier
Plus de sujets relatifs à : [C/GTK+]Ggetip , problème GLib-ERROR


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR