26 #pragma comment(lib,"RasApi32.lib")
27 #if ( WINVER >= 0x500 )
46#define _DIALUP_TRACE(FUNC, VAL) \
47 if ( (VAL) != 0 ) { TRACE3("%s failed: Error %u[%s]\n", _T(#FUNC), VAL, GetErrorString(VAL)); }
117 E_ConnectCheckInterval = 100,
118 E_DisconnectCheckInterval = 500,
130 TCHAR szEntryName[RAS_MaxEntryName + 1];
132 TCHAR szDeviceType[RAS_MaxDeviceType + 1];
133 TCHAR szDeviceName[RAS_MaxDeviceName + 1];
134 TCHAR szPhonebook[MAX_PATH];
141 operator LPRASCONN(){
return reinterpret_cast<LPRASCONN
>(
this); }
148 bool IsPhoneBookEqual(
CStr& s)
const
166 static CRasConnVector ms_GetConnectingEnties(
void)
168 CRasConnVector aRasConn;
170 TRasConn* P =
reinterpret_cast<TRasConn*
>(ba.
GetBuffer(
sizeof(TRasConn)));
171 P->dwSize =
sizeof(TRasConn);
172 DWORD dwBufferSize =
sizeof(TRasConn);
174 DWORD dwRc = ::RasEnumConnections(*P, &dwBufferSize, &dwEntries);
175 if ( dwRc == ERROR_BUFFER_TOO_SMALL )
177 P =
reinterpret_cast<TRasConn*
>(ba.
GetBuffer(dwBufferSize));
178 P->dwSize =
sizeof(TRasConn);
179 dwRc = ::RasEnumConnections(*P, &dwBufferSize, &dwEntries);
181 if ( dwRc != ERROR_SUCCESS )
189 loop ( i, dwEntries )
204 void m_ClearHandleRasConn(
void)
206 if ( m_hRasConn != NULL )
217 static CHndThisMap& ms_ElementMap(
void)
219 static CHndThisMap s_mapHndThis;
233 ms_RasDialFunc1(HRASCONN h, UINT uMsg,
RASCONNSTATE state, DWORD dwError, DWORD dwExError)
237 TRACE3(
"RasDialFunc s=%d e=%u[%s]\n", state, dwError,
GetErrorString(dwError));
239 if ( ms_ElementMap().Lookup(h, P) != 0 )
242 if ( (
reinterpret_cast<ULONG_PTR
>(P) & 0x80000000) == 0 )
244 if ( P->m_piListener != NULL )
246 if ( P->m_threadWatch.
IsAlive() )
256 ms_ElementMap()[h] =
reinterpret_cast<CDialupManager*
>(
static_cast<size_t>(dwError | 0x80000000));
265 virtual DWORD Run(
void)
268 RASCONNSTATUS tStatus;
273 bool boIsFirst =
true;
276 ::Sleep(E_ConnectCheckInterval);
281 if ( dwRc == ERROR_INVALID_HANDLE )
288 if ( tStatus.dwError != 0 )
297 eNowState = tStatus.rasconnstate;
301 else if ( eNowState != tStatus.rasconnstate )
303 eNowState = tStatus.rasconnstate;
316 TRACE1(
"ChangeState =[%s]\n",
StateToString(tStatus.rasconnstate));
317 m_ClearHandleRasConn();
321 ::Sleep(E_DisconnectCheckInterval);
327 m_ClearHandleRasConn();
338 static void ms_CheckPhoneBook(LPCTSTR& _lpszPhoneBook)
340 if ( _lpszPhoneBook != NULL && _lpszPhoneBook[0] ==
'\0' )
342 _lpszPhoneBook = NULL;
347 static LPCTSTR NcStr(LPCTSTR lpsz) {
return lpsz; }
354 NcStr(LPCTSTR lpsz) : m_str(lpsz), m_isNull(lpsz == NULL) {}
355 operator LPTSTR(
void) {
return m_isNull ? NULL : m_str.
GetBuffer(256); }
415 m_ClearHandleRasConn();
426 return m_hRasConn != NULL;
439 bool Attach(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryName,
IListener* piDialupListener = NULL)
442 ms_CheckPhoneBook(lpszPhoneBook);
444 HRASCONN hRasConn =
GetRasConn(lpszPhoneBook, lpszEntryName);
445 if ( hRasConn != NULL )
448 m_hRasConn = hRasConn;
449 ms_ElementMap()[m_hRasConn] =
this;
450 m_piListener = piDialupListener;
451 if ( piDialupListener != NULL )
454 m_threadWatch.
Start(
CStr(lpszEntryName) + _T(
"Dialup"));
456 m_threadWatch.
Start();
480 TRACE0(
"Dial パラメータ異常\n");
481 return ERROR_INVALID_PARAMETER;
489 LPRASENTRY ptEntry =
reinterpret_cast<LPRASENTRY
>(ba.
GetBuffer(dwSize));
490 ptEntry->dwSize = dwSize;
512 t.dwSize =
sizeof(RASDIALPARAMS);
517 t.szCallbackNumber[0] =
'\0';
518 t.szDomain[0] =
'\0';
520 HRASCONN hRasConn = NULL;
521 if ( piDialupListener == NULL )
524 dwRc = ::RasDial(NULL, D.
GetPhoneBook(), &t, 0, NULL, &hRasConn);
529 dwRc = ::RasDial(NULL, D.
GetPhoneBook(), &t, 1, ms_RasDialFunc1, &hRasConn);
537 dwRc = ::RasSetEntryDialParams(D.
GetPhoneBook(), &t,
false);
545 m_hRasConn = hRasConn;
548 if ( ms_ElementMap().Lookup(hRasConn, pMan) != 0 )
550 DWORD dwError =
static_cast<DWORD
>(
reinterpret_cast<size_t>(pMan));
551 ASSERTLIB((dwError & 0x80000000) != 0);
552 dwError &= 0x7FFFFFFF;
558 ms_ElementMap()[hRasConn] =
this;
559 if ( piDialupListener != NULL )
561 m_piListener = piDialupListener;
565 m_threadWatch.
Start();
578 if ( m_hRasConn == NULL )
582 m_threadWatch.
Stop();
584 m_ClearHandleRasConn();
597 if ( m_hRasConn == NULL )
603 m_threadWatch.
Stop();
604 DWORD dwRc = ::RasHangUp(m_hRasConn);
607 RASCONNSTATUS tStatus;
616 if ( dwRc == ERROR_NO_CONNECTION )
621 m_ClearHandleRasConn();
633 _tStatus.dwSize =
sizeof(RASCONNSTATUS);
634 DWORD dwRc = ::RasGetConnectStatus(m_hRasConn, &_tStatus);
647 RASCONNSTATUS tStatus;
651 str = tStatus.szDeviceName;
682 return m_threadWatch;
696 RASPPPIP tRasPppIp = {
sizeof(RASPPPIP) };
697 DWORD dwLen =
sizeof(RASPPPIP);
698 DWORD dwRc = ::RasGetProjectionInfo(hRasConn, RASP_PppIp, &tRasPppIp, &dwLen);
702 return tRasPppIp.szIpAddress;
713 static HRASCONN
GetRasConn(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryname)
715 CStr strPb = lpszPhoneBook;
719 const TRasConn &T = aRass.
At(i);
722 if ( T.IsPhoneBookEqual(strPb) )
725 HRASCONN hRasComm = T.hRasConn;
726 RASCONNSTATUS tStatus = {
sizeof(RASCONNSTATUS) };
727 DWORD dwRc = ::RasGetConnectStatus(hRasComm, &tStatus);
747 ms_CheckPhoneBook(lpszPhoneBook);
750 LPRASENTRYNAME P =
reinterpret_cast<LPRASENTRYNAME
>(ba.
GetBuffer(
sizeof(RASENTRYNAME)));
751 P->dwSize =
sizeof(RASENTRYNAME);
752 DWORD dwBufferSize =
sizeof(RASENTRYNAME);
754 DWORD dwRc = ::RasEnumEntries(NULL, NcStr(lpszPhoneBook), P, &dwBufferSize, &dwEntries);
755 if ( dwRc == ERROR_BUFFER_TOO_SMALL )
757 P =
reinterpret_cast<LPRASENTRYNAME
>(ba.
GetBuffer(dwBufferSize));
758 P->dwSize =
sizeof(RASENTRYNAME);
759 dwRc = ::RasEnumEntries(NULL, NcStr(lpszPhoneBook), P, &dwBufferSize, &dwEntries);
761 if ( dwRc != ERROR_SUCCESS )
770 loop ( i, dwEntries )
772 vstrNames.
Add(P->szEntryName);
791 return vstrEntryNames;
797 vstrEntryNames.
Add(aRass.
At(i).szEntryName);
800 return vstrEntryNames;
811 ms_CheckPhoneBook(lpszPhoneBook);
814 RASDIALPARAMS tParams = {
sizeof(RASDIALPARAMS) };
817 BOOL boHasPassword = TRUE;
818 DWORD dwParamsRc = ::RasGetEntryDialParams(NcStr(lpszPhoneBook), &tParams, &boHasPassword);
819 if ( dwParamsRc != 0 )
826 ::RasGetEntryProperties(NcStr(lpszPhoneBook), NcStr(lpszEntryName), NULL, &dwSize, NULL, NULL);
828 LPRASENTRY ptEntry =
reinterpret_cast<LPRASENTRY
>(ba.
GetBuffer(dwSize));
829 ptEntry->dwSize = dwSize;
830 DWORD dwPropertyRc = ::RasGetEntryProperties(NcStr(lpszPhoneBook), NcStr(lpszEntryName), ptEntry, &dwSize, NULL, NULL);
831 if ( dwPropertyRc != 0 )
860 LPRASDEVINFO P =
reinterpret_cast<LPRASDEVINFO
>(ba.
GetBuffer(
sizeof(RASDEVINFO)));
861 P->dwSize =
sizeof(RASDEVINFO);
862 DWORD dwBufferSize =
sizeof(RASDEVINFO);
864 DWORD dwRc = ::RasEnumDevices(P, &dwBufferSize, &dwEntries);
865 if ( dwRc == ERROR_BUFFER_TOO_SMALL )
867 P =
reinterpret_cast<LPRASDEVINFO
>(ba.
GetBuffer(dwBufferSize));
868 P->dwSize =
sizeof(RASDEVINFO);
869 dwRc = ::RasEnumDevices(P, &dwBufferSize, &dwEntries);
871 if ( dwRc != ERROR_SUCCESS)
880 loop ( i, dwEntries )
882 if ( ! boIsModemOnly ||
STRLIB::Compare(P->szDeviceType, RASDT_Modem) == 0)
884 vstrNames.
Add(P->szDeviceName);
904 OSVERSIONINFO tOsVerInfo = {
sizeof(OSVERSIONINFO) };
905 ::GetVersionEx(&tOsVerInfo);
908 if ( tOsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
911 strModemBase =
"SYSTEM\\CurrentControlSet\\Services\\Class\\Modem";
916 strModemBase =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E96D-E325-11CE-BFC1-08002BE10318}";
919 int iFoundItemNumber = 0;
940 int iPort = strPort.
ToInt(3);
941 if ( iPort < 0 || iPort > 256 )
945 _vstrModemNames.
Set(iPort, strModemName);
948 return iFoundItemNumber;
962 OSVERSIONINFO tOsVerInfo = {
sizeof(OSVERSIONINFO) };
963 ::GetVersionEx(&tOsVerInfo);
966 if ( tOsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
969 strModemBase =
"SYSTEM\\CurrentControlSet\\Services\\Class\\Modem";
974 strModemBase =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E96D-E325-11CE-BFC1-08002BE10318}";
984 if ( strModemName.
IsEqual(lpszModemName) )
987 if ( lpszCommand == NULL || lpszCommand[0] == 0 )
1015 LPCTSTR lpszPhoneBook,
1016 LPCTSTR lpszEntryName,
1017 LPCTSTR lpszDeviceName,
1018 LPCTSTR lpszAreaCode,
1019 LPCTSTR lpszPhoneNumber,
1020 LPCTSTR lpszUserName,
1021 LPCTSTR lpszPassword )
1023 if ( lpszPhoneBook != NULL && lpszPhoneBook[0] ==
'\0' )
1025 lpszPhoneBook = NULL;
1028 RASENTRY rasEntry = {
sizeof(RASENTRY) };
1029 rasEntry.dwfOptions =
1031 RASEO_SpecificNameServers
1033 | RASEO_RemoteDefaultGateway;
1035 CStr strAreaCode = lpszAreaCode;
1036 if ( ! strAreaCode.
IsEmpty() )
1039 rasEntry.dwfOptions |= RASEO_UseCountryAndAreaCodes;
1041 rasEntry.dwfOptions = 0x38010118;
1043 rasEntry.dwCountryID = 81;
1044 rasEntry.dwCountryCode = 81;
1045 rasEntry.dwfNetProtocols = RASNP_Ip;
1046 rasEntry.dwFramingProtocol = RASFP_Ppp;
1047 _tcsncpy(rasEntry.szAreaCode, strAreaCode,
sizeof(rasEntry.szAreaCode) - 1);
1048 _tcsncpy(rasEntry.szLocalPhoneNumber, lpszPhoneNumber,
sizeof(rasEntry.szLocalPhoneNumber) - 1);
1049 _tcsncpy(rasEntry.szDeviceType, RASDT_Modem,
sizeof(rasEntry.szDeviceType) - 1);
1050 _tcsncpy(rasEntry.szDeviceName, lpszDeviceName,
sizeof(rasEntry.szDeviceName) - 1);
1052 dwError = ::RasSetEntryProperties (NcStr(lpszPhoneBook), NcStr(lpszEntryName), &rasEntry,
sizeof(RASENTRY), NULL, NULL);
1058 RASDIALPARAMS dialParm = {
sizeof(RASDIALPARAMS)};
1059 dialParm.dwSize =
sizeof (RASDIALPARAMS);
1060 _tcsncpy(dialParm.szEntryName, lpszEntryName,
sizeof(dialParm.szEntryName) - 1);
1061 _tcsncpy(dialParm.szUserName, lpszUserName,
sizeof(dialParm.szUserName) - 1);
1062 _tcsncpy(dialParm.szPassword, lpszPassword,
sizeof(dialParm.szPassword) - 1);
1064 dwError = ::RasSetEntryDialParams(NcStr(lpszPhoneBook), &dialParm, FALSE);
1082 ::RasGetErrorString(dwError, str.
GetBuffer(256), 256 *
sizeof(TCHAR));
1085 if ( dwError >= 600 && dwError <= 752 )
1087 str.
Format(_T(
"RASエラー(%d)"), dwError);
1128 #if ( WINVER >= 0x400 )
1139 #if ( WINVER >= 0x500 )
1147 return (P != NULL) ?
CStr(P) :
CStr::Fmt(_T(
"Unknown State Code [%d]"), eState);
#define loop(VAR, CNT)
loop構文.
#define _DIALUP_TRACE(FUNC, VAL)
本ヘッダ専用 TRACE マクロ
@ RASCS_CallbackSetByCaller
CallbackSetByCaller
@ RASCS_SubEntryConnected
SubEntryConnected
@ RASCS_AuthCallback
認証をコールバック中です
@ RASCS_PrepareForCallback
コールバック準備中です
@ RASCS_Projected
投影終了しました
@ RASCS_InvokeEapUI
InvokeEapUI
@ RASCS_AuthNotify
認証を終了しました
@ RASCS_CallbackComplete
コールバックが完了しました
@ RASCS_LogonNetwork
ネットワークにログオンしました
@ RASCS_OpenPort
ポートを開いています
@ RASCS_Authenticated
認証が終了しました
@ RASCS_WaitForCallback
コールバックの待機中です
@ RASCS_AllDevicesConnected
すべての機器を接続しました
@ RASCS_WaitForModemReset
モデムのリセット待ちです
@ RASCS_PortOpened
ポートを開きました
@ RASCS_SubEntryDisconnected
SubEntryDisconnected
@ RASCS_Interactive
Interactive
@ RASCS_AuthProject
投影の認証中です
@ RASCS_DeviceConnected
機器を接続しました
@ RASCS_ConnectDevice
機器を接続しています
@ RASCS_PasswordExpired
PasswordExpired
@ RASCS_AuthLinkSpeed
接続速度を認証中です
@ RASCS_Authenticate
認証中です
@ RASCS_AuthChangePassword
認証パスワードを変更しています
@ RASCS_ReAuthenticate
再認証中です
@ RASCS_AuthRetry
認証をリトライ中です
@ RASCS_RetryAuthentication
RetryAuthentication
@ RASCS_StartAuthentication
認証を開始します
CThreadStatus & ReferThreadStatus(void)
[参照] 監視スレッド状態参照.
bool IsAttach(void) const
[確認] アタッチしているか.
CStr GetIpAddress(void) const
[取得] IPアドレス取得.
static TDialParams GetDialParams(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryName)
[取得] エントリのダイアルパラメータ取得
bool Attach(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryName, IListener *piDialupListener=NULL)
[設定] アタッチ.
DWORD GetConnectStatus(RASCONNSTATUS &_tStatus) const
[取得] 接続状態取得
CDialupManager(void)
コンストラクタ
static CStr GetIpAddress(HRASCONN hRasConn)
[取得] 接続のIPアドレス取得
static int SetUserInitCommand(LPCTSTR lpszModemName, LPCTSTR lpszCommand)
[取得] デバイス初期化コマンド設定.
static int EnumModemPorts(CStrVector &_vstrModemNames)
[取得] デバイス(モデム)名一覧.
DWORD Dial(const TDialParams &D, IListener *piDialupListener=NULL)
[操作] ダイアル.
static bool CreatePhoneBookEntry(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryName, LPCTSTR lpszDeviceName, LPCTSTR lpszAreaCode, LPCTSTR lpszPhoneNumber, LPCTSTR lpszUserName, LPCTSTR lpszPassword)
[作成] 電話エントリ作成
static CStr GetErrorString(DWORD dwError)
[変換] エラーコードを文字列に変換
void Detach(void)
[設定] デタッチ.
static CStrVector EnumDeviceNames(bool boIsModemOnly=true)
[取得] 接続されているデバイス名一覧
static CStrVector EnumConnectedEntryNames(void)
[取得] 接続中エントリ名一覧
CStr GetConnectedDeviceName(void) const
[取得] デバイス名取得.
DWORD HangUp(void)
[操作] 切断.
static HRASCONN GetRasConn(LPCTSTR lpszPhoneBook, LPCTSTR lpszEntryname)
[確認] 指定Entry接続完了状態チェック
virtual ~CDialupManager(void)
デストラクタ
static CStr StateToString(RASCONNSTATE eState)
[変換] ステータスを文字列に変換
static CStrVector EnumEntryNames(LPCTSTR lpszPhoneBook)
[取得] エントリ名一覧
bool RemoveKey(INK key)
[削除] キーと値を削除
virtual CStrVector EnumSectionNames(LPCTSTR lpszSectionName=NULL) const
[取得] セクション名一覧取得
static VAL Find(const TYP &key, const TYPS &ts, const VAL &def=VAL())
[検索] 値検索
int Compare(const TYP *lpszSubject) const
[確認] 文字列比較
bool IsEmpty(void) const
[確認] 空チェック
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
size_t GetLength(void) const
[取得] 文字列長
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
int Remove(TYP t)
[処理] 文字削除.
bool IsEqual(const TYP *lpszSubject) const
[確認] 文字列比較
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
int ToInt(INDEX iOffset=0, int iBase=10) const
[取得] 数値(int)へ変換
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
bool IsAlive(void) const
[確認] スレッドが生きているか
bool SetRunner(IRunner *pRunner)
[設定] ランナー、設定
bool Stop(DWORD dwWait=15000)
[設定] スレッド停止 スレッドに対して停止要求します。
bool Start(LPCTSTR lpszName=NULL)
[設定] スレッド開始
virtual size_t GetSize(void) const
[取得] サイズ取得
TYP * GetBuffer(size_t size=0)
[操作] データアドレス取得
void SetIncrementSize(size_t size)
[設定] 余白サイズ
virtual const TYP & At(INDEX index) const
[取得] 要素の参照取得.
void Invalid(void)
[操作] 無効状態にする
bool IsValid(void) const
[確認] 有効チェック
virtual bool SetSize(size_t size)
[操作] サイズ指定
virtual bool RemoveAll(void)
[削除] 空化
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
virtual bool Set(INDEX index, const TYP &t)
[設定] 要素の設定.
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
bool WriteString(LPCTSTR lpszKey, LPCTSTR lpszValue)
[追加] 文字列情報記録
bool DeleteKey(LPCTSTR lpszKey)
[削除] 指定キー削除
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
TNB::CStrT< TCHAR > CStr
文字列クラス
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
void SystemErrorToMessageText(CSimpleStr &_str, DWORD dwError)
[変換] SystemErrorコード文字列化.
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
virtual void OnDialupChangeState(RASCONNSTATE eState)=0
[通知] 状態変化発生
virtual void OnDialupError(DWORD dwError)=0
[通知] エラー発生.
virtual ~IListener(void)
デストラクタ
LPCTSTR GetPhoneBook(void) const
電話帳名取得
bool IsInvalid(void) const
本情報は有効か否か
bool IsRunnable(void) const
[確認] 実行可能か否か