strComputer = "."Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("Select * from MSNdis_StatusMediaDisconnect") Do While True
Set strLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo "网络电缆连接断开!"
WScript.Echo strLatestEvent.InstanceName, Now
Wscript.Echo
Loop
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("Select * from MSNdis_StatusMediaDisconnect") Do While True
Set strLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo "网络电缆连接断开!"
WScript.Echo strLatestEvent.InstanceName, Now
Wscript.Echo
Loop
ZeroMemory(strComputer,sizeof(strComputer));
strcpy(strComputer,".");
//
//Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
//不知道你这个地方用的什么组件
const BSTR strQueryLanguage,
const BSTR strQuery,
long lFlags,
IWbemContext* pCtx,
IEnumWbemClassObject** ppEnum
);
[1]http://msdn.microsoft.com/en-us/library/aa389762(VS.85).aspx
[2]http://msdn.microsoft.com/en-us/library/aa389276(VS.85).aspx
//#include "stdafx.h"
#include "wmi.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <windows.h>
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")// 唯一的应用程序对象CWinApp theApp;using namespace std;HRESULT doThings()
{
IWbemLocator *pLoc = 0;
HRESULT hr; hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr))
{
cout << "Failed to create IWbemLocator object. Err code = 0x"
<< hex << hr << endl;
return hr; // Program has failed.
} IWbemServices *pSvc = 0; CComBSTR strNetworkResource("winmgmts:\\\\computername\\root\\wmi"); // Connect to the root\default namespace with the current user.
hr = pLoc->ConnectServer(
strNetworkResource,
NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hr))
{
cout << "Could not connect. Error code = 0x"
<< hex << hr << endl;
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
} cout << "Connected to WMI" << endl; // Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
); if (FAILED(hr))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hr << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
} //do your things
{
CComBSTR strLang("WQL");
CComBSTR strQuery("Select * from MSNdis_StatusMediaDisconnect");
IEnumWbemClassObject* pResult = NULL; hr = pSvc->ExecNotificationQuery(strLang, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pResult);
if(SUCCEEDED(hr))
{
do{
IWbemClassObject* pObject = NULL;
ULONG lCnt = 0;
hr = pResult->Next(1, 1, &pObject, &lCnt);
if(SUCCEEDED(hr) && pObject)
{
cout << L"网络电缆连接断开!"; VARIANT vtRet;
pObject->Get(L"InstanceName", 0, &vtRet, NULL, NULL); cout << vtRet.bstrVal ;
}
}
while(true);
} } pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0; // Program successfully completed.
}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; // 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
doThings();
CoUninitialize();
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
} return nRetCode;
}
// wmi.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include "wmi.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <windows.h>
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")// 唯一的应用程序对象CWinApp theApp;using namespace std;HRESULT doThings()
{
IWbemLocator *pLoc = 0;
HRESULT hr; hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr))
{
cout << "Failed to create IWbemLocator object. Err code = 0x"
<< hex << hr << endl;
return hr; // Program has failed.
} IWbemServices *pSvc = 0; CComBSTR strNetworkResource("winmgmts:\\\\computername\\root\\wmi"); // Connect to the root\default namespace with the current user.
hr = pLoc->ConnectServer(
strNetworkResource,
NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hr))
{
cout << "Could not connect. Error code = 0x"
<< hex << hr << endl;
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
} cout << "Connected to WMI" << endl; // Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
); if (FAILED(hr))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hr << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
} //do your things
{
CComBSTR strLang("WQL");
CComBSTR strQuery("Select * from MSNdis_StatusMediaDisconnect");
IEnumWbemClassObject* pResult = NULL; hr = pSvc->ExecNotificationQuery(strLang, strQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pResult);
if(SUCCEEDED(hr))
{
do{
IWbemClassObject* pObject = NULL;
ULONG lCnt = 0;
hr = pResult->Next(1, 1, &pObject, &lCnt);
if(SUCCEEDED(hr) && pObject)
{
cout << L"网络电缆连接断开!"; VARIANT vtRet;
pObject->Get(L"InstanceName", 0, &vtRet, NULL, NULL); cout << vtRet.bstrVal ;
}
}
while(true);
} } pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0; // Program successfully completed.
}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; // 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
doThings();
CoUninitialize();
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
} return nRetCode;
}
编译有问题--------------------Configuration: WMI - Win32 Debug--------------------
Compiling...
WMI.cpp
c:\program files\microsoft visual studio\vc98\include\new(35) : error C2833: 'operator DEBUG_NEW' is not a recognized operator or type
c:\program files\microsoft visual studio\vc98\include\new(35) : error C2059: syntax error : 'newline'
c:\program files\microsoft visual studio\vc98\include\new(36) : error C2833: 'operator DEBUG_NEW' is not a recognized operator or type
c:\program files\microsoft visual studio\vc98\include\new(36) : error C2059: syntax error : 'newline'
c:\program files\microsoft visual studio\vc98\include\new(41) : error C2833: 'operator DEBUG_NEW' is not a recognized operator or type
c:\program files\microsoft visual studio\vc98\include\new(41) : error C2059: syntax error : 'newline'
c:\program files\microsoft visual studio\vc98\include\new(42) : error C2143: syntax error : missing ';' before '{'
c:\program files\microsoft visual studio\vc98\include\new(42) : error C2447: missing function header (old-style formal list?)
F:\WMI.cpp(12) : fatal error C1083: Cannot open include file: 'wbemidl.h': No such file or directory
执行 cl.exe 时出错.WMI.obj - 1 error(s), 0 warning(s)
然后再对比一下,要引入哪些头文件和库。
另外:int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; // 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
doThings();
CoUninitialize();
} return nRetCode;
}