Windows程序设计实验报告_第1页
Windows程序设计实验报告_第2页
Windows程序设计实验报告_第3页
Windows程序设计实验报告_第4页
Windows程序设计实验报告_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 = :getcurrentdirectory(max_path, lpbuffer);lpbuffernbufferlength = 0;m_strcurrentdirectory.format(%s,lpbuffer); return m_strcurrentdirectory;cmyprocess:cmyprocess():closehandl

温馨提示

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

评论

0/150

提交评论