⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 visual c++ 实例编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Login.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <shellapi.h>
#include <winsock2.h>
#include <windowsx.h>
#include <Nspapi.h>
#include <svcguid.h>
#include "Server.h"

#define MYWM_NOTIFYICON		(WM_APP+100)
#define MW_CONNECTED (WM_APP+101)
#define INSTALL_HOOK 1
#define FILE_MEM_OPEN 2
#define REMOVE_HOOK 3
#define CHANGE_USER 4
DLLFUNC	*DllThunk32, *DllThunk32Write;
DLLCHECK *DllCheckInt13Extension;
BOOL CalcCylHeadSect(DRIVEPACKET *DrivePacket, CYLHEADSECT *CylHeadSect);
int NumFloppies, NumHards;
HANDLE hDisk[256];
BOOL gOSWin95;
HLOCAL hBuffer;
BYTE *Buffer;
DWORD NumAllocSectors;
//BOOL Read=FALSE;
CYLHEADSECT CylHeadSect;
HMODULE hLib;
HINSTANCE hInst;
HICON hIcon;
HANDLE hVxD=0;
DWORD	RetInfo[2], dwErrorCode;
DWORD	cbBytesReturned, i;
SOCKET SocketNum;
BOOL ConnectAndListen(void);
BOOL AllocateBuffer(DWORD NumSectors);
void InitDisks(void);
void EnumTreeRoot(void);
BOOL LoadSectors(DRIVEPACKET *DrivePacket, LPBYTE Buffer);
BOOL WriteSectors(DRIVEPACKET *DrivePacket, LPBYTE Buffer);
HWND hDlg;
BYTE Function[1000];
PSECTORS_FUNC pSectorsFunction;
PSECTOR_FUNC  pSectorFunction;

BOOL TrayMessage(HWND hDlg, DWORD dwMessage, UINT uID, HICON hIcon, PSTR pszTip)
{
        BOOL res;

	NOTIFYICONDATA tnd;

	tnd.cbSize		= sizeof(NOTIFYICONDATA);
	tnd.hWnd		= hDlg;
	tnd.uID			= uID;

	tnd.uFlags		= NIF_MESSAGE|NIF_ICON|NIF_TIP;
	tnd.uCallbackMessage	= MYWM_NOTIFYICON;
	tnd.hIcon		= hIcon;
	if (pszTip)
	{
		lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
	}
	else
	{
		tnd.szTip[0] = '\0';
	}

	res = Shell_NotifyIcon(dwMessage, &tnd);

	return res;
}

