实例介绍
【实例简介】
显示进程所使用的端口和远程IP
【实例截图】
【核心代码】
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <algorithm>
#include "resource.h"
#include "iptable.h"
#include "getip.h"
#pragma comment(lib, "comctl32.lib ")
//
//common
//
inline void FsListView_InsertColum( HWND hwnd, PCH str, int width ) {
LV_COLUMNA colmn = {0};
colmn.mask = LVCF_WIDTH | LVCF_TEXT;
colmn.pszText = str;
colmn.cx = width;
SendMessageA( (hwnd), LVM_INSERTCOLUMNA, (WPARAM)(int)(0), (LPARAM)(const LV_COLUMN *)(&colmn) );
}
inline void FsListView_SetItemText( HWND hwndLV, int i, int iSubItem_, PCH pszText_ ) {
LV_ITEMA _macro_lvi;
_macro_lvi.iSubItem = (iSubItem_);
_macro_lvi.pszText = (pszText_);
SendMessageA( hwndLV, LVM_SETITEMTEXTA, (WPARAM)(i), (LPARAM)(LV_ITEM *)&_macro_lvi );
}
inline void selectFile(PCH filename) {
//open folder and select file
char szParam[_MAX_PATH] = {0};
strcpy( szParam, "/e,/select, " );
strcat( szParam, filename );
ShellExecuteA( NULL, "open", "explorer", szParam, NULL, SW_SHOW );
}
inline bool ListView_GetSelectStr( HWND hList, int isubItem, OUT PCH retbuf, IN int retbuflen ) {
int sfind = ListView_GetSelectionMark( hList );
if( sfind != -1 ) {
LV_ITEMA _macro_lvi;
_macro_lvi.iSubItem = isubItem;
_macro_lvi.cchTextMax = retbuflen;
_macro_lvi.pszText = retbuf;
SendMessageA( hList, LVM_GETITEMTEXTA, (WPARAM)sfind, (LPARAM)(LV_ITEM *)&_macro_lvi );
return true;
}
return false;
}
//
//others
//
HINSTANCE hinst;
HWND MainWnd;
#define IP_DATA_NAME "qqwry.dat" //纯真数据库
#define hMainList GetDlgItem( MainWnd, IDC_LIST1 )
int lastcolumnclick = -1; //记录上次点击的列头
bool blastcolumnclick = false; //用于判断是否应该翻转排序
void FillOnce( TcpTable::TcpTableStruct& v ) {
LV_ITEMA item = {0};
int cis = 0;
char tmp[10];
item.mask = LVIF_TEXT | LVIF_IMAGE;
wsprintfA( tmp, "%04d", v.pid );
item.pszText = tmp;
item.iItem = cis;
cis = SendMessageA( hMainList, LVM_INSERTITEMA, 0, (LPARAM)(const LV_ITEM *)(&item) );
//1 name
if( v.name[0] == 0 )
strcpy( v.name, "[ no access ]" );
FsListView_SetItemText( hMainList, cis, 1, v.name );
//2 address
//直接用纯真库查询
CIpFinder ipfinder;
if( ipfinder.Open( IP_DATA_NAME ) ) {
std::string country, location;
ipfinder.GetAddressByIp( v.remoteip, country, location );
country = " ";
country = location;
FsListView_SetItemText( hMainList, cis, 2, (PCH)country.c_str() );
}
//3 ip
FsListView_SetItemText( hMainList, cis, 3, v.remoteip );
//4 port
wsprintfA( tmp, "%d", v.remoteport );
FsListView_SetItemText( hMainList, cis, 4, tmp );
//5 path
FsListView_SetItemText( hMainList, cis, 5, v.dir );
}
void FillInfo( int icloumsort = -1 ) {
std::vector<TcpTable::TcpTableStruct> curshow;
auto findpid = [&]( DWORD pid )->bool {
for( auto v : curshow ) {
if( v.pid == pid )
return true;
}
return false;
};
auto isok = []( TcpTable::TcpTableStruct v )->bool {
return v.remoteport && (strstr( v.remoteip, "127.0.0.1" ) == 0);
};
TcpTable tt;
//TcpTable排序
if( icloumsort != -1 ) {
if( lastcolumnclick == icloumsort )
blastcolumnclick = !blastcolumnclick;
lastcolumnclick = icloumsort;
} else {
//last time choice
icloumsort = lastcolumnclick;
}
switch( icloumsort ) {
case 0:
sort( tt.getTableVector().begin(), tt.getTableVector().end(),
[&]( TcpTable::TcpTableStruct x, TcpTable::TcpTableStruct y )->bool {
if( blastcolumnclick )
return x.pid < y.pid; //第二次点击 降序
return x.pid > y.pid; //第一次点击 升序
} );
break;
case 1:
sort( tt.getTableVector().begin(), tt.getTableVector().end(),
[&]( TcpTable::TcpTableStruct x, TcpTable::TcpTableStruct y )->bool {
if( blastcolumnclick )
return strcmp( x.name, y.name ) < 0;
return strcmp( x.name, y.name ) > 0;
} );
break;
case 2: //do nothing
break;
case 3:
sort( tt.getTableVector().begin(), tt.getTableVector().end(),
[&]( TcpTable::TcpTableStruct x, TcpTable::TcpTableStruct y )->bool {
if( blastcolumnclick )
return strcmp( x.remoteip, y.remoteip ) < 0;
return strcmp( x.remoteip, y.remoteip ) > 0;
} );
break;
case 4:
sort( tt.getTableVector().begin(), tt.getTableVector().end(),
[&]( TcpTable::TcpTableStruct x, TcpTable::TcpTableStruct y )->bool {
if( blastcolumnclick )
return x.remoteport < y.remoteport; //第二次点击 降序
return x.remoteport > y.remoteport; //第一次点击 升序
} );
break;
case 5:
sort( tt.getTableVector().begin(), tt.getTableVector().end(),
[&]( TcpTable::TcpTableStruct x, TcpTable::TcpTableStruct y )->bool {
if( blastcolumnclick )
return strcmp( x.dir, y.dir ) < 0;
return strcmp( x.dir, y.dir ) > 0;
} );
break;
}
ListView_DeleteAllItems( hMainList );
curshow.clear();
//b_unique_pid
if( IsDlgButtonChecked( MainWnd, IDC_CHECK1 ) ) {
for( auto v : tt.getTableVector() ) {
if( !findpid( v.pid ) ) {
if( isok( v ) ) {
curshow.push_back( v );
FillOnce( v );
}
}
}
} else {
for( auto v : tt.getTableVector() ) {
if( isok( v ) )
FillOnce( v );
}
}
}
bool selectFileByMainListChoice( ) {
char path[260], name[100];
ListView_GetSelectStr( hMainList, 5, path, 260 );
if( path[0] ) {
ListView_GetSelectStr( hMainList, 1, name, 100 );
strcat( path, name );
selectFile( path );
return true;
}
return false;
}
void terminateSelectProcess() {
char spid[10];
ListView_GetSelectStr( hMainList, 0, spid, 10 );
char cmd[100];
wsprintfA( cmd, "taskkill /f /pid %s", spid );
system( cmd );
}
//
//运行框Login
//
BOOL CALLBACK MainWindowProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
switch( uMsg ) {
case WM_INITDIALOG:
MainWnd = hDlg;
ListView_SetExtendedListViewStyleEx( hMainList, 0, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES ); //style ex
ListView_SetImageList( hMainList, ImageList_Create( 1, 16, ILC_COLOR24 | ILC_MASK, 1, 1 ), LVSIL_SMALL ); //改格子高度
FsListView_InsertColum( hMainList, "所在路径", 360 );
FsListView_InsertColum( hMainList, "端口", 60 );
FsListView_InsertColum( hMainList, "远程IP", 120 );
FsListView_InsertColum( hMainList, "远程地址", 180 );
FsListView_InsertColum( hMainList, "名称", 120 );
FsListView_InsertColum( hMainList, "PID", 60 );
FillInfo();
break;
case WM_COMMAND:
switch( LOWORD( wParam ) ) {
case IDC_BUTTON1:
FillInfo();
break;
//menu
case ID_40001:
FillInfo();
break;
case ID_40002:
selectFileByMainListChoice();
break;
case ID_40004:
terminateSelectProcess();
FillInfo();
break;
}
return 1;
case WM_NOTIFY:
//点击列头部排序消息
if( (((LPNMHDR)lParam)->idFrom == IDC_LIST1) && (((LPNMHDR)lParam)->code == LVN_COLUMNCLICK) ) {
FillInfo( ((LPNMLISTVIEW)lParam)->iSubItem );
return 1;
}
//右击菜单
if( ((LPNMHDR)lParam)->code == NM_RCLICK && LOWORD( wParam ) == IDC_LIST1 ) {
POINT pt;
GetCursorPos( &pt );
HMENU hmu = GetSubMenu( LoadMenuA( hinst, MAKEINTRESOURCEA( IDR_MENU1 ) ), 0 );
TrackPopupMenu( hmu, TPM_LEFTALIGN, pt.x, pt.y, 0, MainWnd, 0 );
return 1;
}
return 1;
case WM_CLOSE:
EndDialog( hDlg, 0 );
return 1;
}
return 0;
}
VOID ShowMainWnd() {
DialogBoxA( hinst, MAKEINTRESOURCEA( IDD_DIALOG1 ), (HWND)0, MainWindowProc );
}
//
//Main
//
void main() {
hinst = GetModuleHandleA( 0 );
ShowMainWnd();
}
int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine, _In_ int nShowCmd ) {
hinst = hInstance;
ShowMainWnd();
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论