Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

TELNET EN C AVEC INTERFACE GRAPHIQUE (API UNIQUEMENT, SANS MFC NI AUTRES COCHONNERIES)


Information sur la source

Catégorie :API Niveau : Débutant Date de création : 11/05/2003 Date de mise à jour : 13/05/2003 10:30:27 Vu : 5 759

Note :
8,5 / 10 - par 4 personnes
8,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (13)
Ajouter un commentaire et/ou une note

Description

Cliquez pour voir la capture en taille normale
Bon, 2e tentative...

Je disais donc que cette source fait suite à mon ancien telnet console... Celui-ci dispose d'une GUI et permet d'envoyer des requêtes HTTP en une seule fois (ne pas oublier d'appuyer sur entrée après chaque ligne dans le memo d'envoi, le prog ne rajoute pas les CRLF...)
 

Source

  • #include <windows.h>
  • #include <winsock2.h>
  • #define WND_CLASS "GWIN32_TELNET"
  • #define IDB_CONNECT 1
  • #define IDB_DISCONNECT 2
  • #define IDB_SEND 3
  • #define IDE_HOST 1
  • #define IDE_PORT 2
  • #define IDE_COMMAND 3
  • HINSTANCE hMain = NULL;
  • HFONT hFont = NULL;
  • HWND wndMain = NULL;
  • HWND stcHost = NULL;
  • HWND edtHost = NULL;
  • HWND stcPort = NULL;
  • HWND edtPort = NULL;
  • HWND btnConnect = NULL;
  • HWND btnDisconnect = NULL;
  • HWND stcTransfer = NULL;
  • HWND edtTransfer = NULL;
  • HWND stcCommand = NULL;
  • HWND edtCommand = NULL;
  • HWND btnSend = NULL;
  • WSADATA WSAData;
  • char* host = NULL;
  • char* port = NULL;
  • BOOL bWinsock = FALSE;
  • CRITICAL_SECTION lock;
  • SOCKADDR_IN ClientSock;
  • SOCKET Client = INVALID_SOCKET;
  • HOSTENT* ServerInfos = NULL;
  • HANDLE RecvThread = NULL;
  • DWORD RecvThreadID = 0;
  • DWORD RecvThreadExitCode = 0;
  • void AddLine(char* c, BOOL r) {
  • int n = 0;
  • EnterCriticalSection(&lock);
  • n = SendMessage(edtTransfer, WM_GETTEXTLENGTH, 0, 0) + 1;
  • SendMessage(edtTransfer, EM_SETSEL, n, n);
  • SendMessage(edtTransfer, EM_REPLACESEL, TRUE, (LPARAM) c);
  • if (r) SendMessage(edtTransfer, EM_REPLACESEL, TRUE, (LPARAM) "\r\n");
  • LeaveCriticalSection(&lock);
  • UpdateWindow(wndMain);
  • }
  • void ResetControls(BOOL FromThread) {
  • EnableWindow(edtTransfer, TRUE);
  • if (RecvThread) {
  • GetExitCodeThread(RecvThread, &RecvThreadExitCode);
  • if (RecvThreadExitCode == STILL_ACTIVE && !FromThread) TerminateThread(RecvThread, 0);
  • CloseHandle(RecvThread);
  • RecvThread = NULL;
  • closesocket(Client);
  • Client = INVALID_SOCKET;
  • SetFocus(edtTransfer);
  • }
  • if (host) {
  • free(host);
  • host = NULL;
  • }
  • if (port) {
  • free(port);
  • port = NULL;
  • }
  • EnableWindow(stcHost, bWinsock);
  • EnableWindow(edtHost, bWinsock);
  • EnableWindow(stcPort, bWinsock);
  • EnableWindow(edtPort, bWinsock);
  • EnableWindow(btnConnect, bWinsock);
  • EnableWindow(btnDisconnect, FALSE);
  • EnableWindow(stcTransfer, FALSE);
  • EnableWindow(stcCommand, FALSE);
  • EnableWindow(edtCommand, FALSE);
  • EnableWindow(btnSend, FALSE);
  • UpdateWindow(wndMain);
  • }
  • DWORD WINAPI ClientRecv(void* arg) {
  • int n = 0;
  • char c = 0;
  • char* line = NULL;
  • while (recv(Client, &c, 1, 0) > 0) {
  • line = (char*) realloc(line, n + 1);
  • line[n++] = c;
  • if (c != '\n') continue;
  • line = (char*) realloc(line, n + 1);
  • line[n++] = 0;
  • AddLine(line, FALSE);
  • free(line);
  • line = NULL;
  • n = 0;
  • }
  • AddLine("\r\n\r\nDéconnecté du serveur.", TRUE);
  • ResetControls(TRUE);
  • return 0;
  • }
  • LRESULT CALLBACK wndMainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  • int n = 0;
  • char* line = NULL;
  • switch (uMsg) {
  • case WM_ACTIVATE:
  • if (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) SetFocus(edtCommand);
  • return 0;
  • case WM_COMMAND:
  • switch (HIWORD(wParam)) {
  • case EN_CHANGE:
  • switch (LOWORD(wParam)) {
  • case IDE_HOST:
  • EnableWindow(btnConnect, bWinsock && SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) > 0 && SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) > 0);
  • break;
  • case IDE_PORT:
  • EnableWindow(btnConnect, bWinsock && SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) > 0 && SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) > 0);
  • break;
  • case IDE_COMMAND:
  • EnableWindow(btnSend, SendMessage(edtCommand, WM_GETTEXTLENGTH, 0, 0) > 0);
  • break;
  • }
  • break;
  • case BN_CLICKED:
  • switch (LOWORD(wParam)) {
  • case IDB_CONNECT:
  • EnableWindow(stcHost, FALSE);
  • EnableWindow(edtHost, FALSE);
  • EnableWindow(stcPort, FALSE);
  • EnableWindow(edtPort, FALSE);
  • EnableWindow(btnConnect, FALSE);
  • n = SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) + 1;
  • host = (char*) malloc(n);
  • SendMessage(edtHost, WM_GETTEXT, n, (LPARAM) host);
  • n = SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) + 1;
  • port = (char*) malloc(n);
  • SendMessage(edtPort, WM_GETTEXT, n, (LPARAM) port);
  • SendMessage(edtTransfer, WM_SETTEXT, 0, (LPARAM) "");
  • AddLine("Recherche du serveur... ", FALSE);
  • ServerInfos = gethostbyname(host);
  • if (!ServerInfos) {
  • AddLine("erreur", TRUE);
  • ResetControls(FALSE);
  • break;
  • }
  • AddLine("ok", TRUE);
  • memset(&ClientSock, 0, sizeof(SOCKADDR_IN));
  • memcpy(&ClientSock.sin_addr.s_addr, ServerInfos->h_addr, ServerInfos->h_length);
  • ClientSock.sin_port = htons(atoi(port));
  • ClientSock.sin_family = AF_INET;
  • Client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  • AddLine("Connexion au serveur... ", FALSE);
  • if (connect(Client, (SOCKADDR*) &ClientSock, sizeof(SOCKADDR_IN))) {
  • AddLine("erreur", TRUE);
  • ResetControls(FALSE);
  • break;
  • }
  • SendMessage(edtTransfer, WM_SETTEXT, 0, (LPARAM) "");
  • EnableWindow(btnDisconnect, TRUE);
  • EnableWindow(stcTransfer, TRUE);
  • EnableWindow(edtTransfer, TRUE);
  • EnableWindow(stcCommand, TRUE);
  • EnableWindow(edtCommand, TRUE);
  • EnableWindow(btnSend, TRUE);
  • SetFocus(edtCommand);
  • UpdateWindow(wndMain);
  • RecvThread = CreateThread(NULL, 0, &ClientRecv, &Client, 0, &RecvThreadID);
  • break;
  • case IDB_DISCONNECT:
  • ResetControls(FALSE);
  • break;
  • case IDB_SEND:
  • SetFocus(edtCommand);
  • n = SendMessage(edtCommand, WM_GETTEXTLENGTH, 0, 0) + 1;
  • line = (char*) malloc(n);
  • SendMessage(edtCommand, WM_GETTEXT, n, (LPARAM) line);
  • SendMessage(edtCommand, WM_SETTEXT, 0, (LPARAM) "");
  • AddLine("=> ", FALSE);
  • AddLine(line, FALSE);
  • send(Client, line, n - 1, 0);
  • free(line);
  • UpdateWindow(wndMain);
  • break;
  • }
  • break;
  • }
  • return 0;
  • case WM_CLOSE:
  • PostQuitMessage(0);
  • return 0;
  • default:
  • return DefWindowProc(hWnd, uMsg, wParam, lParam);
  • }
  • }
  • int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  • MSG msg;
  • WNDCLASSEX wc;
  • hMain = hInstance;
  • wc.cbSize = sizeof(WNDCLASSEX);
  • wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  • wc.lpfnWndProc = wndMainProc;
  • wc.cbClsExtra = 0;
  • wc.cbWndExtra = 0;
  • wc.hInstance = hMain;
  • wc.hIcon = LoadIcon(hMain, "MAINICON");
  • wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  • wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  • wc.lpszMenuName = NULL;
  • wc.lpszClassName = WND_CLASS;
  • wc.hIconSm = NULL;
  • RegisterClassEx(&wc);
  • InitializeCriticalSection(&lock);
  • hFont = CreateFont(8, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "MS Sans Serif");
  • wndMain = CreateWindowEx(0, WND_CLASS, "Telnet Win32", WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 460, 540, HWND_DESKTOP, NULL, hMain, NULL);
  • SendMessage(wndMain, WM_SETFONT, (WPARAM) hFont, TRUE);
  • stcHost = CreateWindowEx(0, "STATIC", "Hôte :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 10, 30, 14, wndMain, NULL, hMain, NULL);
  • SendMessage(stcHost, WM_SETFONT, (WPARAM) hFont, TRUE);
  • edtHost = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "127.0.0.1", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_LOWERCASE, 46, 6, 300, 21, wndMain, (HMENU) IDE_HOST, hMain, NULL);
  • SendMessage(edtHost, WM_SETFONT, (WPARAM) hFont, TRUE);
  • stcPort = CreateWindowEx(0, "STATIC", "Port :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 35, 30, 14, wndMain, NULL, hMain, NULL);
  • SendMessage(stcPort, WM_SETFONT, (WPARAM) hFont, TRUE);
  • edtPort = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "23", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_LOWERCASE, 46, 31, 300, 21, wndMain, (HMENU) IDE_PORT, hMain, NULL);
  • SendMessage(edtPort, WM_SETFONT, (WPARAM) hFont, TRUE);
  • btnConnect = CreateWindowEx(0, "BUTTON", "Connexion", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 6, 80, 21, wndMain, (HMENU) IDB_CONNECT, hMain, NULL);
  • SendMessage(btnConnect, WM_SETFONT, (WPARAM) hFont, TRUE);
  • btnDisconnect = CreateWindowEx(0, "BUTTON", "Déconnexion", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 31, 80, 21, wndMain, (HMENU) IDB_DISCONNECT, hMain, NULL);
  • SendMessage(btnDisconnect, WM_SETFONT, (WPARAM) hFont, TRUE);
  • stcTransfer = CreateWindowEx(0, "STATIC", "Transferts :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 60, 60, 14, wndMain, NULL, hMain, NULL);
  • SendMessage(stcTransfer, WM_SETFONT, (WPARAM) hFont, TRUE);
  • edtTransfer = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_READONLY | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 75, 431, 300, wndMain, NULL, hMain, NULL);
  • SendMessage(edtTransfer, WM_SETFONT, (WPARAM) hFont, TRUE);
  • stcCommand = CreateWindowEx(0, "STATIC", "Requête :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 380, 60, 14, wndMain, NULL, hMain, NULL);
  • SendMessage(stcCommand, WM_SETFONT, (WPARAM) hFont, TRUE);
  • edtCommand = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 398, 431, 104, wndMain, (HMENU) IDE_COMMAND, hMain, NULL);
  • SendMessage(edtCommand, WM_SETFONT, (WPARAM) hFont, TRUE);
  • btnSend = CreateWindowEx(0, "BUTTON", "Envoyer", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 376, 80, 21, wndMain, (HMENU) IDB_SEND, hMain, NULL);
  • SendMessage(btnSend, WM_SETFONT, (WPARAM) hFont, TRUE);
  • AddLine("Initialisation de winsock... ", FALSE);
  • bWinsock = !WSAStartup(MAKEWORD(2, 2), &WSAData);
  • if (bWinsock) AddLine("ok", TRUE);
  • else AddLine("erreur", TRUE);
  • ResetControls(FALSE);
  • ShowWindow(wndMain, SW_SHOW);
  • while (GetMessage(&msg, NULL, 0, 0)) {
  • TranslateMessage(&msg);
  • DispatchMessage(&msg);
  • }
  • ResetControls(FALSE);
  • DestroyWindow(wndMain);
  • DeleteCriticalSection(&lock);
  • DeleteObject(hFont);
  • WSACleanup();
  • return msg.wParam;
  • }