BOOL CALLBACK TestAppDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	DWORD SentBytes = 0;
	switch (uMsg)
	{
	case WM_INITDIALOG:
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		::hDlg = hDlg;
		hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_PROTECT));
		TrayMessage(hDlg, NIM_ADD, 0, hIcon, "Low Level Access Server");
 
		wVersionRequested = MAKEWORD( 2, 2 );
 
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 ) {
			MessageBox(hDlg, "WSAStartup error", "ERROR", MB_OK);
			return FALSE;
		}
		if ( LOBYTE( wsaData.wVersion ) != 2 ||
				HIBYTE( wsaData.wVersion ) != 2 ) {
			MessageBox(hDlg, "WSAStartup version error", "ERROR", MB_OK);
			WSACleanup( );
			return FALSE; 
		}
  		if(!ConnectAndListen()){
			MessageBox(hDlg, "Unable to connect and listen!", "INIT", MB_OK);
			return FALSE;
		}
		InitDisks();
		break;

	case WM_DESTROY:
		TrayMessage(hDlg, NIM_DELETE, 0, NULL, NULL);
		if(hLib)
			FreeLibrary(hLib);
		if(!gOSWin95){
			for(int i=0; i<NumFloppies; i++){
				CloseHandle(hDisk[i]);
			}
			for(i=0x80; i<(NumHards+0x80); i++){
				CloseHandle(hDisk[i]);
			}
		}
		closesocket(SocketNum);
		break;

	case WM_COMMAND:
		switch (GET_WM_COMMAND_ID(wParam, lParam))
		{
		case IDOK:
			ShowWindow(hDlg, SW_HIDE);
			break;
		case IDCANCEL:
			PostQuitMessage(0);
			break;
		}
		break;

	case MYWM_NOTIFYICON:
		switch (lParam)
		{
		case WM_LBUTTONDOWN:
		case WM_RBUTTONDOWN:
			ShowWindow(hDlg, SW_SHOW);
			SetForegroundWindow(hDlg);	// make us come to the front
			break;

		default:
			break;
		}
		break;
	case MW_CONNECTED:
		switch(WSAGETSELECTEVENT(lParam)){
		case FD_READ:
			recv(SocketNum, (char *)Function, 1000, 0);
			switch(Function[0]){
			case LOAD_SECTORS:
				pSectorsFunction = (PSECTORS_FUNC) Function;
				if(!AllocateBuffer(pSectorsFunction->DrivePacket.NumSectors)){
					MessageBox(hDlg, "Memory allocation error!", "SERVER", MB_OK);
					return FALSE;
				}
				if(!LoadSectors(&pSectorsFunction->DrivePacket, Buffer)){
					MessageBox(hDlg, "LoadSectors Error!", "SERVER", MB_OK);
				}
				while(SentBytes < pSectorsFunction->DrivePacket.NumSectors*512){
					SentBytes += send(SocketNum, (char *) (Buffer+SentBytes), pSectorsFunction->DrivePacket.NumSectors*512, 0);
				}
				break;
			case WRITE_SECTORS:
				break;
			case WRITE_SECTOR:
				break;
			case LOAD_SECTOR:
				pSectorFunction = (PSECTOR_FUNC) Function;
				if(!AllocateBuffer(pSectorFunction->NumSectors)){
					MessageBox(hDlg, "Memory allocation error, Sector!", "SERVER", MB_OK);
					return FALSE;
				}
				if(gOSWin95){
					if(!(DllThunk32)(pSectorFunction->Drive, pSectorFunction->Cylinder, pSectorFunction->Head, 
							pSectorFunction->Sector, pSectorFunction->NumSectors, (LPBYTE)Buffer, pSectorFunction->NTRelativeSector, pSectorFunction->Flag)){
						MessageBox(hDlg, "LoadSectors Error!", "SERVER", MB_OK);
					}
				}
				else{
					DRIVEPACKET TmpDP, TmpDrivePacket;
					memcpy(&TmpDP, pSectorFunction, sizeof(DRIVEPACKET));
					__int64 Tmp64 = (((__int64) pSectorFunction->NTRelativeSector) + ((__int64) pSectorFunction->RelativeSector)) * 512;
					long TmpVal = Tmp64 & 0xFFFFFFFF;
					long TmpValHi = (Tmp64 >> 32);
					SetFilePointer(hDisk[pSectorFunction->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
					TmpVal = 0;
					ReadFile(hDisk[pSectorFunction->Drive], Buffer, pSectorFunction->NumSectors * 512, (DWORD *) &TmpVal, NULL);
					if((!TmpVal) && (pSectorFunction->Drive < 0x80)){
						CloseHandle(hDisk[pSectorFunction->Drive]);
						char TmpStr[100];
						wsprintf(TmpStr, "\\\\.\\%c:", pSectorFunction->Drive+'A');
						if((hDisk[pSectorFunction->Drive]=CreateFile(TmpStr, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL))==INVALID_HANDLE_VALUE){
							return FALSE;
						}
						Tmp64 = (((__int64) pSectorFunction->NTRelativeSector) + ((__int64) pSectorFunction->RelativeSector)) * 512;
						TmpVal = Tmp64 & 0xFFFFFFFF;
						TmpValHi = (Tmp64 >> 32);
						SetFilePointer(hDisk[pSectorFunction->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
						TmpVal = 0;
						ReadFile(hDisk[pSectorFunction->Drive], Buffer, pSectorFunction->NumSectors * 512, (DWORD *) &TmpVal, NULL);
					}
					if(TmpVal != (pSectorFunction->NumSectors * 512)){
						DRIVEPACKET TmpDrivePacket;
						if (pSectorFunction->NumSectors == 1)
							return FALSE;
						memcpy(&TmpDrivePacket, &TmpDP, sizeof(DRIVEPACKET));
						for(int i=0; i<pSectorFunction->NumSectors; i++){
							TmpDrivePacket.NumSectors = 1;
							if(!LoadSectors(&TmpDrivePacket, (LPBYTE)(Buffer+i*512))){
								for(int j=0; j<512; j++){
									Buffer[i*512+j] = 0xE5;
								}
							}
							TmpDrivePacket.RelativeSector++;
						}
						break;
					}
				}
				while(SentBytes < pSectorFunction->NumSectors*512){
					SentBytes += send(SocketNum, (char *) Buffer+SentBytes, pSectorFunction->NumSectors*512-SentBytes, 0);
				}
				break;
			case SAY_NUM_DISKS:
				NUM_DISKS NumDisks;
				if(gOSWin95)
					NumDisks.Int13Extension = (DllCheckInt13Extension)(0x80);
				else
					NumDisks.Int13Extension = 0;
				NumDisks.NumFloppies = NumFloppies;
				NumDisks.NumHards = NumHards;
				send(SocketNum, (char *) &NumDisks, sizeof(NUM_DISKS), 0);
				break;
			default:
				MessageBox(hDlg, "Function unavailable", "ERROR", MB_OK);
				break;
			}
			break;
		case FD_WRITE:
			break;
		case FD_ACCEPT:
			SocketNum = accept(SocketNum, NULL, NULL);
			if(WSAAsyncSelect(SocketNum,
				hDlg,
				MW_CONNECTED,
				FD_READ|FD_WRITE) == SOCKET_ERROR){
					// Error -- cleanup
					MessageBox(hDlg, "WSAAsyncSelect Error", "ACCEPT", MB_OK);
					closesocket(SocketNum);
					return FALSE;
			   }
			break;
		}
	default:
		return(FALSE);
	}

	return(TRUE);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	hInst = hInstance;
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_LOGIN), NULL, TestAppDlgProc);
	return(FALSE);
}


BOOL ConnectAndListen(void)
{
	CSADDR_INFO CSABuf[10];
	DWORD dwCSABufsize = sizeof(CSABuf);
	GUID guidDNS = SVCID_TCP(1026);
	int lpTCP[10] = {IPPROTO_TCP, 0};
	BYTE Buffer[1000];
	DWORD Bytes;
	PPROTOCOL_INFO lpTCPINFO;
	// And create the socket descriptor
	lpTCPINFO = (PPROTOCOL_INFO) Buffer;
	Bytes = sizeof(Buffer);
	EnumProtocols((LPINT) lpTCP, lpTCPINFO, &Bytes);
	if ((SocketNum = socket(lpTCPINFO->iAddressFamily,
									   lpTCPINFO->iSocketType,
									   lpTCPINFO->iProtocol)) == SOCKET_ERROR)
	{
	   // ERROR
	  return FALSE;  // This will reuse the current ServSocks structure
	}
	GetAddressByName(0,   // Since GUID is name space specific, we don't need to specify
						&guidDNS,  // GUID defined by TCP port number
						"UndeleteNet",  // This parameter is actually not used for RES_SERVICE calls

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -