Windows程序设计实验报告

上传人:沈*** 文档编号:82302336 上传时间:2022-04-28 格式:DOC 页数:31 大小:224.50KB
收藏 版权申诉 举报 下载
Windows程序设计实验报告_第1页
第1页 / 共31页
Windows程序设计实验报告_第2页
第2页 / 共31页
Windows程序设计实验报告_第3页
第3页 / 共31页
资源描述:

《Windows程序设计实验报告》由会员分享,可在线阅读,更多相关《Windows程序设计实验报告(31页珍藏版)》请在装配图网上搜索。

1、Windows程序设计实验Windows程序设计实 验 报 告专 业: 计算机科学与技术 班 级: 0309402 学 号: 姓 名: 实验一 错误处理一、实验目的1、了解windows系统的错误处理方式、方法。2、定义自己的错误代码,加深对windows系统错误处理方式的理解。二、实验环境Windows Xp Visual studio 6.0三、实验内容编写一个错误查找程序,输入错误代号,得到相应的错误描述信息。四、实验方法、步骤代码: /* GetErrorInformation.h */#include windows.h#include Tchar.h/对错误处理的封装类class

2、GetErrorInformationprivate: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#in

3、clude GetErrorInformation.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 = E

4、rrorCode;TCHAR * 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,

5、 NULL); if (!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,

6、SUBLANG_ENGLISH_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;

7、/* errorClassDlg.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.GetErrorStri

8、ng();五、实验结果记录与分析截图:实验二 字符和字符串处理一、实验目的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.

9、h#include iostreamusing 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

10、)int iCLen; iCLen = 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-size

11、of(TCHAR); pTem = (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-si

12、zeof(TCHAR);if (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

13、-m_pUChar);m_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

14、 UChar.h#include 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 = u1LoadIco

15、n(IDR_MAINFRAME);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_BU

16、TTON_CREAT_NEW, 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,pgui

17、d-Data42,pguid-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等方法。四、实验方法、步骤代码:/*

18、EnvironmentVariableDlg.cpp */void CEnvironmentVariableDlg:OnButtonSetenvironment() / TODO: Add your control notification handler code hereCString strEnvironmentName;CString strEnvironmentValue;m_eSetEnvironmentName.GetWindowText(strEnvironmentName);m_eSetEnvironmentValue.GetWindowText(strEnvironment

19、Value);if (!strEnvironmentName.IsEmpty()&!strEnvironmentValue.IsEmpty() BOOL ret = SetEnvironmentVariable(strEnvironmentName.GetBuffer(0),strEnvironmentValue.GetBuffer(0);if (ret)MessageBox(插入环境变量成功!);elseMessageBox(插入环境变量失败!);strEnvironmentValue.ReleaseBuffer();strEnvironmentName.ReleaseBuffer(); e

20、lseMessageBox(环境变量名或环境变量值不能为空!);void CEnvironmentVariableDlg:OnButtonGetenvironment() / TODO: Add your control notification handler code hereGetEnvironmentValue(你查找的环境变量并不存在);void CEnvironmentVariableDlg:GetEnvironmentValue(CString str) int rCnt;CString strEnvironmentName; char strEnvironmentValueEN

21、VIRONMENT_MAX;m_eGetEnvironmentName.GetWindowText(strEnvironmentName); m_eGetEnvironmentValue.SetWindowText();if (!strEnvironmentName.IsEmpty() rCnt = GetEnvironmentVariable(strEnvironmentName.GetBuffer(0),strEnvironmentValue,ENVIRONMENT_MAX);if (rCnt!=0) strEnvironmentValuerCnt = 0;m_eGetEnvironmen

22、tValue.SetWindowText(strEnvironmentValue); elseMessageBox(str);strEnvironmentName.ReleaseBuffer(); elseMessageBox(环境变量名不能为空!);void CEnvironmentVariableDlg:OnButtonCurrentDirectory() / TODO: Add your control notification handler code hereDWORD nBufferLength; / size of directory buffer TCHAR lpBufferM

23、AX_PATH; nBufferLength = 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_PAT

24、H; TCHAR *lpFilePart;/ 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 c

25、ode hereCString 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 not

26、ification handler 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 contro

27、l notification 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 contr

28、ol notification 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

29、your control notification handler code herePTSTR clStr;clStr = :GetCommandLine();MessageBox(clStr);/*OpenOrCloseProcessDlg.cpp */void COpenOrCloseProcessDlg:OnButtonUpdate() UpdateProcessInformation();return ;void COpenOrCloseProcessDlg:InitalListCtr()m_ctrListCtrProcessInformation.InsertColumn(0,序号

30、);m_ctrListCtrProcessInformation.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_ct

31、rListCtrProcessInformation.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(请选择要删除的进

32、程);return;iSelect = 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

33、();UpdateProcessInformation();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,str

34、FileFilter);if (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 ;Cl

35、oseHandle(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.GetFirstSelectedItemPosition

36、();m_uSelectPos = m_ctrListCtrProcessInformation.GetNextSelectedItem(pos); str = m_ctrListCtrProcessInformation.GetItemText(m_uSelectPos,2);GetDlgItem(IDC_EDIT_DELETE_PROCESS_ID)-SetWindowText(str);*pResult = 0;void COpenOrCloseProcessDlg:UpdateProcessInformation() / TODO: Add your control notificat

37、ion handler code here PROCESSENTRY32 pe32;/ 在使用这个结构之前,先设置它的大小pe32.dwSize = sizeof(pe32); / 给系统内的所有进程拍一个快照HANDLE hProcessSnap = :CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if(hProcessSnap = INVALID_HANDLE_VALUE)MessageBox( CreateToolhelp32Snapshot调用失败!);return ;/ 遍历进程快照,轮流显示每个进程的信息 CString str;i

38、nt i = 0;m_ctrListCtrProcessInformation.DeleteAllItems();BOOL bMore = :Process32First(hProcessSnap, &pe32);while(bMore)m_ctrListCtrProcessInformation.InsertItem(i,);str.Format(%d,i);m_ctrListCtrProcessInformation.SetItemText(i,0,str);str.Format(%s , pe32.szExeFile);m_ctrListCtrProcessInformation.Set

39、ItemText(i,1,str);str.Format( %u , pe32.th32ProcessID);m_ctrListCtrProcessInformation.SetItemText(i,2,str); bMore = :Process32Next(hProcessSnap, &pe32);i=i+1;/ 不要忘记清除掉snapshot对象:CloseHandle(hProcessSnap);/*MyProcess.h */#if !defined(AFX_MYPROCESS_H_A3A44BEF_852A_4B53_863A_924353A5C86C_INCLUDED_)#def

40、ine AFX_MYPROCESS_H_A3A44BEF_852A_4B53_863A_924353A5C86C_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000class CMyProcess public:CMyProcess();BOOL CreateProcess(CString *CommandLine);int ExitProcess(int iExitCode);CString GetCurrentDirectory();virtual CMyProcess();private:STARTUPINFO m_S

41、tartupInfo; / startup information PROCESS_INFORMATION m_ProcessInformation; / process information CString m_strCurrentDirectory;#endif / !defined(AFX_MYPROCESS_H_A3A44BEF_852A_4B53_863A_924353A5C86C_INCLUDED_)/*MyProcess.cpp */#include stdafx.h#include Process.h#include MyProcess.h#ifdef _DEBUG#unde

42、f THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endifCMyProcess:CMyProcess() m_StartupInfo.cb = sizeof(STARTUPINFO);BOOL CMyProcess:CreateProcess(CString *CommandLine) STARTUPINFO si = sizeof(si) ;BOOL bRet = :CreateProcess(NULL, CommandLine-GetBuffer(0), NULL, NULL, FALSE, CREATE_NEW_

43、CONSOLE, NULL, NULL, &si, &m_ProcessInformation);if (!bRet)return FALSE;return true;int CMyProcess:ExitProcess(int iExitCode = 0):ExitProcess(iExitCode);return iExitCode;CString CMyProcess:GetCurrentDirectory()DWORD nBufferLength; / size of directory buffer TCHAR lpBufferMAX_PATH; nBufferLength = :G

44、etCurrentDirectory(MAX_PATH, lpBuffer);lpBuffernBufferLength = 0;m_strCurrentDirectory.Format(%s,lpBuffer); return m_strCurrentDirectory;CMyProcess:CMyProcess():CloseHandle(m_ProcessInformation.hProcess);:CloseHandle(m_ProcessInformation.hThread);五、实验结果记录与分析实验五 线程创建一、实验目的1、理解windows系统线程工作原理机制。2、掌握wi

45、ndows线程的创建、关闭,使用AfxBeginThread、CreateThread、TerminateThread、ExitThread方法创建、关闭线程。及CWinThread类的使用。二、实验环境Windows Xp Visual studio 6.0三、实验内容编写程序,创建和关闭线程,实现简单的线程应用。四、实验方法、步骤/* UIThread1.h */class CUIThread : public CWinThreadDECLARE_DYNCREATE(CUIThread)public:CUIThread(); / protected constructor used by

46、dynamic creation/ Attributespublic:/ Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CUIThread)public:virtual BOOL InitInstance();virtual int ExitInstance();/AFX_VIRTUAL/ Implementationpublic:virtual CUIThread();/ Generated message map functions/AFX_MSG(CUI

47、Thread)/ NOTE - the ClassWizard will add and remove member functions here. afx_msg void AFX_MSG_CALL OnmyMessageFuc(WPARAM wParam, LPARAM lParam);/AFX_MSGDECLARE_MESSAGE_MAP();/* UIThread1.cpp */ UIThread1.cpp : implementation file/#include stdafx.h#include UIThread.h#include UIThread1.h#include myD

48、efine.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CUIThreadIMPLEMENT_DYNCREATE(CUIThread, CWinThread)CUIThread:CUIThread()CUIThread:CUIThread()BOOL CUIThread:InitInstance()/ TODO: perform and per-thread initialization herereturn TRUE;int CUIThread:ExitInstance()/ TODO: perform any per-thread cleanup herereturn CWinThread:ExitInstance();BEGIN_MESSAGE_MAP(CUIThread, CWinThread

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!