#include <windows.h>
#include <winsock2.h>

#define WND_CLASS "GWIN32_TELNET"

#define IDB_CONNECT 1
#define IDB_DISCONNECT 2
#define IDB_SEND 3

#define IDE_HOST 1
#define IDE_PORT 2
#define IDE_COMMAND 3

HINSTANCE hMain = NULL;
HFONT hFont = NULL;

HWND wndMain = NULL;
HWND stcHost = NULL;
HWND edtHost = NULL;
HWND stcPort = NULL;
HWND edtPort = NULL;
HWND btnConnect = NULL;
HWND btnDisconnect = NULL;
HWND stcTransfer = NULL;
HWND edtTransfer = NULL;
HWND stcCommand = NULL;
HWND edtCommand = NULL;
HWND btnSend = NULL;

WSADATA WSAData;
char* host = NULL;
char* port = NULL;
BOOL bWinsock = FALSE;
CRITICAL_SECTION lock;
SOCKADDR_IN ClientSock;
SOCKET Client = INVALID_SOCKET;
HOSTENT* ServerInfos = NULL;
HANDLE RecvThread = NULL;
DWORD RecvThreadID = 0;
DWORD RecvThreadExitCode = 0;

void AddLine(char* c, BOOL r) {
  int n = 0;
  EnterCriticalSection(&lock);
  n = SendMessage(edtTransfer, WM_GETTEXTLENGTH, 0, 0) + 1;
  SendMessage(edtTransfer, EM_SETSEL, n, n);
  SendMessage(edtTransfer, EM_REPLACESEL, TRUE, (LPARAM) c);
  if (r) SendMessage(edtTransfer, EM_REPLACESEL, TRUE, (LPARAM) "\r\n");
  LeaveCriticalSection(&lock);
  UpdateWindow(wndMain);
}

