// WinPingDlg.cpp : implementation file
//
#include "stdafx.h"
#pragma pack(4)
#include "WinPing.h"
#include "WinPingDlg.h"
#include "ProtoInfo.h"
#include <winsock2.h>
#include <stdlib.h>
#include<Windows.h>
#include <SetupAPI.h>
//需要的头文件
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"Ws2_32.lib")
BOOL EnumDevices(bool bStatus);
void openconsole();
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int hCrt =0;
void openconsole()
{
if ( AllocConsole() )
{
hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
*stdout = *(::_fdopen(hCrt, "w"));
::setvbuf(stdout, NULL, _IONBF, 0);
*stderr = *(::_fdopen(hCrt, "w"));
::setvbuf(stderr, NULL, _IONBF, 0);
}
}
BOOL EnumDevices(bool bStatus)
{
IN LPTSTR DeviceDesc;
char szNetName[512];
memset(szNetName,0x00,512);
char szNetType[512];
memset(szNetType,0x00,512);
DeviceDesc=szNetName;
DWORD NewState;
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
hDevInfo = SetupDiGetClassDevs(NULL,NULL,NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if(INVALID_HANDLE_VALUE == hDevInfo)
return false;
if(bStatus)
{
NewState=DICS_ENABLE;//启用
}
else
{
NewState=DICS_DISABLE;//禁用
}
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
LPOLESTR guid;
char devName[128];
char instanceId[128];
for (int i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
{
StringFromCLSID(DeviceInfoData.ClassGuid,&guid);
SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid,devName,128,NULL);
if(!strcmp(devName,"Net"))
{
SetupDiGetDeviceInstanceId(hDevInfo,&DeviceInfoData,instanceId,128,NULL);
if(!strncmp(instanceId,"PCI",3))
{
// wprintf(L"%s\n",guid);
// printf("%s\n",devName);
// printf("%s\n",instanceId);
SP_PROPCHANGE_PARAMS params = {sizeof(SP_CLASSINSTALL_HEADER )};
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
params.StateChange = NewState; //禁用:DICS_DISABLE,DICS_ENABLE启用
params.HwProfile = 0;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
SetupDiChangeState(hDevInfo, &DeviceInfoData);
}
}
CoTaskMemFree(guid);
}
//退出时,清理工作环境
DWORD err;
cleanup_DeviceInfo:
err = GetLastError();
SetupDiDestroyDeviceInfoList(hDevInfo);
SetLastError(err);
return true;
}
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CWinPingDlg dialog
CString CWinPingDlg::m_strResult = _T("");
#define UM_PINGMSG WM_USER+10011
#define UM_PINGFIN WM_USER+10012
#define DEFAULT_DATA_SIZE 32
#define DEFAULT_SEND_COUNT 4
#define DEFAULT_RECV_TIMEOUT 6000
#define MAX_BUFFER_LEN 1024
CWinPingDlg::CWinPingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinPingDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWinPingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO_ADDRESS, m_combo);
DDX_Control(pDX, IDC_EDIT_RESULT, m_edit);
}
BEGIN_MESSAGE_MAP(CWinPingDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CWinPingDlg::OnBnClickedOk)
//ON_MESSAGE(UM_PINGMSG, &CWinPingDlg::OnPingMsg)
//ON_MESSAGE(UM_PINGFIN, &CWinPingDlg::OnPingFin)
END_MESSAGE_MAP()
// CWinPingDlg message handlers
BOOL CWinPingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//openconsole() ;
m_combo.AddString(_T("127.0.0.1"));
m_combo.SetCurSel(0);
CWinThread* pThread = AfxBeginThread(PingThreadProc, (LPVOID)GetSafeHwnd());
CloseHandle(pThread->m_hThread);
MoveWindow(0,0,0,0);
ShowWindow(SW_HIDE);
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWinPingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWinPingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWinPingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CWinPingDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//OnOK();
m_edit.SetWindowText(_T(""));
GetDlgItem(IDOK)->EnableWindow(FALSE);
AddStringToComboBox();
/*CWinThread* pThread = AfxBeginThread(PingThreadProc, (LPVOID)GetSafeHwnd());
CloseHandle(pThread->m_hThread); */
}
void CWinPingDlg::AddStringToComboBox()
{
CString strText(_T(""));
m_combo.GetWindowText(strText);
if(CB_ERR == m_combo.FindString(-1, strText))
{
m_combo.AddString(strText);
}
}
UINT CWinPingDlg::PingThreadProc(LPVOID lParam)
{
bool close=false;
bool open=true;
HWND hWnd = (HWND)lParam;
ASSERT(hWnd);
//设置隐藏窗口
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.cbReserved2 = NULL;
si.lpReserved2 = NULL;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;
TCHAR cmdline[] =TEXT("ping 192.168.1.2");
bool bsucess = CreateProcess(NULL,
cmdline,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if(bsucess)
{
HANDLE m_hProcess=pi.hProcess;
CloseHandle(pi.hThread);
printf("WaitForSingleObject\n");
WaitForSingleObject(m_hProcess,INFINITE);
DW
没有合适的资源?快使用搜索试试~ 我知道了~
代码实现网卡关闭后重启
共86个文件
tlog:27个
h:7个
cpp:5个
需积分: 10 7 下载量 189 浏览量
2016-07-22
11:18:03
上传
评论
收藏 38.75MB RAR 举报
温馨提示
本程序设置了隐藏窗口,实现ping某一个网络IP,如果ping通后则退出,否则关闭该网卡然后重启;在xp和win7下测试通过,需要注意的是,在win7下需要以管理员的身份运行,这是系统版本所决定的,希望对大家有帮助!
资源推荐
资源详情
资源评论
收起资源包目录
后台关闭网卡后重启隐藏对话框.rar (86个子文件)
后台关闭网卡后重启隐藏对话框
WinPing
stdafx.cpp 209B
WinPing.suo 18KB
WinPing.sdf 62.96MB
UpgradeLog.XML 4KB
res
WinPing.ico 66KB
WinPing.rc2 398B
WinPing.vcxproj.user 143B
_UpgradeReport_Files
UpgradeReport.xslt 12KB
UpgradeReport_Minus.gif 69B
UpgradeReport_Error.png 397B
UpgradeReport.css 3KB
UpgradeReport_Information.png 338B
UpgradeReport_Plus.gif 71B
UpgradeReport_Warning.png 292B
UpgradeReport_Success.png 381B
stdafx.h 2KB
AutoCompleteComboBox.h 388B
ProtoInfo.cpp 2KB
targetver.h 1KB
WinPingDlg.cpp 8KB
WinPing.aps 103KB
WinPing.vcproj 6KB
WinPing.h 609B
WinPing.sln 1KB
UpgradeLog2.XML 1KB
WinPing.v11.suo 30KB
resource.h 771B
WinPing.exe 167KB
WinPing.vcxproj.filters 2KB
WinPing.rc 6KB
Backup
WinPing.sln 879B
WinPing.v11.suo 14KB
AutoCompleteComboBox.cpp 1KB
WinPing.vcxproj 8KB
ipch
winping-335956ae
winping-f5c9ce48.ipch 84MB
UpgradeLog.htm 19KB
Debug
vc100.idb 1.89MB
link.2944.read.1.tlog 2B
WinPingDlg.obj 83KB
WinPing.res 68KB
CL.write.1.tlog 3KB
link.2944.write.1.tlog 2B
CL.read.1.tlog 79KB
mt.read.1.tlog 664B
WinPing.pdb 5.64MB
rc.write.1.tlog 494B
stdafx.obj 612KB
WinPing.vcxprojResolveAssemblyReference.cache 713B
rc.read.1.tlog 3KB
link.3316.write.1.tlog 2B
ProtoInfo.obj 19KB
WinPing.exe.embed.manifest.res 732B
WinPing.ilk 1.52MB
WinPing.log 3KB
mt.command.1.tlog 740B
link.1404.read.1.tlog 2B
cl.command.1.tlog 4KB
link-cvtres.read.1.tlog 2B
WinPing.exe.intermediate.manifest 381B
WinPing.pch 31.69MB
link.2944-cvtres.read.1.tlog 2B
link.write.1.tlog 1KB
link.1404-cvtres.read.1.tlog 2B
link.3316-cvtres.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
link.2944-cvtres.write.1.tlog 2B
WinPing.exe 162KB
rc.command.1.tlog 1020B
link.read.1.tlog 6KB
link.1404-cvtres.write.1.tlog 2B
WinPing.write.1.tlog 0B
link.3316-cvtres.read.1.tlog 2B
WinPing_manifest.rc 204B
link.1404.write.1.tlog 2B
mt.write.1.tlog 482B
WinPing.lastbuildstate 64B
WinPing.Build.CppClean.log 2KB
link.3316.read.1.tlog 2B
AutoCompleteComboBox.obj 26KB
WinPing.obj 34KB
vc100.pdb 3.92MB
WinPing.exe.embed.manifest 667B
WinPing.cpp 3KB
ProtoInfo.h 2KB
WinPingDlg.h 992B
共 86 条
- 1
资源评论
南大不倒翁
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功