#include <windows.h>
#include <stdio.h>
#include "resource.h"
//函数声明
LRESULT CALLBACK MyFloppyWriterProc(HWND, UINT, WPARAM, LPARAM);
void WriteBintoFloppy(HWND);
void WriteBinToImage(HWND);
/****************************************************************************************
* 函数: WinMain(HINSTANCE, HINSTANCE, PSTR, int)
* 目的:程序入口函数
****************************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iShowCmd)
{
MSG msg;
HWND hwnd;
static TCHAR szAppName[] = TEXT("MyFloppyWriter");
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPICON));
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = MyFloppyWriterProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = 0;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("错误!"), TEXT("运行此程序要求NT系统!"), MB_OK);
}
hwnd = CreateWindow(szAppName, TEXT("软盘绝对扇区写工具 MyFloppyWriter"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/****************************************************************************************
* 函数:MyFloppyWriterProc(HWND, UINT, WPARAM, LPARAM)
* 目的:程序的主消息处理函数
****************************************************************************************/
LRESULT CALLBACK MyFloppyWriterProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hStcWriteBytes;
HWND hEdtWriteBytes;//要写的字节数
HWND hBtnWriterFileToFloppy;
HWND hBtnWriterFielToImage;
int cxClient = 0, cyClient = 0;
HFONT hFont;
switch(msg) {
case WM_CREATE://STATIC EDIT
//要写的字节数
hStcWriteBytes = CreateWindow(
TEXT("STATIC"), TEXT("写入字节数:"), WS_CHILD |
WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE,
125, 100, 100, 18, hwnd, (HMENU)IDB_SSIZE,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hEdtWriteBytes = CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("EDIT"), TEXT("1024"), WS_VISIBLE |
WS_CHILD | ES_AUTOHSCROLL | ES_NUMBER,
225, 100, 50, 20, hwnd, (HMENU)IDB_ESIZE,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
//按钮
hBtnWriterFileToFloppy = CreateWindow(
TEXT("BUTTON"), TEXT("把文件写入软盘"), WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
125, 150, 150, 25, hwnd, (HMENU)IDB_WRITEFILETOFLOPPY,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hBtnWriterFielToImage = CreateWindow(
TEXT("BUTTON"), TEXT("把文件写入映像"), WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
125, 200, 150, 25, hwnd, (HMENU)IDB_WRITEFILETOIMAGE,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hFont = GetStockObject(SYSTEM_FIXED_FONT);
SendMessage(hStcWriteBytes, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
SendMessage(hEdtWriteBytes, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
SendMessage(hBtnWriterFileToFloppy, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
SendMessage(hBtnWriterFielToImage, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(FALSE, 0));
return 0;
case WM_COMMAND:
if(BN_CLICKED == HIWORD(wParam)){
switch(LOWORD(wParam)){
case IDB_WRITEFILETOFLOPPY:
WriteBintoFloppy(hwnd);
break;
case IDB_WRITEFILETOIMAGE:
WriteBinToImage(hwnd);
break;
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
/****************************************************************************************
* 函数:WriteBintoFloppy(HWND)
* 目的:把1k的映像写入软盘的1、2扇区
****************************************************************************************/
void WriteBintoFloppy(HWND hwnd)
{
unsigned char *puchBootData = 0;
unsigned char *pBuffer = 0;
unsigned long dwFileSizeHigh, dwBytesWritten, dwRead, dwFilesize;
HANDLE hBootBin, hFloppy;
unsigned int nWriteBytes = 0;
unsigned long dwBytesRead;
OPENFILENAME ofn;
char szFile[MAX_PATH] = TEXT("boot.bin");
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "二进制文件(*.bin)\0*.bin\0All(*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = "打开文件";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
nWriteBytes = GetDlgItemInt(hwnd, IDB_ESIZE, NULL, FALSE);
puchBootData = (unsigned char*)malloc(nWriteBytes);
pBuffer = (unsigned char*)malloc(nWriteBytes);
if (GetOpenFileName(&ofn)==FALSE){
return;
}
hBootBin = CreateFile(szFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
dwFilesize = GetFileSize(hBootBin, &dwFileSizeHigh);
if (dwFilesize <= 0) {
MessageBox(hwnd, "文件错误!", "Error", MB_OK);
return;
}
if (!ReadFile(hBootBin, puchBootData, dwFilesize, &dwRead, NULL)) {
int iErr;
char szError[128];
iErr = GetLastError();
sprintf(szError, "文件读取错误!\n错误代码: %d", iErr);
MessageBox(hwnd, szError, "Error", MB_OK);
return;
}
CloseHandle(hBootBin);
hFloppy = CreateFile("\\\\.\\A:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hFloppy == INVALID_HANDLE_VALUE)
{
MessageBox(hwnd, TEXT("打开软盘错误!"), TEXT("软盘错误"), MB_OK);
return;
}
//移动文件指针到需要读取的扇区的开始位置
SetFilePointer(hFloppy, 0, NULL, FILE_BEGIN);
if (!WriteFile(hFloppy, puchBootData, nWriteBytes, &dwBytesWritten, NULL))
{
int err;
char error[10];
err=GetLastError ();
itoa (err, error, 10);
MessageBox(hwnd, error, "写扇区失败! ", MB_OK);
return ;
}
SetFilePointer(hFloppy, 0, NULL, FILE_BEGIN);
//测试写软驱是否成功!
if (!ReadFile(hFloppy, pBuffer, nWriteBytes, &dwBytesRead, NULL))
{
int err;
char error[10];
err=GetLastError();
itoa (err, error, 10);
MessageBox (hwnd, error, "读取扇区失败!", MB_OK);
return;
}
//关闭软驱句柄
CloseHandle(hFloppy);
//释放分配的内存
free(puchBootData);
free(pBuffer);
MessageBox(hwnd, "成功!", "MyFloppyWriter", MB_OK);
}
/****************************************************************************************
* 函数:WriteBinToImage(HWND)
* 目的:把一般文件写入映像
****************************************************************************************/
void WriteBinToImage(HWND hwnd)
{
unsigned char *puchBootData = 0;
HANDLE hFileWriteToImage, hFileImage;
DWORD dwBytesWritten;
DWORD dwFileSizeHigh = 0;
DWORD dwFilesize = 0;
DWORD dwRead = 0;
unsigned int nWriteBytes = 0;
OPENFILENAME ofn; // 对话框结构体
char szFile[MAX_PATH] = "boot.bin"; // 存放文件名
char szFileFilterBIN[255] = "二进制文件(*.bin)\0*.bin\0All(*.*)\0*.*\0";
char szFileFilterIMG[255] = "镜像文件(*.img)\0*.img\0All(*.*)\0*.*\0";
char szOpenFileDlgBoxTitle[40] = "打开二进制文件";
// 初始化 OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = szFileFilterBIN;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
of