void ResetControls(BOOL FromThread) {
  EnableWindow(edtTransfer, TRUE);
  if (RecvThread) {
    GetExitCodeThread(RecvThread, &RecvThreadExitCode);
    if (RecvThreadExitCode == STILL_ACTIVE && !FromThread) TerminateThread(RecvThread, 0);
    CloseHandle(RecvThread);
    RecvThread = NULL;
    closesocket(Client);
    Client = INVALID_SOCKET;
    SetFocus(edtTransfer);
  }
  if (host) {
    free(host);
    host = NULL;
  }
  if (port) {
    free(port);
    port = NULL;
  }
  EnableWindow(stcHost, bWinsock);
  EnableWindow(edtHost, bWinsock);
  EnableWindow(stcPort, bWinsock);
  EnableWindow(edtPort, bWinsock);
  EnableWindow(btnConnect, bWinsock);
  EnableWindow(btnDisconnect, FALSE);
  EnableWindow(stcTransfer, FALSE);
  EnableWindow(stcCommand, FALSE);
  EnableWindow(edtCommand, FALSE);
  EnableWindow(btnSend, FALSE);
  UpdateWindow(wndMain);
}

DWORD WINAPI ClientRecv(void* arg) {
  int n = 0;
  char c = 0;
  char* line = NULL;
  while (recv(Client, &c, 1, 0) > 0) {
    line = (char*) realloc(line, n + 1);
    line[n++] = c;
    if (c != '\n') continue;
    line = (char*) realloc(line, n + 1);
    line[n++] = 0;
    AddLine(line, FALSE);
    free(line);
    line = NULL;
    n = 0;
  }
  AddLine("\r\n\r\nDéconnecté du serveur.", TRUE);
  ResetControls(TRUE);
  return 0;
}

LRESULT CALLBACK wndMainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  int n = 0;
  char* line = NULL;
  switch (uMsg) {
  case WM_ACTIVATE:
    if (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) SetFocus(edtCommand);
    return 0;
  case WM_COMMAND:
    switch (HIWORD(wParam)) {
    case EN_CHANGE:
      switch (LOWORD(wParam)) {
      case IDE_HOST:
        EnableWindow(btnConnect, bWinsock && SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) > 0 && SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) > 0);
        break;
      case IDE_PORT:
        EnableWindow(btnConnect, bWinsock && SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) > 0 && SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) > 0);
        break;
      case IDE_COMMAND:
        EnableWindow(btnSend, SendMessage(edtCommand, WM_GETTEXTLENGTH, 0, 0) > 0);
        break;
      }
      break;
    case BN_CLICKED:
      switch (LOWORD(wParam)) {
      case IDB_CONNECT:
        EnableWindow(stcHost, FALSE);
        EnableWindow(edtHost, FALSE);
        EnableWindow(stcPort, FALSE);
        EnableWindow(edtPort, FALSE);
        EnableWindow(btnConnect, FALSE);
        n = SendMessage(edtHost, WM_GETTEXTLENGTH, 0, 0) + 1;
        host = (char*) malloc(n);
        SendMessage(edtHost, WM_GETTEXT, n, (LPARAM) host);
        n = SendMessage(edtPort, WM_GETTEXTLENGTH, 0, 0) + 1;
        port = (char*) malloc(n);
        SendMessage(edtPort, WM_GETTEXT, n, (LPARAM) port);
        SendMessage(edtTransfer, WM_SETTEXT, 0, (LPARAM) "");
        AddLine("Recherche du serveur... ", FALSE);
        ServerInfos = gethostbyname(host);
        if (!ServerInfos) {
          AddLine("erreur", TRUE);
          ResetControls(FALSE);
          break;
        }
        AddLine("ok", TRUE);
        memset(&ClientSock, 0, sizeof(SOCKADDR_IN));
        memcpy(&ClientSock.sin_addr.s_addr, ServerInfos->h_addr, ServerInfos->h_length);
        ClientSock.sin_port = htons(atoi(port));
        ClientSock.sin_family = AF_INET;
        Client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        AddLine("Connexion au serveur... ", FALSE);
        if (connect(Client, (SOCKADDR*) &ClientSock, sizeof(SOCKADDR_IN))) {
          AddLine("erreur", TRUE);
          ResetControls(FALSE);
          break;
        }
        SendMessage(edtTransfer, WM_SETTEXT, 0, (LPARAM) "");
        EnableWindow(btnDisconnect, TRUE);
        EnableWindow(stcTransfer, TRUE);
        EnableWindow(edtTransfer, TRUE);
        EnableWindow(stcCommand, TRUE);
        EnableWindow(edtCommand, TRUE);
        EnableWindow(btnSend, TRUE);
        SetFocus(edtCommand);
        UpdateWindow(wndMain);
        RecvThread = CreateThread(NULL, 0, &ClientRecv, &Client, 0, &RecvThreadID);
        break;
      case IDB_DISCONNECT:
        ResetControls(FALSE);
        break;
      case IDB_SEND:
        SetFocus(edtCommand);
        n = SendMessage(edtCommand, WM_GETTEXTLENGTH, 0, 0) + 1;
        line = (char*) malloc(n);
        SendMessage(edtCommand, WM_GETTEXT, n, (LPARAM) line);
        SendMessage(edtCommand, WM_SETTEXT, 0, (LPARAM) "");
        AddLine("=> ", FALSE);
        AddLine(line, FALSE);
        send(Client, line, n - 1, 0);
        free(line);
        UpdateWindow(wndMain);
        break;
      }
      break;
    }
    return 0;
  case WM_CLOSE:
    PostQuitMessage(0);
    return 0;
  default:
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  MSG msg;
  WNDCLASSEX wc;
  hMain = hInstance;
  wc.cbSize = sizeof(WNDCLASSEX);
  wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = wndMainProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hMain;
  wc.hIcon = LoadIcon(hMain, "MAINICON");
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  wc.lpszMenuName = NULL;
  wc.lpszClassName = WND_CLASS;
  wc.hIconSm = NULL;
  RegisterClassEx(&wc);
  InitializeCriticalSection(&lock);
  hFont = CreateFont(8, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "MS Sans Serif");
  wndMain = CreateWindowEx(0, WND_CLASS, "Telnet Win32", WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 460, 540, HWND_DESKTOP, NULL, hMain, NULL);
  SendMessage(wndMain, WM_SETFONT, (WPARAM) hFont, TRUE);
  stcHost = CreateWindowEx(0, "STATIC", "Hôte :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 10, 30, 14, wndMain, NULL, hMain, NULL);
  SendMessage(stcHost, WM_SETFONT, (WPARAM) hFont, TRUE);
  edtHost = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "127.0.0.1", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_LOWERCASE, 46, 6, 300, 21, wndMain, (HMENU) IDE_HOST, hMain, NULL);
  SendMessage(edtHost, WM_SETFONT, (WPARAM) hFont, TRUE);
  stcPort = CreateWindowEx(0, "STATIC", "Port :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 35, 30, 14, wndMain, NULL, hMain, NULL);
  SendMessage(stcPort, WM_SETFONT, (WPARAM) hFont, TRUE);
  edtPort = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "23", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_LOWERCASE, 46, 31, 300, 21, wndMain, (HMENU) IDE_PORT, hMain, NULL);
  SendMessage(edtPort, WM_SETFONT, (WPARAM) hFont, TRUE);
  btnConnect = CreateWindowEx(0, "BUTTON", "Connexion", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 6, 80, 21, wndMain, (HMENU) IDB_CONNECT, hMain, NULL);
  SendMessage(btnConnect, WM_SETFONT, (WPARAM) hFont, TRUE);
  btnDisconnect = CreateWindowEx(0, "BUTTON", "Déconnexion", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 31, 80, 21, wndMain, (HMENU) IDB_DISCONNECT, hMain, NULL);
  SendMessage(btnDisconnect, WM_SETFONT, (WPARAM) hFont, TRUE);
  stcTransfer = CreateWindowEx(0, "STATIC", "Transferts :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 60, 60, 14, wndMain, NULL, hMain, NULL);
  SendMessage(stcTransfer, WM_SETFONT, (WPARAM) hFont, TRUE);
  edtTransfer = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_READONLY | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 75, 431, 300, wndMain, NULL, hMain, NULL);
  SendMessage(edtTransfer, WM_SETFONT, (WPARAM) hFont, TRUE);
  stcCommand = CreateWindowEx(0, "STATIC", "Requête :", WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP, 10, 380, 60, 14, wndMain, NULL, hMain, NULL);
  SendMessage(stcCommand, WM_SETFONT, (WPARAM) hFont, TRUE);
  edtCommand = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 398, 431, 104, wndMain, (HMENU) IDE_COMMAND, hMain, NULL);
  SendMessage(edtCommand, WM_SETFONT, (WPARAM) hFont, TRUE);
  btnSend = CreateWindowEx(0, "BUTTON", "Envoyer", WS_CHILD | WS_VISIBLE | BS_TEXT | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON | BS_NOTIFY, 360, 376, 80, 21, wndMain, (HMENU) IDB_SEND, hMain, NULL);
  SendMessage(btnSend, WM_SETFONT, (WPARAM) hFont, TRUE);
  AddLine("Initialisation de winsock... ", FALSE);
  bWinsock = !WSAStartup(MAKEWORD(2, 2), &WSAData);
  if (bWinsock) AddLine("ok", TRUE);
  else AddLine("erreur", TRUE);
  ResetControls(FALSE);
  ShowWindow(wndMain, SW_SHOW);
  while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  ResetControls(FALSE);
  DestroyWindow(wndMain);
  DeleteCriticalSection(&lock);
  DeleteObject(hFont);
  WSACleanup();
  return msg.wParam;
}

Conclusion

Si y'a des maladresses, hésitez pas à les pointer du doigt, c'est ma toute première application graphique ;)

MAJ : placement du focus sur edtCommand... çà peut sembler inutile mais lorsqu'on utilise le prog quotidiennement, c'est appréciable ;)

PS: désolé pour le manque de commentaires...
 

Commentaires et avis

signaler à un administrateur
Commentaire de Krox68 le 12/05/2003 17:25:50

on dirai ke taime pa tro les MFC (comme moi :) avec ton titre...
lol il est bien ton telnet continue

signaler à un administrateur
Commentaire de Nebula le 12/05/2003 17:37:21

C'est pas tellement les MFC en tant que telles qui me gêne, c'est juste que je vois pas l'intérêt de dépendre de dlls parfois titanesques (je ne citerais que le cas de VB, meme si c'est pas des MFC) alors que tout ce dont on a besoin se trouve dans les dlls de windows... Mais çà n'engage que moi hein, pas frapper ;)

Krox, merci pour ta source, elle m'a beaucoup appris et je vais probablement faire un truc similaire pour le faciliter la vie :p En y incluant la création de contrôles communs toutefois, si çà t'intéresse je te l'enverrais ;)

Mon objectif maintenant est de recoder une appli que j'ai faite en Delphi (et bourrée de bugs car j'ai du bricoler avec l'API pour combler les lacunes de Delphi) en C, et y'a du boulot (PageControls, RichEdits et ListBox en OwnerDraw seront de la partie) :s

Envoyez vos dons (sous la forme de cachets d'aspirine) svp ;p

signaler à un administrateur
Commentaire de Anacr0x le 26/05/2003 22:36:39

ben moi j'aimeré posé une tte petite question : en fait, j'ai un peu de mal a comprendre cette source (surtt kil y a plein de fonction ke je coné pa) et j'aimeré bi1 savoir kommen tu fait pour affiché dans la boite de dialogue la réponse que le serveur t'envoi

signaler à un administrateur
Commentaire de Anacr0x le 26/05/2003 23:21:05

dsl, oublié ce ke je vien de dire, j'en é plus besoin, merci kan meme ;)

signaler à un administrateur
Commentaire de meech le 28/05/2003 19:29:30

mortel...

signaler à un administrateur
Commentaire de x3dt le 07/06/2003 20:12:43

sa compile pas sous dev-c++ (gcc) ya une manip a faire ??
sa me dit linker error ,,,

signaler à un administrateur
Commentaire de Nebula le 08/06/2003 00:56:05

linke le projet avec ws2_32 (j'utilise moi meme GCC), c'est la seule manip à faire ...

ligne de commande:
gcc telnet.c -o telnet.exe -lws2_32

signaler à un administrateur
Commentaire de kalden_mibs le 17/04/2004 15:30:33

j'ai un probléme lorsque je compil tes sources avec gcc en tuilisant gcc telnet.c -o telnet.exe -lws2_32 y me fait une erreur sur createFont et il me dit undefined reference to createFont
tu peux m'aider?

signaler à un administrateur
Commentaire de meech le 19/04/2004 09:13:31

J'utilise également Dev-C++ (cet outil est terrible), et je n'ai absolument aucun problème lorsque je compile le projet, que ce soit en le liant avec la librairie statique libws2_32.a ou bien dynamiquement avec la librairie partagée de windows ws2_32.dll.

Vérifie, lors de la compilation, que les chemins des includes et des librairies sont spécifiés correctement (option -I et -L).

Peux-tu retourner l'erreur précise résultante de ton compilateur ? Au cas où nous pourrions t'aider...

NB. J'ai même pu le compiler avec GCC sous Cygwin !! (je suis fan de Cygwin ;-)  En tout cas, c'est une bien belle application, même si elle demeure simple, dont nous a gratifié Nébula. Félicitations une fois de plus.

signaler à un administrateur
Commentaire de meech le 19/04/2004 09:16:42

Idée vite fait : Essaie de le lier avec la librairie statistique libgdi32.a ou un truc du genre...

signaler à un administrateur
Commentaire de kalden_mibs le 21/04/2004 18:36:48

Bon alors j'ai telechargé dev-c++ je compile et j'ai droit à :

In file included from c:\docume~1\kalden\mesdoc~1\telnet.cpp:3:
C:\DEV-C_~1\Include\winsock2.h:46: unbalanced `#endif'

signaler à un administrateur
Commentaire de Nebula le 15/09/2004 22:46:05

meech> merci, çà fait plaisir :-)

Pour CreateFont : -lgdi32, en effet

kalden_mibs> euh, essaie avec une version plus récente de devcpp, ou inclus winsock2.h avant windows.h :-/

signaler à un administrateur
Commentaire de Mini92 le 25/04/2006 14:46:19

Merci pour cette source magnifique et instructive.
Des que je peu voter je met 10/10 °o°

Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Octobre 2008
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,234 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.