먼저, 레지스트리의HKEY_LOCAL_MACHINE\HARDWARE\\DEVICEMAP\\SERIALCOMM
경로에 나열된 포트들은 현재 사용가능한 것들입니다. 이 키들의 값(Port 이름)을 하나씩 가지고 와서 아래 WMI 쿼리에 적용시킵니다.
그리하여 값을 가지고 오는데에 성공했다면 해당하는 포트가 모뎀 포트가 됩니다.^^
경로에 나열된 포트들은 현재 사용가능한 것들입니다. 이 키들의 값(Port 이름)을 하나씩 가지고 와서 아래 WMI 쿼리에 적용시킵니다.
그리하여 값을 가지고 오는데에 성공했다면 해당하는 포트가 모뎀 포트가 됩니다.^^
참고사이트: http://topic.csdn.net/u/20071108/22/841e0600-221c-4551-8ac6-64a34693db04.html
#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <windows.h>
#include <comdef.h>
#include <wbemcli.h>
#pragma comment(lib, "Wbemuuid")
BOOL WMI(CString strWQL,CString strOutputFile)
{
FILE *stream = NULL;
HRESULT hres ;
// Initialize COM.
hres=CoInitializeEx(0,COINIT_MULTITHREADED);
if(FAILED(hres))
{
cout<<"Failed to initialize COM library. Error code = 0x"
<<hex<<hres<<endl ;
return FALSE ;
// Program has failed.
}
// Initialize
hres=CoInitializeSecurity(
NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
if(FAILED(hres))
{
cout<<"Failed to initialize security. Error code = 0x"
<<hex<<hres<<endl ;
CoUninitialize();
return FALSE ;
// Program has failed.
}
// Obtain the initial locator to Windows Management on a particular host computer.
IWbemLocator*pLoc=0 ;
hres=CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,
IID_IWbemLocator,(LPVOID*)&pLoc);
if(FAILED(hres))
{
cout<<"Failed to create IWbemLocator object. Err code = 0x"
<<hex<<hres<<endl ;
CoUninitialize();
return FALSE ;
// Program has failed.
}
IWbemServices*pSvc=0 ;
// Connect to the root\cimv2 namespace with the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres=pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"),
NULL,NULL,0,NULL,0,0,&pSvc);
if(FAILED(hres))
{
cout<<"Could not connect. Error code = 0x"
<<hex<<hres<<endl ;
pLoc->Release();
CoUninitialize();
return 1 ;
// Program has failed.
}
cout<<"Connected to WMI"<<endl ;
// Set the IWbemServices proxy so that impersonation of the user (client) occurs.
hres=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(hres))
{
cout<<"Could not set proxy blanket. Error code = 0x"
<<hex<<hres<<endl ;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE ;
// Program has failed.
}
// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:
// For example, query for print queues that have more than 10 jobs
IEnumWbemClassObject*pEnumerator=NULL ;
hres=pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(strWQL),//"SELECT * from Win32_LogicalDisk"),
WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if(FAILED(hres))
{
cout<<"Query for print queues failed. Error code = 0x"
<<hex<<hres<<endl ;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE ;
// Program has failed.
}
else
{
stream = _wfopen(strOutputFile, _T("w") );
if(stream == NULL)
{
return false;
}
IWbemClassObject*pInstance=NULL ;
ULONG dwCount ;
while(pEnumerator->Next(
WBEM_INFINITE,
1,
&pInstance,
&dwCount)==S_OK)
{
SAFEARRAY*pvNames=NULL ;
if(pInstance->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_MASK_CONDITION_ORIGIN,NULL,&pvNames)==S_OK)
{
long vbl,vbu ;
SafeArrayGetLBound(pvNames,1,&vbl);
SafeArrayGetUBound(pvNames,1,&vbu);
for(long idx=vbl;idx<=vbu;idx++)
{
long aidx=idx ;
wchar_t *wsName=0 ;
VARIANT vValue ;
VariantInit(&vValue);
SafeArrayGetElement(pvNames,&aidx,&wsName);
BSTR bs=SysAllocString(wsName);
hres=pInstance->Get(bs,0,&vValue,NULL,0);
SysFreeString(bs);
if(SUCCEEDED(hres))
{
switch(vValue.vt)
{
case VT_BSTR :
fwprintf(stream,L"%s\n",V_BSTR(&vValue));
break ;
case VT_I2 :
fwprintf(stream,L"%d\n",V_I2(&vValue));
break ;
case VT_I4 :
fwprintf(stream,L"%d\n",V_I4(&vValue));
break ;
case VT_BOOL :
fwprintf(stream,L"%s\n",V_BOOL(&vValue)?L"TRUE":L"FALSE");
break ;
default:
/*WCHAR tmp[100];
wcscpy(tmp, V_BSTR(&vValue));
char tmp1[MAX_PATH];
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,tmp,-1,
tmp1,sizeof(tmp1),NULL,NULL);
cout<<tmp1;*/
fwprintf(stream,_T("\n"));
break;
}
// cout<<endl ;
}
SysFreeString(wsName);
}
// fprintf(stream,"\n");
}
else
{
cout<<"Query for print queues failed. Error code = 0x"
<<hex<<hres<<endl ;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE ;
// Program has failed.
}
if(pvNames)SafeArrayDestroy(pvNames);
}
if(pInstance)pInstance->Release();
}
// Cleanup
// ========
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
if(NULL != stream)
{
fclose( stream );
}
return TRUE;
// Program successfully completed.
}