Lost in the matrix

C/C++/C#/Java, Multithreading

Ceci est une version très basique de la fonction Ping.
Elle ne gère pas tous les cas, mais je vais poster une version améliorée bientôt.

#include <winsock2.h>
#include <Ipexport.h>
#include <icmpapi.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

int IsPingable(const char *host, int timeout)
{
 IP_OPTION_INFORMATION ioi;
 ICMP_ECHO_REPLY       ier;
 LPHOSTENT             Host;
 WSADATA               wsaData;
 IN_ADDR               iaAddr;
 HANDLE                hIcmp;

 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
 {
  WSACleanup();
  return (0);
 }
 iaAddr.s_addr = inet_addr(host);
 if (iaAddr.s_addr == INADDR_NONE)
  Host = gethostbyname(host);
 else
  Host = gethostbyaddr((char *)&iaAddr, sizeof(IN_ADDR), AF_INET);
 if (Host == NULL)
 {
  WSACleanup();
  return (0);
 }
 hIcmp = IcmpCreateFile();
 ioi.Ttl = 255;
 ioi.Tos = 0;
 ioi.Flags = 0;
 ioi.OptionsSize = 0;
 ioi.OptionsData = NULL;
 IcmpSendEcho(hIcmp, *(DWORD *)(*Host->h_addr_list), NULL, 0, &ioi, &ier, sizeof(ICMP_ECHO_REPLY), timeout);
 IcmpCloseHandle(hIcmp);
 WSACleanup();
 return (ier.Status == 0);
}
Exemple:
int main(int argc, char **argv)
{
 printf("IsPingable : %d\n", IsPingable("clustrmaps.com", 5000));
 getchar();
 return (EXIT_SUCCESS);
}

Aujourd'hui, je suis tombé sur un problème avec le contrôle PropertyGrid, car je voulais éditer les propriétés d'un objet ainsi que ses sous-objets. Toutefois, j'ai eu quelques soucis, car la PropertyGrid ne permet pas (de base) d'éditer les sous-objets et ceux-ci apparaissent grisés.

Voici mon code:

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class PropertyGridTest : Form
    {
        public PropertyGridTest()
        {
            InitializeComponent();
            this.propertyGrid1.SelectedObject = new Car();
        }
    }

    public class Driver
    {
        public string Name { get; set; }
        public bool HasDriverLicence { get; set; }
    }

    public class Car
    {
        public Car()
        {
            this.Driver = new Driver()
            {
                Name = "Johannes Fetz",
                HasDriverLicence = false
            };
            this.Color = Color.Red;
            this.Kind = "Sedan";
        }

        public string Kind { get; set; }
        public Color Color { get; set; }
        public Driver Driver { get; set; }
    }
}
qui donne le résultat suivant:
Après quelques recherches, j'ai trouvé comment résoudre ce problème. En effet, il suffit d'indiquer à la PropertyGrid que "Driver" est un objet "Expandable". Pour cela, il faut ajouter un using :
using System.ComponentModel;
et un attribut au dessus de la classe "Driver" :
[TypeConverterAttribute(typeof(ExpandableObjectConverter))]
public class Driver
{
Ce qui donne le résultat suivant: