《Windows程序设计实验报告.doc》由会员分享,可在线阅读,更多相关《Windows程序设计实验报告.doc(31页珍藏版)》请在三一办公上搜索。
1、Windows程序设计实 验 报 告专 业: 计算机科学与技术 班 级: 0309402 学 号: 姓 名: 实验一 错误处理一、实验目的1、了解windows系统的错误处理方式、方法。2、定义自己的错误代码,加深对windows系统错误处理方式的理解。二、实验环境Windows Xp Visual studio 6.0三、实验内容编写一个错误查找程序,输入错误代号,得到相应的错误描述信息。四、实验方法、步骤代码: /* GetErrorInformation.h */#include windows.h#include Tchar.h/对错误处理的封装类class GetErrorInfor
2、mationprivate:HLOCAL m_hlocal; int m_iErrorCode;TCHAR *m_pcErrorInformation;public:GetErrorInformation();GetErrorInformation();void SetErrorCode(int ErrorCode);TCHAR * GetErrorString();protected:private:;/* GetErrorInformation.cpp */#include StdAfx.h#include Windows.h#include Tchar.h#include GetErro
3、rInformation.hGetErrorInformation:GetErrorInformation()m_hlocal = NULL;m_pcErrorInformation = NULL;GetErrorInformation:GetErrorInformation()if (m_hlocal != NULL) LocalFree(m_hlocal); elsefree(m_pcErrorInformation);void GetErrorInformation:SetErrorCode(int ErrorCode)this-m_iErrorCode = ErrorCode;TCHA
4、R * GetErrorInformation:GetErrorString() / Get the error codes textual description BOOL fOk = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, m_iErrorCode, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (PTSTR) &m_hlocal, 0, NULL); if (!
5、fOk) / Is it a network-related error? HMODULE hDll = LoadLibraryEx(TEXT(netmsg.dll), NULL, DONT_RESOLVE_DLL_REFERENCES); if (hDll != NULL) FormatMessage( FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_MAX_WIDTH_MASK, hDll, m_iErrorCode, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLI
6、SH_US), (PTSTR) &m_hlocal, 0, NULL); FreeLibrary(hDll); if (m_hlocal!=NULL) m_pcErrorInformation = (char*)m_hlocal; else m_pcErrorInformation = (TCHAR *)malloc(30*sizeof(TCHAR); TCHAR *charError = _TEXT(你所查找的错误代码不存在); lstrcpy(m_pcErrorInformation,charError); return m_pcErrorInformation;/* errorClass
7、Dlg.cpp */void CErrorClassDlg:OnButtonErrorInformationFind() / TODO: Add your control notification handler code hereint errorCode;GetErrorInformation gei;errorCode = GetDlgItemInt(IDC_EDIT_ERROR_CODE);gei.SetErrorCode(errorCode);SetDlgItemText(IDC_EDIT_ERROR_INFORMATION,gei.GetErrorString();五、实验结果记录
8、与分析截图:实验二 字符和字符串处理一、实验目的1、了解windows中的Unicode字符和ANSI字符、字符串类型。2、掌握ANSI字符类型与Unicode的区别及转换。3、掌握Unicode字符的使用和处理方式。二、实验环境Windows Xp Visual studio 6.0三、实验内容编写一个Unicode字符及字符串处理程序。使用TCHAR、LPTSTR、LPCTSTR等数据类型,掌握这类数据类型的使用。四、实验方法、步骤代码:/* UChar.h */#ifndef _UCHAR_H_#define _UCHAR_H_#include windows.h#include ios
9、treamusing namespace std;class UCharfriend ostream &operator(ostream &os,UChar &uc)osuc.m_pUCharendl;osuc.m_Length(UChar uchar2);UINT Length();virtual UChar();protected:private:;#endif/* UChar.cpp */#include UChar.hUChar:UChar()m_pUChar = NULL;m_Length = 0;UChar:UChar(const TCHAR *pChar)int iCLen; i
10、CLen = lstrlen(pChar)+sizeof(TCHAR);m_Length = iCLen-sizeof(TCHAR);m_pUChar = (TCHAR *)malloc(iCLen*sizeof(TCHAR);lstrcpy(m_pUChar,pChar);UChar& UChar:operator+(UChar uchar2)int iCLen;LPTSTR pTem;iCLen = lstrlen(this-m_pUChar)+lstrlen(uchar2.m_pUChar)+sizeof(TCHAR);m_Length = iCLen-sizeof(TCHAR); pT
11、em = (PTSTR)malloc(iCLen*sizeof(TCHAR);lstrcpy(pTem,this-m_pUChar);if (this-m_pUChar != NULL)free(this-m_pUChar);lstrcat(pTem,uchar2.m_pUChar);this-m_pUChar = pTem;return *this;UChar& UChar:operator=(const TCHAR * pStr2)int iCLen; iCLen = lstrlen(pStr2)+sizeof(TCHAR);m_Length = iCLen-sizeof(TCHAR);i
12、f (this-m_pUChar != NULL)free(this-m_pUChar);this-m_pUChar = (TCHAR *)malloc(iCLen);lstrcpy(this-m_pUChar,pStr2);return *this;UChar& UChar:operator=(UChar uchar2)int iCLen; iCLen = lstrlen(uchar2.m_pUChar)+sizeof(TCHAR);m_Length = iCLen-sizeof(TCHAR); if (this-m_pUChar != NULL)free(this-m_pUChar);m_
13、pUChar = (TCHAR *)malloc(iCLen);lstrcpy(m_pUChar,uchar2.m_pUChar);return *this;BOOL UChar:operator(UChar uchar2)return lstrcmp(this-m_pUChar,uchar2.m_pUChar);UINT UChar:Length()return this-m_Length;UChar:UChar()if (m_pUChar!=NULL)free(m_pUChar);/* uincode.cpp */#include stdafx.h#include UChar.h#incl
14、ude iostream#include TChar.husing namespace std;int main(int argc, char* argv)UChar u1(_TEXT(爸爸妈妈你们好吗!);UChar u2(_TEXT(爷爷奶奶你们好吗!);BOOL b;coutu1endl;coutu2endl;u1 = _TEXT(哥哥姐姐你们好吗!);u2 = _TEXT(叔叔伯伯你们好吗!); coutu1endl;coutu2u2;coutb = bendl;/u1+u2;coutu1 = u1endl;u1 = u2;coutu1 = u1LoadIcon(IDR_MAINFRA
15、ME);void CGUIDDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CGUIDDlg)DDX_Control(pDX, IDC_EDIT_GUID, m_eGuid);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CGUIDDlg, CDialog)/AFX_MSG_MAP(CGUIDDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_CREAT_NE
16、W, OnButtonCreatNew)/AFX_MSG_MAPEND_MESSAGE_MAP()void CGUIDDlg:OnButtonCreatNew() CString strGUID;HRESULT hResult;GUID *pguid = new GUID();hResult = CoCreateGuid(pguid);strGUID.Format(%-x-%-x-%-x-%-x%-x%-x%-x%-x%-x%-x%-x,pguid-Data1,pguid-Data2,pguid-Data3,pguid-Data40,pguid-Data41,pguid-Data42,pgui
17、d-Data43,pguid-Data44,pguid-Data45,pguid-Data46,pguid-Data47);m_eGuid.SetWindowText(strGUID);五、实验结果记录与分析实验四 进程一、实验目的1、了解windows进程原理及运行机制,进程的环境变量。2、掌握进程的创建、销毁方法。二、实验环境Windows Xp Visual studio 6.0三、实验内容编写程序,创建进程的环境变量、创建进程及销毁进程。使用CreateProcess、ExitProcess、TerminateProcess等方法。四、实验方法、步骤代码:/*EnvironmentVa
18、riableDlg.cpp */void CEnvironmentVariableDlg:OnButtonSetenvironment() / TODO: Add your control notification handler code hereCString strEnvironmentName;CString strEnvironmentValue;m_eSetEnvironmentName.GetWindowText(strEnvironmentName);m_eSetEnvironmentValue.GetWindowText(strEnvironmentValue);if (!s
19、trEnvironmentName.IsEmpty()&!strEnvironmentValue.IsEmpty() BOOL ret = SetEnvironmentVariable(strEnvironmentName.GetBuffer(0),strEnvironmentValue.GetBuffer(0);if (ret)MessageBox(插入环境变量成功!);elseMessageBox(插入环境变量失败!);strEnvironmentValue.ReleaseBuffer();strEnvironmentName.ReleaseBuffer(); elseMessageBox
20、(环境变量名或环境变量值不能为空!);void CEnvironmentVariableDlg:OnButtonGetenvironment() / TODO: Add your control notification handler code hereGetEnvironmentValue(你查找的环境变量并不存在);void CEnvironmentVariableDlg:GetEnvironmentValue(CString str) int rCnt;CString strEnvironmentName; char strEnvironmentValueENVIRONMENT_MAX
21、;m_eGetEnvironmentName.GetWindowText(strEnvironmentName); m_eGetEnvironmentValue.SetWindowText();if (!strEnvironmentName.IsEmpty() rCnt = GetEnvironmentVariable(strEnvironmentName.GetBuffer(0),strEnvironmentValue,ENVIRONMENT_MAX);if (rCnt!=0) strEnvironmentValuerCnt = 0;m_eGetEnvironmentValue.SetWin
22、dowText(strEnvironmentValue); elseMessageBox(str);strEnvironmentName.ReleaseBuffer(); elseMessageBox(环境变量名不能为空!);void CEnvironmentVariableDlg:OnButtonCurrentDirectory() / TODO: Add your control notification handler code hereDWORD nBufferLength; / size of directory buffer TCHAR lpBufferMAX_PATH; nBuf
23、ferLength = GetCurrentDirectory(MAX_PATH, lpBuffer); MessageBox(lpBuffer);void CEnvironmentVariableDlg:OnBUTTONGetFullPathName() / TODO: Add your control notification handler code here DWORD nBufferLength; / size of path buffer TCHAR lpBufferMAX_PATH; / path buffer TCHAR FilePartMAX_PATH; TCHAR *lpF
24、ilePart;/ address of file name in path lpFilePart = FilePart;nBufferLength = GetFullPathName(TEXT(e:),MAX_PATH,lpBuffer,&lpFilePart);MessageBox(lpBuffer);MessageBox(lpFilePart);/*GlobleParamDlg.cpp */void CGlobleParamDlg:OnButtonWinmajor() / TODO: Add your control notification handler code hereCStri
25、ng str;str.Format(操作系统主版本号为%d,_winmajor);this-MessageBox(str);void CGlobleParamDlg:OnButtonWinminor() / TODO: Add your control notification handler code hereCString str;str.Format(操作系统次版本号为%d,_winminor);this-MessageBox(str);void CGlobleParamDlg:OnButtonAgc() / TODO: Add your control notification han
26、dler code hereCString str;str.Format(GUI没有argc);this-MessageBox(str);void CGlobleParamDlg:OnButtonEnviron() / TODO: Add your control notification handler code hereCString str;str.Format(环境变量为%s,_wenviron);this-MessageBox(str);void CGlobleParamDlg:OnButtonPgmptr() / TODO: Add your control notificatio
27、n handler code hereCString str;str.Format(全路径和名字%s,_pgmptr);this-MessageBox(str);void CGlobleParamDlg:OnButtonAgv() / TODO: Add your control notification handler code hereCString str;str.Format(GUI没有argv);this-MessageBox(str);void CGlobleParamDlg:OnButtonDlladdr() / TODO: Add your control notificati
28、on handler code hereCString str;UpdateData(true);MessageBox(m_dllAddr.GetBuffer(0);HMODULE hModule = :GetModuleHandle(m_dllAddr.GetBuffer(0);m_dllAddr.ReleaseBuffer(); str.Format(动态链接库加载地址为0X%8x,(DWORD)hModule);this-MessageBox(str);void CGlobleParamDlg:OnButtonCommandline() / TODO: Add your control
29、notification handler code herePTSTR clStr;clStr = :GetCommandLine();MessageBox(clStr);/*OpenOrCloseProcessDlg.cpp */void COpenOrCloseProcessDlg:OnButtonUpdate() UpdateProcessInformation();return ;void COpenOrCloseProcessDlg:InitalListCtr()m_ctrListCtrProcessInformation.InsertColumn(0,序号);m_ctrListCt
30、rProcessInformation.InsertColumn(1,进程名);m_ctrListCtrProcessInformation.InsertColumn(2,进程ID);CRect rect;m_ctrListCtrProcessInformation.GetWindowRect(&rect);m_ctrListCtrProcessInformation.SetColumnWidth(0,rect.Width()/4);m_ctrListCtrProcessInformation.SetColumnWidth(1,rect.Width()/2);m_ctrListCtrProce
31、ssInformation.SetColumnWidth(2,rect.Width()/4);void COpenOrCloseProcessDlg:OnButtonProcessDelete() / TODO: Add your control notification handler code hereCString strPID;DWORD dwPID;int iSelect;GetDlgItem(IDC_EDIT_DELETE_PROCESS_ID)-GetWindowText(strPID);if ( = strPID)MessageBox(请选择要删除的进程);return;iSe
32、lect = MessageBox(真的要终止该进程吗?,终止进程,MB_YESNO);if (iSelect = IDYES)dwPID = atoi(strPID);HANDLE hProcess = :OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwPID);if (hProcess!=NULL):TerminateProcess(hProcess,0);elseMessageBox(打开进程出错!,出错);elsereturn;GetDlgItem(IDC_EDIT_DELETE_PROCESS_ID)-SetWindowText();UpdateProc
33、essInformation();void COpenOrCloseProcessDlg:OnButtonProcessStart() / TODO: Add your control notification handler code hereCString pathName;CString strFileFilter = 可执行文件|*.exe;CFileDialog dFileDlg(TRUE,exe,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST,strFileFilter);i
34、f (dFileDlg.DoModal()=IDOK)pathName = dFileDlg.GetPathName();/MessageBox(pathName);STARTUPINFO si = sizeof(si) ;PROCESS_INFORMATION pi;BOOL bRet = :CreateProcess(NULL, pathName.GetBuffer(0), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);if (!bRet)printf(启动进程失败);return ;CloseHandle(pi.hProcess);CloseHandle(pi.hThread);UpdateProcessInformation();void COpenOrCloseProcessDlg:OnClickListProcess(NMHDR* pNMHDR, LRESULT* pResult) / TODO: Add your control notification handler code hereCString str;POSITION pos; pos = m_ctrListCtrProcessInformation.GetFirstSelectedItemP