본문 바로가기

먼저, 레지스트리의HKEY_LOCAL_MACHINE\HARDWARE\\DEVICEMAP\\SERIALCOMM
경로에 나열된 포트들은 현재 사용가능한 것들입니다. 이 키들의 값(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.
}





번호 제목 날짜 조회 수
HOT [TIP] PYTHON 에서 "UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence" 오류 날때... [52] 2016.02.20 267326
HOT [펌] ARM GCC Inline Assembler Cookbook 2006.08.18 58661
HOT [TIP] R에서 페이스북 페이지 정보 크롤링 하기 [6] 2017.02.11 40638
130 [TIP] 파이선에서 구글 스프레드시트 사용하기 [5] 2016.03.27 6385
129 [TIP] 파이선에서 리스트를 사용하는 재미있는 예제 2016.03.15 1974
128 [TIP] PYTHON 에서 "UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence" 오류 날때... [52] 2016.02.20 267326
127 [TIP] 퍼포먼스를 높여주는 몇가지 코딩 예 2016.01.22 1605
126 [TIP] JQuery와 PHP로 이미지 파일을 업로드 하는 간단한 소스 (How to upload image file on PHP server by using JQuery) 2015.04.03 14427
125 [TIP] 자작 커널을 위한 인터럽트 벡터 수정 (EZ-2410보드 대상) 2013.05.04 1562
124 위치추적 [1] 2013.04.06 15794
123 [TIP] MySQL - NaviCat 사용시 에러 확인방법 2010.02.23 1815
122 [TIP] 설치된 코덱 리스트 얻어오기 2010.02.18 1356
121 [TIP] VFW 에서 영상 압축을 위한 코덱을 코드상에서 선택하여 실시간 저장하는 방법입니다. 2010.02.04 1527
120 [TIP] Visual Studio App Wizard 만들기 관련 2009.12.17 1374
119 [TIP] C# 에서 Win32 DLL 을 사용하기 위한 방법 2009.07.16 1569
» [TIP] WMI 이용, 사용가능한 모뎀 포트 알아내기 2009.07.05 1312
117 [TIP] Excel Ole 를 사용시 Hidden 열 혹은 행인지 알아내는 방법 2009.05.17 1388
116 [TIP] Win32 API를 사용하여 응용 프로그램 열거하기 2009.01.15 1075
115 [TIP] 홈페이지 자동 클릭하기 소스 2009.10.25 2371
114 [TIP] (MSDN) VS2005 에서 BHO 만드는 방법 링크 2009.01.06 984
113 [TIP] Windows 에서 COM10 이상을 여는 방법 [2] 2008.05.26 1548
112 [TIP] 인터넷으로 문자메시지 (SMS) 보내기 2008.03.23 3783
111 [TIP] 동적 DLL이 가진 Dialog를 Child Window로 띄우기 2008.03.16 2100