📄 bootsec.c
字号:
//****************************************************************************
// File: BOOTSEC.C
//
//
// Purpose: The main module. Brings up a private dialog, that prompts
// the user for a drive letter. When the user chooses "Get
// Info", BOOTSEC determines what kind of drive it is, and then if
// appropriate, reads the boot sector of the drive, and displays
// all the information from the bootsector in the dialog.
//
// Functions:
// WinMain() - initializes app and processes message loop
// ActualDlgProc - the actual Dialog Procedure
// AboutDlgProc - Dialog procedure for the About box
// ClassDlgProc - the Window Proc for the Private Dialog
// NewEditProc - the subclassed Edit window procedure
// ReadBootSector - read the boot sector via INT 25
// ShowDriveInfo - display the boot sector structure
// GetPictRect - get a rect to display a drive icon
// IsCDRomDrive - is it a CD-ROM drive?
// IsNetDrive - is it a network drive?
// SetAllLabels - show a given string in all the labels
// MyGetDriveType - return the type of drive
//
// Development Team:
//
// Joe Long, June 1993
//
//
// Written by Microsoft Product Support Services, Windows Developer Support
// Copyright (c) 1992 Microsoft Corporation. All rights reserved.
//****************************************************************************
#include "windows.h"
#include "bootsec.h"
#include "memory.h"
#include "resource.h"
#define CBSECTORSIZE 512
// global vars
int iDriveType; // what kind of drive is selected
HINSTANCE hInst; // the instance handle
FARPROC lpfnOldEditProc; // the original edit procedure
char szErrorBuf[64]; // a buffer to load error strings
// exported function
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
BOOL FAR PASCAL __export ActualDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL FAR PASCAL __export AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
long FAR PASCAL __export ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam);
long FAR PASCAL __export NewEditProc(HWND hEdit, UINT message, WPARAM wParam , LPARAM lParam);
//helper functions
BOOL ReadBootSector(int iDrive, PSTR pBuf);
void ShowDriveInfo(HWND hDlg, BOOTSECTOR *bs);
void GetPictRect(HWND hWnd, LPRECT lpRect);
BOOL IsCDRomDrive(int iDrive);
WORD IsNetDrive(int iDrive);
void SetAllLabels(HWND hDlg, LPSTR szText);
int MyGetDriveType(BOOTSECTOR *bs);
/****************************************************************************
FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
PURPOSE: calls initialization function, processes message loop
****************************************************************************/
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc;
HWND hwnd;
DLGPROC dlgProc;
hInst = hInstance;
if (!hPrevInstance)
{
memset(&wc,NULL, sizeof(WNDCLASS));
wc.lpfnWndProc = ClassDlgProc;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = COLOR_WINDOW + 1;
wc.lpszClassName = "BootSectorClass";
RegisterClass(&wc);
}
dlgProc = (DLGPROC)MakeProcInstance(ActualDlgProc, hInst);
hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, dlgProc);
ShowWindow(hwnd,nCmdShow);
while (GetMessage(&msg,NULL,0,0))
{
// if we want to be able to use the TAB key
// to move between controls, the ENTER
// key to execute the default button,
// the ESCAPE key to dismiss the dialog,
// or any other dialog - type functionality,
// we must call IsDialogMessage()
if (!IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
FreeProcInstance((FARPROC)dlgProc);
return msg.wParam;
}
/****************************************************************************
ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam)
PURPOSE:
this function gets placed between the dialog and the DefDlgProc because
1. its a private dialog
and
2. we specified a DLGPROC for the third parameter
of the CreateDialog() call.
we could handle all of the messages here (except for the WM_INITDIALOG
message which is not sent to non-dialogs, or we can pass the messages
off to DefDlgProc(), which will then call our dialog procedure
ActualDlgProc(), given below
****************************************************************************/
long FAR PASCAL __export ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam)
{
return DefDlgProc(hDlg, message, wParam, lParam);
}
/****************************************************************************
ActualDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
PURPOSE:
The procedure for the application that does most of the work.
This is the function that we passed in as the last parameter to the
CreateDialog() call. We do this so that we can get the WM_INITDIALOG
message, which is not passed to the WndProc of a Private Dialog.
We subclass the edit control so that we can restrict input to capital
letters, the backspace key, and the TAB key.
****************************************************************************/
BOOL FAR PASCAL __export ActualDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hMemDC;
static HBITMAP hBitmap, hbmOld;
switch (message)
{
case WM_INITDIALOG:
{
HDC hdc;
HWND hwndEdit;
HMENU hSysMenu;
hSysMenu = GetSystemMenu(hDlg, FALSE);
// disable the "maximize" option in the system menu
EnableMenuItem(hSysMenu, 4, MF_GRAYED|MF_DISABLED|MF_BYPOSITION);
// disable the "size" option of the system menu
EnableMenuItem(hSysMenu, 2, MF_GRAYED|MF_DISABLED|MF_BYPOSITION);
// subclass the edit control so that we
// can restrict input to letter only
hwndEdit = GetDlgItem(hDlg, IDC_DRIVE);
if (hwndEdit)
lpfnOldEditProc = (FARPROC)SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)NewEditProc);
// limit the text of the edit control to 1 character
SendMessage(hwndEdit, EM_LIMITTEXT, 1, 0L);
// put a reasonable default into the edit control
SetWindowText(hwndEdit, "C");
hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_DRIVES));
hdc = GetDC(NULL);
hMemDC = CreateCompatibleDC(hdc);
ReleaseDC(NULL, hdc);
hbmOld = SelectObject(hMemDC, hBitmap);
iDriveType = BM_NONE;
return FALSE; // didn't set the focus
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
BeginPaint(hDlg, &ps);
GetPictRect(hDlg, &rect);
BitBlt(ps.hdc,rect.left, rect.top,
BM_WIDTH, BM_HEIGHT,
hMemDC,
iDriveType * BM_WIDTH,
0,
SRCCOPY);
EndPaint(hDlg, &ps);
}
break;
case WM_COMMAND:
switch (wParam)
{
case ID_ABOUT:
{
DLGPROC dlgprc;
dlgprc = (DLGPROC) MakeProcInstance(AboutDlgProc, hInst);
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hDlg, dlgprc);
FreeProcInstance((FARPROC) dlgprc);
}
break;
case ID_GET:
{
int iDrive;
char szDrive[2];
BOOTSECTOR *bs;
char szBuffer[512];
RECT rect;
BOOL bRet;
// get the rectangle for the bitmap
GetPictRect(hDlg, &rect);
InvalidateRect(hDlg, &rect, TRUE);
// set the focus to the edit control
SetFocus(GetDlgItem(hDlg, IDC_DRIVE));
// hilite the text in the edit control
SendDlgItemMessage(hDlg, IDC_DRIVE, EM_SETSEL,0,MAKELPARAM(0,-1));
GetDlgItemText(hDlg, IDC_DRIVE, szDrive, 2);
iDrive = szDrive[0] - 'A';
if (IsCDRomDrive(iDrive))
{
LoadString(hInst, IDS_NOCDROM, szErrorBuf, sizeof(szErrorBuf));
iDriveType = BM_CDROM;
// if the MessageBox has no caption, Windows
// will put "Error" as the caption, so we can
// save some space by leaving it off...
MessageBox(hDlg, szErrorBuf, "", MB_ICONSTOP|MB_OK);
SetAllLabels(hDlg, "N/A");
UpdateWindow(hDlg);
break;
}
if (IsNetDrive(iDrive))
{
LoadString(hInst, IDS_NONET, szErrorBuf, sizeof(szErrorBuf));
iDriveType = BM_NET;
MessageBox(hDlg, szErrorBuf, "", MB_ICONSTOP|MB_OK);
SetAllLabels(hDlg, "N/A");
UpdateWindow(hDlg);
break;
}
SetCapture(hDlg);
SetCursor(LoadCursor(NULL, IDC_WAIT));
bRet = ReadBootSector(iDrive, szBuffer);
ReleaseCapture();
SetCursor(LoadCursor(NULL, IDC_ARROW));
if (bRet)
{
int nDriveType;
bs = (BOOTSECTOR *)szBuffer;
ShowDriveInfo(hDlg, bs);
nDriveType = MyGetDriveType(bs);
switch (nDriveType)
{
case DRIVE_REMOVABLE:
iDriveType = BM_FLOPPY;
break;
case DRIVE_RAM:
iDriveType = BM_RAM;
break;
case DRIVE_FIXED:
iDriveType = BM_FIXED;
break;
default: // this should never happen!!
iDriveType = BM_NONE;
break;
}
}
else // failed to read boot sector!
{
LoadString(hInst, IDS_READERROR, szErrorBuf, sizeof(szErrorBuf));
iDriveType = BM_NONE;
MessageBox(hDlg, szErrorBuf, "", MB_ICONSTOP|MB_OK);
SetAllLabels(hDlg, "N/A");
UpdateWindow(hDlg);
}
}
break;
case IDCANCEL:
DestroyWindow(hDlg);
break;
default:
return FALSE; // didn't handle it
break;
}
break;
case WM_DESTROY:
SelectObject(hMemDC, hbmOld);
DeleteObject(hBitmap);
DeleteDC(hMemDC);
PostQuitMessage(0);
break;
default:
return FALSE; //didn't handle the message
break;
}
return TRUE; // we handled the message
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -