windows程序设计实验报告.docx

上传人:b****8 文档编号:10402861 上传时间:2023-02-11 格式:DOCX 页数:16 大小:18.71KB
下载 相关 举报
windows程序设计实验报告.docx_第1页
第1页 / 共16页
windows程序设计实验报告.docx_第2页
第2页 / 共16页
windows程序设计实验报告.docx_第3页
第3页 / 共16页
windows程序设计实验报告.docx_第4页
第4页 / 共16页
windows程序设计实验报告.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

windows程序设计实验报告.docx

《windows程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《windows程序设计实验报告.docx(16页珍藏版)》请在冰豆网上搜索。

windows程序设计实验报告.docx

windows程序设计实验报告

 

姓名:

学号:

030940125

学科:

计算机科学与技术

指导老师:

试验分数:

Windows程序设计

实验报告

实验一错误处理

一.实验目的:

通过对windows代码的纠错处理,达到能检测所写代码的错误,以及系统运行的要求。

熟悉纠错功能的运行过程。

学会运用纠错功能。

二,实验过程:

1,实验环境设置:

在visualC++6.0中新建mfc工程,命名为dlgerror,完成进入编辑状态。

2,实验实现过程:

a,在对话框中建立如下控件:

b,响应“查询”按钮函数:

voidCDlgErrorDlg:

:

OnButtonLookup()。

C,新建类classGetErrorInformation

声明代码:

{

private:

HLOCALm_hlocal;

intm_iErrorCode;

TCHAR*m_pcErrorInformation;

public:

GetErrorInformation();

~GetErrorInformation();

voidSetErrorCode(intErrorCode);

TCHAR*GetErrorString();

protected:

private:

};

d,新建成员函数:

TCHAR*GetErrorInformation:

:

GetErrorString()

三,实验核心代码:

1,类classGetErrorInformation成员函数代码:

TCHAR*GetErrorInformation:

:

GetErrorString()

{

//Gettheerrorcode'stextualdescription

BOOLfOk=FormatMessage(

FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_MAX_WIDTH_MASK,NULL,m_iErrorCode,MAKELANGID(LANG_CHINESE,UBLANG_ENGLISH_US),

(PTSTR)&m_hlocal,0,NULL);

if(!

fOk){

//Isitanetwork-relatederror?

HMODULEhDll=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_CHINESE,SUBLANG_ENGLISH_US),

(PTSTR)&m_hlocal,0,NULL);

FreeLibrary(hDll);

}

}

if(m_hlocal!

=NULL)

{

m_pcErrorInformation=(char*)m_hlocal;

}

else

{

m_pcErrorInformation=(TCHAR*)malloc(60);

TCHAR*charError=_TEXT("你所查找的错误代码不存在");

strcpy(m_pcErrorInformation,charError);

}

returnm_pcErrorInformation;

}

2,查询函数:

voidCDlgErrorDlg:

:

OnButtonLookup()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

interrorCode;

GetErrorInformationgei;

errorCode=GetDlgItemInt(IDC_EDIT_ERROR_CODE);

gei.SetErrorCode(errorCode);

SetDlgItemText(IDC_EDIT_DISPLAY,gei.GetErrorString());

}

四,实验结果:

实验二unicode实验

一,实验目的

通过对unicode的操作理解数据编码;

熟悉unicode的各种运算;

了解unicode和ASCII的区别。

二,实验过程

1,新建win32控制台程序,命名Uchar。

2,建立空工程。

3,添加类Uchar的声明:

classUChar

{

friendostream&operator<<(ostream&os,UChar&uc)

{

os<

os<

returnos;

}

private:

LPTSTRm_pUChar;

UINTm_Length;

public:

UChar();

UChar(constTCHAR*pChar);

UChar&operator+(UCharuchar2);

UChar&operator=(constTCHAR*pStr2);

UChar&operator=(UCharuchar2);

BOOLoperator>(UCharuchar2);

UINTLength();

virtual~UChar();

protected:

private:

};

4,实现unicode入口函数

5,实现各个函数。

三,实验核心代码

1,Uchar类成员函数的实现:

UChar:

:

UChar()

{

m_pUChar=NULL;

m_Length=0;

}

UChar:

:

UChar(constTCHAR*pChar)

{

intiCLen;

iCLen=lstrlen(pChar)+sizeof(TCHAR);

m_Length=iCLen-sizeof(TCHAR);

m_pUChar=(TCHAR*)malloc(iCLen*sizeof(TCHAR));

lstrcpy(m_pUChar,pChar);

}

UChar&UChar:

:

operator+(UCharuchar2)

