📄 rasdemo.cpp
字号:
// RasDemo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "Ras.h"
#include "Raserror.h"
#include "RasStateMessage.h"
BOOL CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
void DoCommand(HWND, UINT, WPARAM, LPARAM);
DWORD DialRasNetwork(HWND);
void GetRasBookEntry();
LPRASDIALPARAMS GetRasDialParams(TCHAR* name);
void ParseRasDialEvent(HWND, WPARAM, LPARAM);
void UpdateRasStateInWindow(HWND hwnd, RASCONNSTATE state);
void UpdateButton(HWND, RASCONNSTATE);
void HangUpNetwork();
HRASCONN g_hRasConn;
HINSTANCE g_hInst;
RASCONNSTATE g_state = RASCS_Disconnected;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow){
// TODO: Place code here.
static TCHAR szAppName[] = TEXT ("RasDemostration");
HWND hwnd;
MSG msg;
g_hInst = hInstance;
hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_RASDEMO_DIALOG), NULL, DialogProc);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case WM_INITDIALOG:
ShowWindow(hwnd,SW_SHOW);
break;
case WM_COMMAND:
DoCommand(hwnd, message, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage (0) ;
break;
case WM_RASDIALEVENT:
ParseRasDialEvent(hwnd, wParam, lParam);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void DoCommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch ( LOWORD (wParam) ){
case IDC_DIAL_RASNETWORK:
if (g_state == RASCS_Disconnected){
DialRasNetwork(hwnd);
}else{
HangUpNetwork();
}
break;
}
}
DWORD DialRasNetwork(HWND hwnd){
DWORD dwRet = 0;
GetRasBookEntry();
dwRet = RasDial(NULL, NULL,
GetRasDialParams(TEXT("无线互联网")),0xFFFFFFFF,hwnd,&g_hRasConn);
if (dwRet != ERROR_SUCCESS){
// Something wrong, handle error
dwRet = GetLastError();
}
return dwRet;
}
void GetRasBookEntry(){
DWORD dwRet = 0, cb = 0, cEntries = 0;
LPRASENTRYNAME lprasentryname = (LPRASENTRYNAME)LocalAlloc(LPTR, sizeof(RASENTRYNAME));
lprasentryname->dwSize = sizeof(RASENTRYNAME);
if ( (RasEnumEntries(NULL, NULL, lprasentryname, &cb, &cEntries)) == ERROR_BUFFER_TOO_SMALL){
lprasentryname = (LPRASENTRYNAME)LocalAlloc(LPTR, cb);
lprasentryname->dwSize = sizeof(RASENTRYNAME);
}
dwRet = RasEnumEntries(NULL, NULL, lprasentryname, &cb, &cEntries);
if (dwRet != ERROR_SUCCESS){
printf("RasEnumEntries failed: Error %d\n", dwRet);
}
for(DWORD i=0; i < cEntries; i++)
{
// Here you can get the name of a specific entry.
printf("%s\n",lprasentryname->szEntryName);
lprasentryname++;
}
}
LPRASDIALPARAMS GetRasDialParams(TCHAR* name){
BOOL passwordReturned = TRUE;
LPRASDIALPARAMS pParam = (LPRASDIALPARAMS)LocalAlloc(LPTR,sizeof(RASDIALPARAMS));
pParam->dwSize = sizeof(RASDIALPARAMS);
wcscpy((TCHAR*)pParam->szEntryName, name);
DWORD dwRet = RasGetEntryDialParams(NULL,pParam,&passwordReturned);
if (dwRet != ERROR_SUCCESS){
dwRet = GetLastError();
// Something wrong occured here, handle the error as you want.
return NULL;
}
return pParam;
}
void ParseRasDialEvent(HWND hwnd, WPARAM wParam, LPARAM lParam){
RASCONNSTATE rasState = (RASCONNSTATE)wParam;
g_state = rasState;
DEBUGMSG(1,(TEXT("Ras Event %d\r\n"),rasState));
UpdateButton(hwnd, g_state);
DWORD dwErr = (DWORD)lParam;
if (dwErr != 0){
DEBUGMSG(1,(TEXT("Error code is %d\r\n"),lParam));
UpdateRasStateInWindow(hwnd, (RASCONNSTATE)26);
}
UpdateRasStateInWindow(hwnd, rasState);
}
void UpdateRasStateInWindow(HWND hwnd, RASCONNSTATE state){
TCHAR* szRasState = CRasStateMessage::GetRasStateString(g_hInst, state);
if (szRasState == NULL){
// no define string or event is not valid.
return;
}
HWND wndStateStr = GetDlgItem(hwnd,IDC_RAS_STATUS);
if (wndStateStr == NULL){
// This should not happen.
return;
}
SetWindowText(wndStateStr,szRasState);
LocalFree(szRasState);
}
void UpdateButton(HWND hwnd, RASCONNSTATE state){
TCHAR szBtnCaption[MAX_PATH] = {0};
HWND hwdBtn = GetDlgItem(hwnd,IDC_DIAL_RASNETWORK);
if (state == RASCS_Disconnected){
// TODO: Show Dial
LoadString(g_hInst,IDS_STR_DIAL,szBtnCaption,MAX_PATH*sizeof(TCHAR));
}else if (state == RASCS_Connected){
// TODO: Show Hang up.
LoadString(g_hInst,IDS_STR_HANGUP,szBtnCaption,MAX_PATH*sizeof(TCHAR));
}else{
// Disable button.
}
SetWindowText(hwdBtn,szBtnCaption);
}
void HangUpNetwork(){
RasHangUp(g_hRasConn);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -