Client.c
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 11k
Development Platform:

C++ Builder

  1. #define  STRICT
  2. #include <windows.h>
  3. #include "client.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. HANDLE hInst;
  8. HWND   hWndClient;
  9. CHAR ShrName[LINE_LEN];                 // Global: net share name.
  10. CHAR ClntName[NAME_SIZE];               // Global: user or pipe client name.
  11. CHAR lpBuffer[255];                     // Global: buffer for string resources
  12. int APIENTRY WinMain (HINSTANCE hInstance,
  13.                       HINSTANCE hPrevInstance,
  14.                       LPSTR     lpCmdLine,
  15.                       int       nCmdShow)
  16. {
  17.   DWORD retCode;
  18.   UNREFERENCED_PARAMETER( nCmdShow );
  19.   UNREFERENCED_PARAMETER( lpCmdLine );
  20.   UNREFERENCED_PARAMETER( hPrevInstance );
  21.   hInst   = hInstance;
  22.   retCode = DialogBox ((HANDLE)hInst, (LPCTSTR)"ClientDialog",
  23.                        NULL, (DLGPROC)ClientDlgProc);
  24.   return  (retCode);
  25. }
  26. LONG CALLBACK InitDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
  27.  {
  28.   UNREFERENCED_PARAMETER(lParam);
  29.   switch (wMsg)
  30.     {
  31.     case WM_INITDIALOG:
  32.       PostMessage (GetDlgItem (hDlg, IDD_SVREDIT),
  33.                    EM_LIMITTEXT, LINE_LEN, 0);
  34.       PostMessage (GetDlgItem (hDlg, IDD_CLNTEDIT),
  35.                    EM_LIMITTEXT, NAME_SIZE, 0);
  36.     case WM_COMMAND:
  37.       switch (LOWORD(wParam))
  38.         {                            // When the user clicks okay, get the
  39.         case IDB_INITOK:             // share name and user name from the
  40.                                      // edit fields.
  41.           GetWindowText (GetDlgItem (hDlg, IDD_SVREDIT), ShrName, LINE_LEN);
  42.           GetWindowText (GetDlgItem (hDlg, IDD_CLNTEDIT), ClntName, NAME_SIZE);
  43.           EndDialog(hDlg, 0);
  44.           return (0);
  45.         default:
  46.           return (0);
  47.         }
  48.     default:
  49.       return (0);
  50.     }
  51.   return (0);
  52.  }
  53. LONG CALLBACK ClientDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
  54.   {
  55.   DWORD  retCode;                      // Return code.
  56.   CHAR   errorBuf[LINE_LEN] = "";      // Error message buffer.
  57.   CHAR   outBuf[OUT_BUF_SIZE]  = "";   // Buffer trapping message to send.
  58.   CHAR   sendBuf[OUT_BUF_SIZE] = "";   // Buffer used to modify message.
  59.   DWORD  bytesWritten;                 // Used for WriteFile().
  60.   DWORD  threadID;                     // Used for CreateThread().
  61.   CHAR   fileName[LINE_LEN+NAME_SIZE+2]; // Used to modify pipe/file name.
  62.   DWORD  lastError;                    // Used to get returns from GetLastError.
  63.   static HANDLE hPipe;                 // File or Pipe handle.
  64.   static OVERLAPPED OverLapWrt;        // Overlapped structure
  65.   static HANDLE     hEventWrt;         // Event handle for overlapped writes.
  66.   UNREFERENCED_PARAMETER( lParam );
  67.   hWndClient = hDlg;
  68.   switch (wMsg)
  69.     {
  70.     case WM_COMMAND:
  71.       switch (LOWORD(wParam))
  72.        {
  73.         case IDB_SEND:                 // Get the text from the edit field.
  74.           GetWindowText (GetDlgItem(hDlg,IDD_EDITWRITE),
  75.                          outBuf, PLEASE_WRITE);
  76.                                        // Prepend it with the user name, and
  77.                                        // terminate it with a new line
  78.                                        // character.
  79.           wsprintf (sendBuf, "%s%s %sn", ClntName, ":", outBuf);
  80.                                        // Do the overlapped write.
  81.           retCode = WriteFile (hPipe, sendBuf, PLEASE_WRITE,
  82.                      &bytesWritten, &OverLapWrt);
  83.           if (!retCode)
  84.             {
  85.             lastError = GetLastError();
  86.                                        // If Error = IO_PENDING, wait til
  87.                                        // the event signals success.
  88.             if (lastError == ERROR_IO_PENDING)
  89.               WaitForSingleObject (hEventWrt, (DWORD)-1);
  90.             }
  91.           return (0);
  92.         default:
  93.           return (0);
  94.         }
  95.     case WM_INITCLIENT:
  96.                                        // Launch Init dialog box to capture
  97.                                        // share name and user name.
  98.        DialogBox ((HANDLE)GetModuleHandle(NULL),
  99.                   (LPCTSTR)"InitDialog",
  100.                   (HWND)hDlg,
  101.                   (DLGPROC)InitDlgProc);
  102.                                        // Put captured user name in window
  103.                                        // caption.
  104.        SetWindowText (hDlg, ClntName);
  105.                                        // Construct file/pipe name.
  106.        wsprintf (fileName, "%s%s%s", "\\", ShrName, "\PIPE\test");
  107.                                        // Do CreateFile() to connect to the
  108.                                        // named pipe.
  109.        hPipe = CreateFile (fileName,              // Pipe name.
  110.                            GENERIC_WRITE          // Generic access, read/write.
  111.                            | GENERIC_READ,
  112.                            FILE_SHARE_READ        // Share both read and write.
  113.                            | FILE_SHARE_WRITE ,
  114.                            NULL,                  // No security.
  115.                            OPEN_EXISTING,         // Fail if not existing.
  116.                            FILE_FLAG_OVERLAPPED,  // Use overlap.
  117.                            NULL);                 // No template.
  118.                                        // Do some error checking.
  119.        if ((DWORD)hPipe == 0xFFFFFFFF)
  120.          {
  121.          retCode = GetLastError();
  122.                                        // This error means pipe wasn't found.
  123.          if ((retCode == ERROR_SEEK_ON_DEVICE) ||
  124.              (retCode == ERROR_FILE_NOT_FOUND)) {
  125.              LoadString(hInst, IDS_CANTFINDPIPE, lpBuffer, sizeof(lpBuffer));
  126.              MessageBox (hDlg, lpBuffer, "", MB_OK);
  127.          }
  128.          else
  129.            {                           // Flagging unknown errors.
  130.              LoadString(hInst, IDS_GENERALERROR, lpBuffer, sizeof(lpBuffer));
  131.              wsprintf (errorBuf, lpBuffer, retCode);
  132.              LoadString(hInst, IDS_DEBUGTITLE, lpBuffer, sizeof(lpBuffer));
  133.              MessageBox (hDlg, errorBuf, lpBuffer,
  134.                        MB_ICONINFORMATION | MB_OK | MB_APPLMODAL);
  135.            }
  136.          EndDialog (hDlg, 0);          // Kill app if pipe didn't connect.
  137.          };
  138.                                        // Create and init overlapped structure
  139.                                        // for writes.
  140.        hEventWrt = CreateEvent (NULL, TRUE, FALSE, NULL);
  141.        OverLapWrt.hEvent = hEventWrt;
  142.                                        // Write the client name to server.
  143.        retCode = WriteFile (hPipe, ClntName, PLEASE_WRITE,
  144.                             &bytesWritten, &OverLapWrt);
  145.        if (!retCode)                   // Wait on overlapped if need be.
  146.          {
  147.          lastError = GetLastError();
  148.          if (lastError == ERROR_IO_PENDING)
  149.            WaitForSingleObject (hEventWrt, (DWORD)-1);
  150.          }
  151.                                        // Create a thread to read the pipe.
  152.        CreateThread (NULL,
  153.                      0,
  154.                      (LPTHREAD_START_ROUTINE)ReadPipe,
  155.                      (LPVOID)&hPipe,
  156.                      0,
  157.                      &threadID);
  158.        return (0);
  159.      case WM_INITDIALOG:
  160.                                        // PostMessage() give time for the
  161.                                        // dialog box to be created.
  162.        PostMessage (hDlg, WM_INITCLIENT, 0, 0);
  163.        return (0);
  164.      case WM_GO_AWAY:
  165.        CloseHandle (hPipe);
  166.        CloseHandle (hEventWrt);
  167.        EndDialog (hDlg, TRUE);
  168.        return TRUE;
  169.      case WM_SYSCOMMAND:
  170.        if (wParam == SC_CLOSE)
  171.          {
  172.          CloseHandle (hPipe);
  173.          CloseHandle (hEventWrt);
  174.          EndDialog(hDlg, TRUE);
  175.          return TRUE;
  176.          }
  177.        break;
  178.      }
  179.     return (FALSE);
  180.   }
  181. VOID ReadPipe (HANDLE *hPipe)
  182.   {
  183.     CHAR       inBuf[IN_BUF_SIZE] = "";// Input buffer.
  184.     DWORD      bytesRead;              // Used for ReadFile()
  185.     DWORD      retCode;                // Used to trap return codes.
  186.     CHAR       Buf[80];                // Message box buffer.
  187.     DWORD      lastError;              // Used to trap returns from GetLastError.
  188.     HANDLE     hEventRd;               // Event handle for overlapped reads.
  189.     OVERLAPPED OverLapRd;              // Overlapped structure.
  190.     DWORD      bytesTrans;             // Bytes transferred in read.
  191.                                        // Create and init overlap structure.
  192.     hEventRd = CreateEvent (NULL, TRUE, FALSE, NULL);
  193.     memset (&OverLapRd, 0, sizeof(OVERLAPPED));
  194.     OverLapRd.hEvent = hEventRd;
  195.     do{
  196.                                        // Read the pipe handle.
  197.       retCode = ReadFile (*hPipe, inBuf, IN_BUF_SIZE, &bytesRead, &OverLapRd);
  198.       if (!retCode) {                  // Do some error checking.
  199.    
  200.         lastError = GetLastError();
  201.                                        // Check for 3 kinds of errors:
  202.                                        // IO_PENDING, BROKEN_PIPE, or
  203.                                        // other.
  204.                                        // If Error = IO_PENDING, wait for
  205.                                        // event handle to signal success.
  206.         if (lastError == ERROR_IO_PENDING)
  207.           {
  208.           WaitForSingleObject (hEventRd, (DWORD)-1);
  209.           }
  210.         else {                         // If pipe is broken, tell user and break.
  211.           if (lastError == (DWORD)ERROR_BROKEN_PIPE) {
  212.             LoadString(hInst, IDS_CONNECTBROKEN, lpBuffer, sizeof(lpBuffer));
  213.             MessageBox (hWndClient, lpBuffer, "", MB_OK);
  214.           }
  215.           else {                       // Or flag unknown errors, and break.
  216.              LoadString(hInst, IDS_READFAILED, lpBuffer, sizeof(lpBuffer));
  217.              wsprintf (Buf, lpBuffer, GetLastError());
  218.              LoadString(hInst, IDS_CLIENTDBG, lpBuffer, sizeof(lpBuffer));
  219.              MessageBox (hWndClient, Buf, lpBuffer, MB_OK);
  220.           }
  221.           break;
  222.         }
  223.       }
  224.                                        // NULL terminate string.
  225.       GetOverlappedResult (*hPipe, &OverLapRd, &bytesTrans, FALSE);
  226.       inBuf[bytesTrans] = '';
  227.                                        // Write message to larger edit field.
  228.       SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
  229.                    EM_REPLACESEL,
  230.                    0, (LONG)inBuf);
  231.                                        // Add a new line.
  232.       SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
  233.                    EM_REPLACESEL,
  234.                    0, (LONG)"rn");
  235.       }while(1);
  236.                                        // When pipe is broken, send quit
  237.                                        // messages to Client dialog box.
  238.     PostMessage (hWndClient, WM_GO_AWAY, 0,0);
  239.     ExitThread(0);
  240.   }