VC6.0中MFC向导选中Windows Sockets后

来源:互联网 发布:pcb绘图软件中文版 编辑:程序博客网 时间:2024/05/17 21:43

VC6.0中MFC向导选中Windows Sockets后,mfc向导会自动在建立的应用程序框架中加入winsocket初始化代码。如果我们需要在传统的串口通信程序加入网络通信的功能,就必须手工加入网络初始化代码。步骤如下。

1.加入winsocket初始化头文件

在stdafx.h中加入afxsock.h

 

// stdafx.h : include file for standard system include files,//  or project specific include files that are used frequently, but//      are changed infrequently//#if !defined(AFX_STDAFX_H__71EE4E1B_9F80_4D8E_99A1_4B615AF25C9E__INCLUDED_)#define AFX_STDAFX_H__71EE4E1B_9F80_4D8E_99A1_4B615AF25C9E__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#define VC_EXTRALEAN// Exclude rarely-used stuff from Windows headers#include <afxwin.h>         // MFC core and standard components#include <afxext.h>         // MFC extensions#include <afxdisp.h>        // MFC Automation classes#include <afxdtctl.h>// MFC support for Internet Explorer 4 Common Controls#ifndef _AFX_NO_AFXCMN_SUPPORT#include <afxcmn.h>// MFC support for Windows Common Controls#endif // _AFX_NO_AFXCMN_SUPPORT#include <afxsock.h>// MFC socket extensions 要添加的代码//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__71EE4E1B_9F80_4D8E_99A1_4B615AF25C9E__INCLUDED_)


2.在app实现文件中加入winsocket初始化代码

app实现文件名和创建的工程名相同,后缀名为cpp,在此文件的initinstance函数中加入以下代码:

//需要添加的代码if (!AfxSocketInit()){AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;}//添加代码结束


3.添加CSocket对象或相应的类

菜单insert->new class,添加基类为CSocket的CMySocket类。添加完成后,类向导会生成MySocket.h和MySocket.cpp。

原创粉丝点击