{

intiCLen;

LPTSTRpTem;

iCLen=lstrlen(this->m_pUChar)+lstrlen(uchar2.m_pUChar)+sizeof(TCHAR);

m_Length=iCLen-sizeof(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=(constTCHAR*pStr2)

{

intiCLen;

iCLen=lstrlen(pStr2)+sizeof(TCHAR);

m_Length=iCLen-sizeof(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=(UCharuchar2)

{

intiCLen;

iCLen=lstrlen(uchar2.m_pUChar)+sizeof(TCHAR);

m_Length=iCLen-sizeof(TCHAR);

if(this->m_pUChar!

=NULL)

{

free(this->m_pUChar);

}

m_pUChar=(TCHAR*)malloc(iCLen);

lstrcpy(m_pUChar,uchar2.m_pUChar);

return*this;

}

BOOLUChar:

:

operator>(UCharuchar2)

{

returnlstrcmp(this->m_pUChar,uchar2.m_pUChar);

}

UINTUChar:

:

Length()

{

returnthis->m_Length;

}

UChar:

:

~UChar()

{

if(m_pUChar!

=NULL)

{

free(m_pUChar);

}

}

2,入口函数的实现:

intmain(intargc,char*argv[])

{

UCharu1(_TEXT("爸爸妈妈你们好吗!

"));

UCharu2(_TEXT("爷爷奶奶你们好吗!

"));

BOOLb;

cout<

cout<

u1=_TEXT("哥哥姐姐你们好吗!

");

u2=_TEXT("叔叔伯伯你们好吗!

");

cout<

cout<

b=u1>u2;

cout<<"b="<

//u1+u2;

cout<<"u1="<

u1=u2;

cout<<"u1="<

return0;

}

四,实验结果:

实验三内核对象

一,实验目的

了解系统内核对象;

了解内核对象的作用。

二,实验过程

1,启动VC6.0,新建基于对话框的MFC工程,命名为GUID;

2,设计对话框界面,如图:

 

3,双击“产生”,响应函数。

4,编写函数实现过程代码。

三,实验核心代码

1,guid结构:

#ifndefGUID_DEFINED

#defineGUID_DEFINED

typedefstruct_GUID{//sizeis16

DWORDData1;

WORDData2;

WORDData3;

BYTEData4[8];

}GUID;

#endif//!

GUID_DEFINED

2,“产生”函数实现过程代码:

voidCGUIDDlg:

:

OnButtonCreatNew()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CStringstrGUID;

HRESULThResult;

GUID*pguid=newGUID();

hResult=CoCreateGuid(pguid);

strGUID.Format("%-x-%-x-%-x-%-x%-x%-x%-x%-x%-x%-x%-x",pguid->Data1,pguid->Data2,pguid->Data3,pguid->Data4[0],pguid->Data4[1],pguid->Data4[2],pguid->Data4[3],pguid->Data4[4],pguid->Data4[5],pguid->Data4[6],pguid->Data4[7]);

m_eGuid.SetWindowText(strGUID);

}

四,实验结果

单击产生结果如下:

 

实验四系统进程控制

一,实验目的

理解进程在系统中的作用;

熟悉进程的工作方式;

通过启动进程,熟悉进程的原理。

二,实验过程

1,启动VC6.0,创建基于对话框的MFC工程,命名process。

2,设计对话框,如下图所示:

3,实现各个按钮

三,实验核心代码

1,启动进程

voidCProcessDlg:

:

OnButtonStartProcess()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

BOOLret;

CStringpathName;

CStringstrFileFilter="可执行文件|*.exe";

CFileDialogdFileDlg(TRUE,"exe",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST,strFileFilter);

if(dFileDlg.DoModal()==IDOK)

{

pathName=dFileDlg.GetPathName();

ret=m_MyProcess.CreateProcess(&pathName);

if(!

ret)

{

MessageBox("进程创建失败");

}

}

}

2,退出进程

voidCProcessDlg:

:

OnButtonExitprocess()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

m_MyProcess.ExitProcess(0);

}

3,进程目录

voidCProcessDlg:

:

OnButtonProcessDirectory()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CStringstr;

str=m_MyProcess.GetCurrentDirectory();

m_directory.SetWindowText(str);

}

四.实验结果

1,运行后点击启动过程,启动需要启动的进程:

1,在这里选择进程editplus.exe,单击打开即可打开editplus软件。

 

2,结果如下图:

 

2,点击退出进程即可退出。

3,点击进程目录就可知道当前打开的进程,效果如图:

实验五线程实现

一,实验目的

重点掌握线程函数的创建;

理解多线程;

通过学习,能自己运用多线程到实际的项目中。

二,实验过程

1,启动VC6.0创建基于对话框的MFC工程,命名MFThread.。

2,进入编辑状态,设计对话框如图所示:

3,编写头文件:

PARAMSTRUCT.H

#ifndef_THREADSTRUCT_H_

#define_THREADSTRUCT_H_

typedefstruct_THREADSTRUCT

{

intaddParam1;

intaddParam2;

intaddResult;

}THREADSTRUCT,*PTHREADSTRUCT;

#endif

4,实现函数功能。

三,实验核心代码

响应启动函数:

voidCMFCThreadDlg:

:

OnButtonStartThread()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

PTHREADSTRUCTpThreadParam=newTHREADSTRUCT();

CStringstr;

GetDlgItem(IDC_EDIT_A)->GetWindowText(str);

pThreadParam->addParam1=atoi(str);

GetDlgItem(IDC_EDIT_B)->GetWindowText(str);

pThreadParam->addParam2=atoi(str);

m_pThread=AfxBeginThread((AFX_THREADPROC)CMFCThreadDlg:

:

AddThread,(LPVOID)pThreadParam);

ASSERT(m_pThread);

intret;

ret=WaitForSingleObject(m_pThread->m_hThread,10);

switch(ret)

{

caseWAIT_FAILED:

{

DWORDerrCode;

errCode=GetLastError();

CStringerrStr;

errStr.Format("线程调用出错,出错代码为%d",errCode);

MessageBox(errStr);

break;

}

caseWAIT_TIMEOUT:

{

MessageBox("线程等待超时");

break;

}

default:

{

str.Format("%d",pThreadParam->addResult);

GetDlgItem(IDC_EDIT_ADD_RESULT)->SetWindowText(str);

MessageBox("成功调用");

break;

}

}

deletepThreadParam;

:

:

CloseHandle(m_pThread->m_hThread);

m_pThread=NULL;

return;

}

四,实验结果

实验完成加法运算:

4+5=9

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1