Documente Academic
Documente Profesional
Documente Cultură
c
*
* Pravin Paratey (May 16, 2003)
**/
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
//#include <string.h>
#include <winsock2.h>
#include "ypktres.h"
/*<---------------------------------------------------------------------->*/
HINSTANCE hInst; // Instance handle
HWND hwndMain; //Main window handle
HWND hListbox1;
HWND hListbox2;
HWND hWndStatusbar;
WSADATA *WsaData;
HWND hwndToolBar;
BOOL _stdcall LoginDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_CLOSE:
EndDialog(hwnd,0);
return 1;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK: // LOGIN
// Store username and password and initiate login
process
GetDlgItemText(hwnd, IDE_USERNAME, ystrUsername, 49);
GetDlgItemText(hwnd, IDE_PASSWORD, ystrPassword, 49);
GetDlgItemText(hwnd, IDE_SERVERNAME, ystrServerName,
49);
GetDlgItemText(hwnd, IDE_SERVERPORT, ystrServerPort,
49);
sa_out.sin_addr.s_addr =
inet_addr("216.136.233.131");
sa_out.sin_port = htons(23);
//lstrcpy(ystrUsername,"zobloid");
//lstrcpy(ystrPassword,"applewindow246");
/*------------------------------------------------------------------------
Procedure: UpdateStatusBar ID:1
Purpose: Updates the statusbar control with the appropiate
text
Input: lpszStatusString: Charactar string that will be shown
partNumber: index of the status bar part number.
displayFlags: Decoration flags
Output: none
Errors: none
------------------------------------------------------------------------*/
void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
{
SendMessage(hWndStatusbar,
SB_SETTEXT,
partNumber | displayFlags,
(LPARAM)lpszStatusString);
}
/*------------------------------------------------------------------------
Procedure: MsgMenuSelect ID:1
Purpose: Shows in the status bar a descriptive explaation of
the purpose of each menu item.The message
WM_MENUSELECT is sent when the user starts browsing
the menu for each menu item where the mouse passes.
Input: Standard windows.
Output: The string from the resources string table is shown
Errors: If the string is not found nothing will be shown.
------------------------------------------------------------------------*/
LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
static char szBuffer[256];
UINT nStringID = 0;
UINT fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
UINT uCmd = GET_WM_MENUSELECT_CMD(wparam, lparam);
HMENU hMenu = GET_WM_MENUSELECT_HMENU(wparam, lparam);
/*------------------------------------------------------------------------
Procedure: InitializeStatusBar ID:1
Purpose: Initialize the status bar
Input: hwndParent: the parent window
nrOfParts: The status bar can contain more than one
part. What is difficult, is to figure out how this
should be drawn. So, for the time being only one is
being used...
Output: The status bar is created
Errors:
------------------------------------------------------------------------*/
void InitializeStatusBar(HWND hwndParent,int nrOfParts)
{
const int cSpaceInBetween = 8;
int ptArray[40]; // Array defining the number of parts/sections
RECT rect;
HDC hDC;
hDC = GetDC(hwndParent);
GetClientRect(hwndParent, &rect);
ptArray[nrOfParts-1] = rect.right;
//---TODO--- Add code to calculate the size of each part of the status
// bar here.
ReleaseDC(hwndParent, hDC);
SendMessage(hWndStatusbar,
SB_SETPARTS,
nrOfParts,
(LPARAM)(LPINT)ptArray);
UpdateStatusBar("Ready", 0, 0);
//---TODO--- Add code to update all fields of the status bar here.
// As an example, look at the calls commented out below.
/*------------------------------------------------------------------------
Procedure: CreateSBar ID:1
Purpose: Calls CreateStatusWindow to create the status bar
Input: hwndParent: the parent window
initial text: the initial contents of the status bar
Output:
Errors:
------------------------------------------------------------------------*/
static BOOL CreateSBar(HWND hwndParent,char *initialText,int nrOfParts)
{
hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER|
SBARS_SIZEGRIP,
initialText,
hwndParent,
IDM_STATUSBAR);
if(hWndStatusbar)
{
InitializeStatusBar(hwndParent,nrOfParts);
return TRUE;
}
return FALSE;
}
memset(&ofn,0,sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hInstance = GetModuleHandle(NULL);
ofn.hwndOwner = GetActiveWindow();
ofn.lpstrFile = buffer;
ofn.nMaxFile = buflen;
ofn.lpstrTitle = "Open";
ofn.nFilterIndex = 2;
ofn.lpstrDefExt = "pkt";
strcpy(buffer,"*.pkt");
strcpy(tmpfilter,"All files,*.*,yPkt Files,*.pkt");
while(tmpfilter[i]) {
if (tmpfilter[i] == ',')
tmpfilter[i] = 0;
i++;
}
tmpfilter[i++] = 0; tmpfilter[i++] = 0;
ofn.Flags = 539678;
ofn.lpstrFilter = tmpfilter;
return GetOpenFileName(&ofn);
/*<---------------------------------------------------------------------->*/
/*@@0->@@*/
static BOOL InitApplication(void)
{
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS));
wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "ypktWndClass";
wc.lpszMenuName = MAKEINTRESOURCE(IDMAINMENU);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
if (!RegisterClass(&wc))
return 0;
/*@@0<-@@*/
// ---TODO--- Call module specific initialization routines here
return 1;
}
/*<---------------------------------------------------------------------->*/
/*@@1->@@*/
HWND CreateypktWndClassWnd(void)
{
return CreateWindow("ypktWndClass","Yahoo! Messenger Packet Builder - by
LightBade",
WS_MINIMIZEBOX|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|
WS_MAXIMIZEBOX|WS_CAPTION|WS_BORDER|WS_SYSMENU|WS_THICKFRAME,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,
NULL,
NULL,
hInst,
NULL);
}
/*@@1<-@@*/
/*<---------------------------------------------------------------------->*/
/* --- The following code comes from C:\lcc\lib\wizard\aboutdlg.tpl. */
BOOL _stdcall AboutDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_CLOSE:
EndDialog(hwnd,0);
return 1;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
EndDialog(hwnd,1);
return 1;
}
break;
}
return 0;
}
/*<---------------------------------------------------------------------->*/
/* --- The following code comes from C:\lcc\lib\wizard\defOnCmd.tpl. */
/* --- The following code comes from C:\lcc\lib\wizard\aboutcmd.tpl. */
void MainWndProc_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{ char buffer[MAX_PATH];
switch(id) {
// ---TODO--- Add new menu commands here
/*@@NEWCOMMANDS@@*/
case IDM_ABOUT:
DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUT),
hwndMain,AboutDlg);
break;
case IDM_OPEN:
GetFileName(buffer,sizeof(buffer));
break;
case IDM_EXIT:
PostMessage(hwnd,WM_CLOSE,0,0);
break;
}
}
/*<---------------------------------------------------------------------->*/
/*@@2->@@*/
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
unsigned char *inbuf;
static unsigned char header[1000];
int retval;
unsigned short pktsize;
unsigned char c; //tmp var
static int bGotHeader=0;
unsigned char errStr[20];
RECT rc;
RECT rcToolbar, rcStatusbar;
switch (msg) {
case WM_WSAASYNC:
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
/*
// check for an error
retval = WSAGetLastError();
if (retval != 0 && retval != WSAEWOULDBLOCK) // Error
{
bGotHeader = 0;
wsprintf(header,"Error %i",retval);
MessageBox(hwnd, "Socket error in FD_READ", header, MB_OK);
WSASetLastError(0); // Reset error
return 0;
}
*/
if(!bGotHeader) // We have not recv'd header do it now
{
// Get header and determine the length of the packet
retval = recv(wParam,header,20,0);
if(WSAGetLastError() == WSAEWOULDBLOCK)
{
WSASetLastError(0);
}
else if (retval == SOCKET_ERROR)
{
MessageBox(hwnd, "Error while reading header",
"ERROR", MB_OK);
break;
}
bGotHeader = !bGotHeader;
}
else // We got header now get rest of the data
{
// 9 and 10th bytes give size
// Below two bytes are swapped for endian-ness
c = header[9];
header[9] = header[8];
header[8] = c;
// Set length
memcpy(&pktsize, header+8, 2);
inbuf = (unsigned char*)HeapAlloc(GetProcessHeap(), 0,
pktsize+20);
if (!inbuf)
{
MessageBox(hwnd, "Not enough memory", "ERROR",
MB_OK);
break;
}
// copy header
memcpy(inbuf,header,20);
// Now confirm that the WinSock 2 DLL supports the exact version
// we want. If not, make sure to call WSACleanup().
if (LOBYTE(WsaData.wVersion) != 2) {
MessageBox(hwndMain,
"Could not find the correct version of WinSock",
"Error", MB_OK | MB_ICONSTOP | MB_SETFOREGROUND);
WSACleanup();
ReturnValue = FALSE;
}
}
if (ReturnValue)
return &WsaData;
return(NULL);
} // InitWS2()
/*<---------------------------------------------------------------------->*/
#define NUM_TOOLBAR_BUTTONS 3
/* --- The following code comes from C:\lcc\lib\wizard\creatToolbar1.tpl. */
HWND CreateAToolBar(HWND hwndParent)
{
HWND hwndTB;
TBADDBITMAP tbab;
TBBUTTON tbb[NUM_TOOLBAR_BUTTONS];
int index;
// Create a toolbar that the user can customize and that has a
// tooltip associated with it.
hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR) NULL,
WS_CHILD|WS_BORDER,//|TBSTYLE_FLAT,
0, 0, 0, 0, hwndParent, (HMENU) ID_TOOLBAR, hInst, NULL);
index = SendMessage(hwndTB,TB_ADDSTRING,0,(LPARAM)"Replace");
tbb[0].iString = index;
index = SendMessage(hwndTB,TB_ADDSTRING,0,(LPARAM)"Delete");
tbb[1].iString = index;
index = SendMessage(hwndTB,TB_ADDSTRING,0,(LPARAM)"New");
tbb[2].iString = index;
// Button "Replace"
tbb[0].iBitmap = STD_REPLACE;
tbb[0].idCommand = IDM_EDITREPLACE;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
// Button "Delete"
tbb[1].iBitmap = STD_DELETE;
tbb[1].idCommand = IDM_EDITDELETE;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
// Button "New"
tbb[2].iBitmap = STD_FILENEW;
tbb[2].idCommand = IDM_NEW;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
SendMessage(hwndTB,TB_ADDBUTTONS,3,(LPARAM)&tbb);
ShowWindow(hwndTB,SW_SHOW);
return hwndTB;
}
/*<---------------------------------------------------------------------->*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
INT nCmdShow)
{
MSG msg;
HANDLE hAccelTable;
hInst = hInstance;
if (!InitApplication())
return 0;
hAccelTable = LoadAccelerators(hInst,MAKEINTRESOURCE(IDACCEL));
if ((hwndMain = CreateypktWndClassWnd()) == (HWND)0)
return 0;
CreateSBar(hwndMain,"Ready",1);
hwndToolBar = CreateAToolBar(hwndMain);
ShowWindow(hwndMain,SW_SHOW);
WsaData = InitWS2();
if (WsaData == NULL)
return 0;
while (GetMessage(&msg,NULL,0,0)) {
if (!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
WSACleanup();
return msg.wParam;
}
memcpy(pos,ystrUsername, ystrLenUsername);
pos += ystrLenUsername;
memcpy(pos,SEP,2);
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//str1 24 bytes
192,128, //53B
'9','6',
192,128, //58E
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//str2 24 bytes
192,128, //83B
'2',
192,128,
'1',
192,128,
'1',
192,128, //92E
//username,
//192,128
};
memcpy(outbuf,dastr,24);
pos = outbuf+23;
memcpy(outbuf+16,ystrSessionID,4);
ystrLenUsername = lstrlen(ystrUsername);
memcpy(pos,ystrUsername,ystrLenUsername);
pos +=ystrLenUsername;
memcpy(pos,ystrUsername,ystrLenUsername);
pos += ystrLenUsername;
// packet_dump(outbuf,len+20);
// PACKET HAS BEEN FORMED AT THIS POINT
if(send(sock,outbuf,len+20,0) == -1)
return 0;
else
return 1;
}
SEP
*/
// packet_dump(inbuf, len);
return 1;
}