name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #define STRICT
#include <windows.h>
#include "client.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
HANDLE hInst;
HWND hWndClient;
CHAR ShrName[LINE_LEN]; // Global: net share name.
CHAR ClntName[NAME_SIZE]; // Global: user or pipe client name.
CHAR lpBuffer[255]; // Global: buffer for string resources
int APIENTRY WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DWORD retCode;
UNREFERENCED_PARAMETER( nCmdShow );
UNREFERENCED_PARAMETER( lpCmdLine );
UNREFERENCED_PARAMETER( hPrevInstance );
hInst = hInstance;
retCode = DialogBox ((HANDLE)hInst, (LPCTSTR)"ClientDialog",
NULL, (DLGPROC)ClientDlgProc);
return (retCode);
}
LONG CALLBACK InitDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (wMsg)
{
case WM_INITDIALOG:
PostMessage (GetDlgItem (hDlg, IDD_SVREDIT),
EM_LIMITTEXT, LINE_LEN, 0);
PostMessage (GetDlgItem (hDlg, IDD_CLNTEDIT),
EM_LIMITTEXT, NAME_SIZE, 0);
case WM_COMMAND:
switch (LOWORD(wParam))
{ // When the user clicks okay, get the
case IDB_INITOK: // share name and user name from the
// edit fields.
GetWindowText (GetDlgItem (hDlg, IDD_SVREDIT), ShrName, LINE_LEN);
GetWindowText (GetDlgItem (hDlg, IDD_CLNTEDIT), ClntName, NAME_SIZE);
EndDialog(hDlg, 0);
return (0);
default:
return (0);
}
default:
return (0);
}
return (0);
}
LONG CALLBACK ClientDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
DWORD retCode; // Return code.
CHAR errorBuf[LINE_LEN] = ""; // Error message buffer.
CHAR outBuf[OUT_BUF_SIZE] = ""; // Buffer trapping message to send.
CHAR sendBuf[OUT_BUF_SIZE] = ""; // Buffer used to modify message.
DWORD bytesWritten; // Used for WriteFile().
DWORD threadID; // Used for CreateThread().
CHAR fileName[LINE_LEN+NAME_SIZE+2]; // Used to modify pipe/file name.
DWORD lastError; // Used to get returns from GetLastError.
static HANDLE hPipe; // File or Pipe handle.
static OVERLAPPED OverLapWrt; // Overlapped structure
static HANDLE hEventWrt; // Event handle for overlapped writes.
UNREFERENCED_PARAMETER( lParam );
hWndClient = hDlg;
switch (wMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDB_SEND: // Get the text from the edit field.
GetWindowText (GetDlgItem(hDlg,IDD_EDITWRITE),
outBuf, PLEASE_WRITE);
// Prepend it with the user name, and
// terminate it with a new line
// character.
wsprintf (sendBuf, "%s%s %s/n", ClntName, ":", outBuf);
// Do the overlapped write.
retCode = WriteFile (hPipe, sendBuf, PLEASE_WRITE,
&bytesWritten, &OverLapWrt);
if (!retCode)
{
lastError = GetLastError();
// If Error = IO_PENDING, wait til
// the event signals success.
if (lastError == ERROR_IO_PENDING)
WaitForSingleObject (hEventWrt, (DWORD)-1);
}
return (0);
default:
return (0);
}
case WM_INITCLIENT:
// Launch Init dialog box to capture
// share name and user name.
DialogBox ((HANDLE)GetModuleHandle(NULL),
(LPCTSTR)"InitDialog",
(HWND)hDlg,
(DLGPROC)InitDlgProc);
// Put captured user name in window
// caption.
SetWindowText (hDlg, ClntName);
// Construct file/pipe name.
wsprintf (fileName, "%s%s%s", "", ShrName, "//PIPE//test");
// Do CreateFile() to connect to the
// named pipe.
hPipe = CreateFile (fileName, // Pipe name.
GENERIC_WRITE // Generic access, read/write.
| GENERIC_READ,
FILE_SHARE_READ // Share both read and write.
| FILE_SHARE_WRITE ,
NULL, // No security.
OPEN_EXISTING, // Fail if not existing.
FILE_FLAG_OVERLAPPED, // Use overlap.
NULL); // No template.
// Do some error checking.
if ((DWORD)hPipe == 0xFFFFFFFF)
{
retCode = GetLastError();
// This error means pipe wasn't found.
if ((retCode == ERROR_SEEK_ON_DEVICE) ||
(retCode == ERROR_FILE_NOT_FOUND)) {
LoadString(hInst, IDS_CANTFINDPIPE, lpBuffer, sizeof(lpBuffer));
MessageBox (hDlg, lpBuffer, "", MB_OK);
}
else
{ // Flagging unknown errors.
LoadString(hInst, IDS_GENERALERROR, lpBuffer, sizeof(lpBuffer));
wsprintf (errorBuf, lpBuffer, retCode);
LoadString(hInst, IDS_DEBUGTITLE, lpBuffer, sizeof(lpBuffer));
MessageBox (hDlg, errorBuf, lpBuffer,
MB_ICONINFORMATION | MB_OK | MB_APPLMODAL);
}
EndDialog (hDlg, 0); // Kill app if pipe didn't connect.
};
// Create and init overlapped structure
// for writes.
hEventWrt = CreateEvent (NULL, TRUE, FALSE, NULL);
OverLapWrt.hEvent = hEventWrt;
// Write the client name to server.
retCode = WriteFile (hPipe, ClntName, PLEASE_WRITE,
&bytesWritten, &OverLapWrt);
if (!retCode) // Wait on overlapped if need be.
{
lastError = GetLastError();
if (lastError == ERROR_IO_PENDING)
WaitForSingleObject (hEventWrt, (DWORD)-1);
}
// Create a thread to read the pipe.
CreateThread (NULL,
0,
(LPTHREAD_START_ROUTINE)ReadPipe,
(LPVOID)&hPipe,
0,
&threadID);
return (0);
case WM_INITDIALOG:
// PostMessage() give time for the
// dialog box to be created.
PostMessage (hDlg, WM_INITCLIENT, 0, 0);
return (0);
case WM_GO_AWAY:
CloseHandle (hPipe);
CloseHandle (hEventWrt);
EndDialog (hDlg, TRUE);
return TRUE;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
CloseHandle (hPipe);
CloseHandle (hEventWrt);
EndDialog(hDlg, TRUE);
return TRUE;
}
break;
}
return (FALSE);
}
VOID ReadPipe (HANDLE *hPipe)
{
CHAR inBuf[IN_BUF_SIZE] = "";// Input buffer.
DWORD bytesRead; // Used for ReadFile()
DWORD retCode; // Used to trap return codes.
CHAR Buf[80]; // Message box buffer.
DWORD lastError; // Used to trap returns from GetLastError.
HANDLE hEventRd; // Event handle for overlapped reads.
OVERLAPPED OverLapRd; // Overlapped structure.
DWORD bytesTrans; // Bytes transferred in read.
// Create and init overlap structure.
hEventRd = CreateEvent (NULL, TRUE, FALSE, NULL);
memset (&OverLapRd, 0, sizeof(OVERLAPPED));
OverLapRd.hEvent = hEventRd;
do{
// Read the pipe handle.
retCode = ReadFile (*hPipe, inBuf, IN_BUF_SIZE, &bytesRead, &OverLapRd);
if (!retCode) { // Do some error checking.
lastError = GetLastError();
// Check for 3 kinds of errors:
// IO_PENDING, BROKEN_PIPE, or
// other.
// If Error = IO_PENDING, wait for
// event handle to signal success.
if (lastError == ERROR_IO_PENDING)
{
WaitForSingleObject (hEventRd, (DWORD)-1);
}
else { // If pipe is broken, tell user and break.
if (lastError == (DWORD)ERROR_BROKEN_PIPE) {
LoadString(hInst, IDS_CONNECTBROKEN, lpBuffer, sizeof(lpBuffer));
MessageBox (hWndClient, lpBuffer, "", MB_OK);
}
else { // Or flag unknown errors, and break.
LoadString(hInst, IDS_READFAILED, lpBuffer, sizeof(lpBuffer));
wsprintf (Buf, lpBuffer, GetLastError());
LoadString(hInst, IDS_CLIENTDBG, lpBuffer, sizeof(lpBuffer));
MessageBox (hWndClient, Buf, lpBuffer, MB_OK);
}
break;
}
}
// NULL terminate string.
GetOverlappedResult (*hPipe, &OverLapRd, &bytesTrans, FALSE);
inBuf[bytesTrans] = '/0';
// Write message to larger edit field.
SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
EM_REPLACESEL,
0, (LONG)inBuf);
// Add a new line.
SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
EM_REPLACESEL,
0, (LONG)"/r/n");
}while(1);
// When pipe is broken, send quit
// messages to Client dialog box.
PostMessage (hWndClient, WM_GO_AWAY, 0,0);
ExitThread(0);
}