这是学习网络编程时的代码,今天整理一下贴出来了:
///////////////////////////////////////////////////
//
// AppTest.cpp文件
/////////////////////////////////////////////////// //
#include < afxwin.h >
#include " SkinMagicLib.h "
#include " resource.h "
#include < commctrl.h > // 通用控件
#include < Ws2spi.h > // SPI函数定义
#pragma comment(lib,"SkinMagicLibMT6Trial") // 链接到Skin库
#pragma comment(lib, "WS2_32") // 链接到WS2_32.lib
#pragma comment(lib,"comctl32") // 通用控件库
/////////////////////////////////////////////////// //
// 外部函数
extern LPWSAPROTOCOL_INFOW GetProvider(LPINT lpnTotalProtocols);
extern void FreeProvider(LPWSAPROTOCOL_INFOW pProtoInfo);
/////////////////////////////////////////////////// //
// 全局变量
LPWSAPROTOCOL_INFOW pProtoInfo;
HINSTANCE hInstance;
HWND hListProtocol;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void ShowProtocolList(HWND hListProtocol)
{
char szBuffer[MAX_PATH];
int nProtocols;
LVITEM item = { 0 };
LVITEM lvi = { 0 };
pProtoInfo = GetProvider( & nProtocols);
for ( int i = 0 ; i < nProtocols; ++ i)
{
item.iItem = i;
item.mask = LVIF_TEXT; // 指定pszText域有
wsprintf(szBuffer, " %d " ,i + 1 );
item.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_INSERTITEM, 0 , ( long ) & item); // 设置Index域
lvi.iSubItem = 1 ;
wsprintf(szBuffer, " %ws " ,pProtoInfo[i].szProtocol);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置Protocol域
lvi.iSubItem = 2 ;
wsprintf(szBuffer, " %d " ,pProtoInfo[i].dwCatalogEntryId);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置CatalogEntryId域
lvi.iSubItem = 3 ;
wsprintf(szBuffer, " %d " ,pProtoInfo[i].ProtocolChain.ChainLen);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置ChainLen域
}
}
int CALLBACK WinMain(HINSTANCE hInst,HINSTANCE,LPSTR, int )
{
InitSkinMagicLib(hInst, " AppTest " ,NULL,NULL);
LoadSkinFile( " AlphaOS.smf " );
INITCOMMONCONTROLSEX icce;
icce.dwSize = sizeof (icce);
// ListView
icce.dwICC = ICC_LISTVIEW_CLASSES ;
InitCommonControlsEx( & icce); // 初使化扩展通用控件库
hInstance = hInst;
::DialogBoxParam(hInst,(LPCTSTR)IDD_MAIN,NULL,(DLGPROC)WndProc,NULL);
ExitSkinMagicLib();
return 1 ;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
POINT p;
HICON hIcon;
switch (uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_HOMELINK:
::GetCursorPos( & p);
::TrackSkinPopupMenu(::LoadMenu(hInstance,
MAKEINTRESOURCE(IDR_MENU2)),
TPM_TOPALIGN,p.x,p.y,hWnd);
break ;
case IDM_PEDIY:
::ShellExecute(hWnd, " Open " , " http://bbs.pediy.com " ,NULL,NULL,SW_SHOW);
break ;
case IDM_UNPACKCN:
::ShellExecute(hWnd, " Open " , " http://www.unpack.cn " ,NULL,NULL,SW_SHOW);
break ;
case IDM_BAJIAO:
::ShellExecute(hWnd, " Open " , " http://www.bajiao123.com " ,NULL,NULL,SW_SHOW);
break ;
}
}
break ;
case WM_INITDIALOG:
::SetWindowText (hWnd, " Enum SPI -- By thinkSJ " ); // 设置标题栏
if (LoadSkinFromResource(hInstance,(LPCTSTR)IDR_SKIN1, " Skin " ))
{
SetWindowSkin(hWnd, " MainFrame " ); // 换肤
}
hIcon = ::LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
::SendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)hIcon); // 设置图标
hListProtocol = ::GetDlgItem(hWnd,IDC_LISTPROTOCOL);
::SendMessage(hListProtocol, LVM_SETEXTENDEDLISTVIEWSTYLE,
0 , LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
LVCOLUMN column;
// 指定LVCOLUMN结构中的pszText、fmt、cx域有效
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
// 设置有效的域的属性
column.fmt = LVCFMT_LEFT;
column.pszText = " Index " ;
column.cx = 50 ;
::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 1 , (LPARAM) & column);
column.pszText = " Protocol " ;
column.cx = 150 ;
::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 2 , (LPARAM) & column);
column.pszText = " CatalogEntryId " ;
column.cx = 100 ;
::SendMessage (hListProtocol,LVM_INSERTCOLUMN, 3 ,(LPARAM) & column);
column.pszText = " ChainLen " ;
column.cx = 70 ;
::SendMessage (hListProtocol,LVM_INSERTCOLUMN, 4 ,(LPARAM) & column);
ShowProtocolList(hListProtocol); // 显示
return true ;
case WM_CLOSE:
::FreeProvider(pProtoInfo);
::EndDialog(hWnd, 0 );
break ;
}
return false ;
}
// AppTest.cpp文件
/////////////////////////////////////////////////// //
#include < afxwin.h >
#include " SkinMagicLib.h "
#include " resource.h "
#include < commctrl.h > // 通用控件
#include < Ws2spi.h > // SPI函数定义
#pragma comment(lib,"SkinMagicLibMT6Trial") // 链接到Skin库
#pragma comment(lib, "WS2_32") // 链接到WS2_32.lib
#pragma comment(lib,"comctl32") // 通用控件库
/////////////////////////////////////////////////// //
// 外部函数
extern LPWSAPROTOCOL_INFOW GetProvider(LPINT lpnTotalProtocols);
extern void FreeProvider(LPWSAPROTOCOL_INFOW pProtoInfo);
/////////////////////////////////////////////////// //
// 全局变量
LPWSAPROTOCOL_INFOW pProtoInfo;
HINSTANCE hInstance;
HWND hListProtocol;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void ShowProtocolList(HWND hListProtocol)
{
char szBuffer[MAX_PATH];
int nProtocols;
LVITEM item = { 0 };
LVITEM lvi = { 0 };
pProtoInfo = GetProvider( & nProtocols);
for ( int i = 0 ; i < nProtocols; ++ i)
{
item.iItem = i;
item.mask = LVIF_TEXT; // 指定pszText域有
wsprintf(szBuffer, " %d " ,i + 1 );
item.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_INSERTITEM, 0 , ( long ) & item); // 设置Index域
lvi.iSubItem = 1 ;
wsprintf(szBuffer, " %ws " ,pProtoInfo[i].szProtocol);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置Protocol域
lvi.iSubItem = 2 ;
wsprintf(szBuffer, " %d " ,pProtoInfo[i].dwCatalogEntryId);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置CatalogEntryId域
lvi.iSubItem = 3 ;
wsprintf(szBuffer, " %d " ,pProtoInfo[i].ProtocolChain.ChainLen);
lvi.pszText = szBuffer;
::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, ( long ) & lvi); // 设置ChainLen域
}
}
int CALLBACK WinMain(HINSTANCE hInst,HINSTANCE,LPSTR, int )
{
InitSkinMagicLib(hInst, " AppTest " ,NULL,NULL);
LoadSkinFile( " AlphaOS.smf " );
INITCOMMONCONTROLSEX icce;
icce.dwSize = sizeof (icce);
// ListView
icce.dwICC = ICC_LISTVIEW_CLASSES ;
InitCommonControlsEx( & icce); // 初使化扩展通用控件库
hInstance = hInst;
::DialogBoxParam(hInst,(LPCTSTR)IDD_MAIN,NULL,(DLGPROC)WndProc,NULL);
ExitSkinMagicLib();
return 1 ;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
POINT p;
HICON hIcon;
switch (uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_HOMELINK:
::GetCursorPos( & p);
::TrackSkinPopupMenu(::LoadMenu(hInstance,
MAKEINTRESOURCE(IDR_MENU2)),
TPM_TOPALIGN,p.x,p.y,hWnd);
break ;
case IDM_PEDIY:
::ShellExecute(hWnd, " Open " , " http://bbs.pediy.com " ,NULL,NULL,SW_SHOW);
break ;
case IDM_UNPACKCN:
::ShellExecute(hWnd, " Open " , " http://www.unpack.cn " ,NULL,NULL,SW_SHOW);
break ;
case IDM_BAJIAO:
::ShellExecute(hWnd, " Open " , " http://www.bajiao123.com " ,NULL,NULL,SW_SHOW);
break ;
}
}
break ;
case WM_INITDIALOG:
::SetWindowText (hWnd, " Enum SPI -- By thinkSJ " ); // 设置标题栏
if (LoadSkinFromResource(hInstance,(LPCTSTR)IDR_SKIN1, " Skin " ))
{
SetWindowSkin(hWnd, " MainFrame " ); // 换肤
}
hIcon = ::LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
::SendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)hIcon); // 设置图标
hListProtocol = ::GetDlgItem(hWnd,IDC_LISTPROTOCOL);
::SendMessage(hListProtocol, LVM_SETEXTENDEDLISTVIEWSTYLE,
0 , LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
LVCOLUMN column;
// 指定LVCOLUMN结构中的pszText、fmt、cx域有效
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
// 设置有效的域的属性
column.fmt = LVCFMT_LEFT;
column.pszText = " Index " ;
column.cx = 50 ;
::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 1 , (LPARAM) & column);
column.pszText = " Protocol " ;
column.cx = 150 ;
::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 2 , (LPARAM) & column);
column.pszText = " CatalogEntryId " ;
column.cx = 100 ;
::SendMessage (hListProtocol,LVM_INSERTCOLUMN, 3 ,(LPARAM) & column);
column.pszText = " ChainLen " ;
column.cx = 70 ;
::SendMessage (hListProtocol,LVM_INSERTCOLUMN, 4 ,(LPARAM) & column);
ShowProtocolList(hListProtocol); // 显示
return true ;
case WM_CLOSE:
::FreeProvider(pProtoInfo);
::EndDialog(hWnd, 0 );
break ;
}
return false ;
}
程序主要是通过WSCEnumProtocols函数来枚举SPI,不仅可以枚举基础服务和服务链,而且还可以枚举分层式
服务.
(注意:不要用浏览器下载,最好用工具下载)
下载工程