一、同步问题的解决方式:
1.同步代码块方法;
2.互斥量同步方法;
3.信号量同步方法;
4.事件同步方法。临界区同步/保护代码块同步步骤:
1.定义临界区变量;
2.临界区变量初始化;
3.进入临界区;
4.离开临界区;
5.删除临界区。
只应用于单进程程序#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
HANDLE hWriteThread;
HANDLE hReadThread;
CRITICAL_SECTION m_section;//临界区变量
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd1, UINT msg1, WPARAM wParam1, LPARAM lParam1);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
DWORD WINAPI writeThread(LPVOID lpParameter);
DWORD WINAPI readThread(LPVOID lpParameter);
typedef struct Data{
TCHAR name[20];
int age;
}DATA;
DATA data;
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
InitializeCriticalSection(&m_section);//临界区初始化
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
OnBTNClick(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON2:
OnClose(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DeleteCriticalSection(&m_section);
CloseHandle(hWriteThread);
CloseHandle(hReadThread);
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwritelist,hreadlist;
DWORD dwthread1,dwthread2;
hwritelist = GetDlgItem(hwnd,IDC_LIST1);
hreadlist = GetDlgItem(hwnd,IDC_LIST2);
hWriteThread = CreateThread(NULL,0,writeThread,(LPVOID)hwritelist,0,&dwthread1);
hReadThread = CreateThread(NULL,0,readThread,(LPVOID)hreadlist,0,&dwthread2);
return TRUE;
}
DWORD WINAPI writeThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
EnterCriticalSection(&m_section);
//临界区同步或保护代码块同步
//在EnterCriticalSection和LeaveCriticalSection之间的代码成为临界区代码,只要进入此区域,cpu不能在访问其他区域
if(i % 2 == 0)
{
wcscpy(data.name,TEXT("kiwin"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 20;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
else
{
wcscpy(data.name,TEXT("susan"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 22;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
LeaveCriticalSection(&m_section);
}
return 0;
}
DWORD WINAPI readThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
EnterCriticalSection(&m_section);
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);//取出name
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);//取出age
LeaveCriticalSection(&m_section);
}
return 0;
}
互斥量同步
适用于多进程多线程之间访问#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
HANDLE hWriteThread;
HANDLE hReadThread;
HANDLE m_mutex;//互斥量对象
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd1, UINT msg1, WPARAM wParam1, LPARAM lParam1);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
DWORD WINAPI writeThread(LPVOID lpParameter);
DWORD WINAPI readThread(LPVOID lpParameter);
typedef struct Data{
TCHAR name[20];
int age;
}DATA;
DATA data;
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_mutex = CreateMutex(NULL,FALSE,TEXT("mutex"));//互斥对象的创建
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
OnBTNClick(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON2:
OnClose(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CloseHandle(m_mutex);
CloseHandle(hWriteThread);
CloseHandle(hReadThread);
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwritelist,hreadlist;
DWORD dwthread1,dwthread2;
hwritelist = GetDlgItem(hwnd,IDC_LIST1);
hreadlist = GetDlgItem(hwnd,IDC_LIST2);
hWriteThread = CreateThread(NULL,0,writeThread,(LPVOID)hwritelist,0,&dwthread1);
hReadThread = CreateThread(NULL,0,readThread,(LPVOID)hreadlist,0,&dwthread2);
return TRUE;
}
DWORD WINAPI writeThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if(WaitForSingleObject(m_mutex,-1) == WAIT_OBJECT_0)
{
if(i % 2 == 0)
{
wcscpy(data.name,TEXT("kiwin"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 20;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
else
{
wcscpy(data.name,TEXT("susan"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 22;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
}
ReleaseMutex(m_mutex);
}
return 0;
}
DWORD WINAPI readThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if (WaitForSingleObject(m_mutex,-1) == WAIT_OBJECT_0)
{
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);//取出name
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);//取出age
}
ReleaseMutex(m_mutex);
}
return 0;
}
信号量同步方式
能够应用在一个生产者,多各个消费者的模式中
#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
HANDLE hWriteThread;
HANDLE hReadThread;
HANDLE m_signal;//信号量
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd1, UINT msg1, WPARAM wParam1, LPARAM lParam1);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
DWORD WINAPI writeThread(LPVOID lpParameter);
DWORD WINAPI readThread(LPVOID lpParameter);
typedef struct Data{
TCHAR name[20];
int age;
}DATA;
DATA data;
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_signal = CreateSemaphore(NULL,1,1,NULL);
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
OnBTNClick(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON2:
OnClose(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CloseHandle(m_signal);
CloseHandle(hWriteThread);
CloseHandle(hReadThread);
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwritelist,hreadlist;
DWORD dwthread1,dwthread2;
hwritelist = GetDlgItem(hwnd,IDC_LIST1);
hreadlist = GetDlgItem(hwnd,IDC_LIST2);
hWriteThread = CreateThread(NULL,0,writeThread,(LPVOID)hwritelist,0,&dwthread1);
hReadThread = CreateThread(NULL,0,readThread,(LPVOID)hreadlist,0,&dwthread2);
return TRUE;
}
DWORD WINAPI writeThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if(WaitForSingleObject(m_signal,-1) == WAIT_OBJECT_0)
{
if(i % 2 == 0)
{
wcscpy(data.name,TEXT("kiwin"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 20;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
else
{
wcscpy(data.name,TEXT("susan"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 22;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
}
ReleaseSemaphore(m_signal,1,NULL);
}
return 0;
}
DWORD WINAPI readThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if(WaitForSingleObject(m_signal,-1) == WAIT_OBJECT_0)
{
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);//取出name
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);//取出age
}
ReleaseSemaphore(m_signal,1,NULL);
}
return 0;
}
事件同步方式
#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
HANDLE hWriteThread;
HANDLE hReadThread;
HANDLE m_event;//事件对象
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd1, UINT msg1, WPARAM wParam1, LPARAM lParam1);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
DWORD WINAPI writeThread(LPVOID lpParameter);
DWORD WINAPI readThread(LPVOID lpParameter);
typedef struct Data{
TCHAR name[20];
int age;
}DATA;
DATA data;
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_event = CreateEvent(NULL,FALSE,TRUE,NULL);
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
OnBTNClick(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON2:
OnClose(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CloseHandle(m_event);
CloseHandle(hWriteThread);
CloseHandle(hReadThread);
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hwritelist,hreadlist;
DWORD dwthread1,dwthread2;
hwritelist = GetDlgItem(hwnd,IDC_LIST1);
hreadlist = GetDlgItem(hwnd,IDC_LIST2);
hWriteThread = CreateThread(NULL,0,writeThread,(LPVOID)hwritelist,0,&dwthread1);
hReadThread = CreateThread(NULL,0,readThread,(LPVOID)hreadlist,0,&dwthread2);
return TRUE;
}
DWORD WINAPI writeThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if(WaitForSingleObject(m_event,-1) == WAIT_OBJECT_0)
{
if(i % 2 == 0)
{
wcscpy(data.name,TEXT("kiwin"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 20;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
else
{
wcscpy(data.name,TEXT("susan"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);
Sleep(30);
data.age = 22;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);
}
}
SetEvent(m_event);
}
return 0;
}
DWORD WINAPI readThread(LPVOID lpParameter)
{
int i;
HWND hlist;
TCHAR buf[64];
hlist = (HWND)lpParameter;
while (!ListBox_DeleteString(hlist,0))
continue;
for (i = 0;i < 20;i++)
{
if(WaitForSingleObject(m_event,-1) == WAIT_OBJECT_0)
{
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hlist,buf);//取出name
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hlist,buf);//取出age
}
SetEvent(m_event);
}
return 0;
}
二、文件的基本操作
文件的创建和打开
CreateFile()
文件的关闭
CloseHandle()
文件的读写
WriteFile()/ReadFile()
文件指针的移动
SetFilePointer()
获取/设置文件属性
GetFileAttributes()/SetFileAttributes()
获取/设置文件时间
GetFileTime()/SetFileTime()
获取文件大小
GetFileSize()
#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNCreateFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNWriteFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNReadFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_event = CreateEvent(NULL,FALSE,TRUE,NULL);
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
OnBTNCreateFile(hwnd,msg,wParam,lParam);
break;
case IDCANCEL:
OnClose(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON2:
OnBTNWriteFile(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON3:
OnBTNReadFile(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON4:
OnBTNMessage(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNCreateFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HANDLE hFile;
hFile = CreateFile(TEXT("\\Temp\\a.txt"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}
LONG OnBTNWriteFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HANDLE hFile;
LPCTSTR buf;
DWORD dwWrite;
buf = TEXT("welcome to wince");
dwWrite = -1;
hFile = CreateFile(TEXT("\\Temp\\a.txt"),GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
SetFilePointer(hFile,0,NULL,FILE_END);
WriteFile(hFile,buf,wcslen(buf)*2,&dwWrite,NULL);
CloseHandle(hFile);
return TRUE;
}
LONG OnBTNReadFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HANDLE hFile;
TCHAR buf[64];
DWORD dwRead;
HWND hedit;
dwRead = -1;
hFile = CreateFile(TEXT("\\Temp\\a.txt"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
ReadFile(hFile,(LPVOID)buf,64,&dwRead,NULL);
hedit = GetDlgItem(hwnd,IDC_EDIT1);
Edit_SetText(hedit,buf);
CloseHandle(hFile);
return TRUE;
}
LONG OnBTNMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DWORD dwFile;
HANDLE hFile;
FILETIME createtime,lasttime,lastwirtetime;
SYSTEMTIME st;
TCHAR buf[64];
HWND hedit;
FILETIME lt;
DWORD dwSize;
hFile = CreateFile(TEXT("\\Temp\\a.txt"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
dwFile = GetFileAttributes(TEXT("\\Temp\\a.txt"));
if (dwFile & FILE_ATTRIBUTE_NORMAL == 1)
{
MessageBox(hwnd,TEXT("Normal file"),TEXT("message"),0);
}
if(dwFile & FILE_ATTRIBUTE_TEMPORARY == 1)
{
MessageBox(hwnd,TEXT("Normal file"),TEXT("message"),0);
}
GetFileTime(hFile,&createtime,&lasttime,&lastwirtetime);
FileTimeToLocalFileTime(&createtime,<);
FileTimeToSystemTime(<,&st);
wsprintf(buf,TEXT("%d - %d - %d %d : %d : %d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
hedit = GetDlgItem(hwnd,IDC_EDIT1);
dwSize = GetFileSize(hFile,NULL);
wsprintf(buf,TEXT("%d"),dwSize);
Edit_SetText(hedit,buf);
CloseHandle(hFile);
return TRUE;
}
在开发板的Temp文件夹下创建txt文件

写入文件,在Temp中打开查看内容

读取文件

获取文件大小
三、文件系统的基本操作
目录的创建、删除
文件的拷贝、移动、删除
文件的查找
创建目录
CreateDirectory()
删除目录
RemoveDircetory()
文件的拷贝
CopyFile()
移动文件
MoveFile()
删除文件
DeleteFile()
查找文件
FindFirestFile()
FindNextFile()
FindClose()
#include <Windows.h>
#include <WindowsX.h>
#include <WinGDI.h>
#include <WinDef.h>
#include "resource.h"
HINSTANCE ins;
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNCreateDirectory(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNRemoveDirectory(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNCopyFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNMoveFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNDeleteFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LONG OnBTNFindFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WinMain( HINSTANCE hInstance, //句柄
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, //应用程序的启动参数
int nShowCmd) //显示方式
{
ins = hInstance;//全局变量参数传递
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 1;
}
LONG OnCreate(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DialogBox(ins,MAKEINTRESOURCE(IDD_DIALOG1),NULL,About);
return 0;
}
BOOL CALLBACK About(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_INITDIALOG)//初始化对话框
{
OnInit(hwnd,msg,wParam,lParam);
}
if (msg == WM_COMMAND)//命令消息的处理
{
OnCommand(hwnd,msg,wParam,lParam);
}
return FALSE;
}
BOOL OnInit(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return TRUE;
}
BOOL OnCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON2:
OnBTNCreateDirectory(hwnd,msg,wParam,lParam);
break;
case IDCANCEL:
OnClose(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON3:
OnBTNRemoveDirectory(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON4:
OnBTNCopyFile(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON5:
OnBTNMoveFile(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON6:
OnBTNDeleteFile(hwnd,msg,wParam,lParam);
break;
case IDC_BUTTON1:
OnBTNFindFile(hwnd,msg,wParam,lParam);
break;
}
return TRUE;
}
LONG OnClose(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNClick(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
EndDialog(hwnd,0);
return TRUE;
}
LONG OnBTNCreateDirectory(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (CreateDirectory(TEXT("\\Program Files\\abc"),NULL) == 1)
{
MessageBox(hwnd,TEXT("创建成功!"),TEXT("message"),0);
}
return TRUE;
}
LONG OnBTNRemoveDirectory(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(RemoveDirectory(TEXT("\\Program Files\\abc")) == 1)
{
MessageBox(hwnd,TEXT("删除成功!"),TEXT("message"),0);
}
return TRUE;
}
LONG OnBTNCopyFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(CopyFile(TEXT("\\Temp\\a.txt"),TEXT("\\Program Files\\abc\\a.txt"),TRUE) == 1)
{
MessageBox(hwnd,TEXT("拷贝成功!"),TEXT("message"),0);
}
return TRUE;
}
LONG OnBTNMoveFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(MoveFile(TEXT("\\Program Files\\abc\\a.txt"),TEXT("\\Temp\\b.txt")) == 1)
{
MessageBox(hwnd,TEXT("移动成功!"),TEXT("message"),0);
}
return TRUE;
}
LONG OnBTNDeleteFile(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
if( DeleteFile(TEXT("\\Program Files\\abc\\b.txt"))== 1)
{
MessageBox(hwnd,TEXT("删除成功!"),TEXT("message"),0);
}
return TRUE;
}
LONG OnBTNFindFile(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WIN32_FIND_DATA data;
HANDLE hFile;
HWND hlist;
hlist = GetDlgItem(hwnd,IDC_LIST1);
hFile = FindFirstFile(TEXT("\\*.*"),&data);
if (hFile == -1)
{
return FALSE;
}
do
{
ListBox_AddString(hlist,data.cFileName);
} while (FindNextFile(hFile,&data));
FindClose(hFile);
return TRUE;
}
创建文件夹
在Program Files文件在下创建abc文件夹
删除abc文件夹
同目录下拷贝,即将文件更名将a变为b
拷贝文件
将更名后的文件从abc文件夹移动到Temp中
移动文件
查找根目录下的所有文件