📄 rasdial.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
rasdial.c
Abstract:
This file contains some sample code to use the rasdial api. It will
allow you to establish a PPP session to a remote computer. It will also
display the status of the current connections. It is a commandline program
so will need to be run under the debugger.
Functions:
Notes:
--*/
#include <windows.h>
#include <tchar.h>
#include "string.h"
#include "memory.h"
#include "raserror.h"
#include "ras.h"
#define DECLARE_OUTMSG
#include "outmsg.h"
#define NUM_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
HWND v_hWnd;
DWORD v_DebugFlag;
BOOL fExit;
BOOL fDisconnected;
typedef struct _RAS_STATE_STR {
DWORD dwRasState;
LPTSTR szState;
} RAS_STATE_STR, *PRAS_STATE_STRING;
RAS_STATE_STR RasStates[] = {
{RASCS_OpenPort, TEXT("OpenPort")},
{RASCS_PortOpened, TEXT("PortOpened")},
{RASCS_ConnectDevice, TEXT("ConnectDevice")},
{RASCS_DeviceConnected, TEXT("DeviceConnected")},
{RASCS_AllDevicesConnected, TEXT("AllDevicesConnected")},
{RASCS_Authenticate, TEXT("Authenticate")},
{RASCS_AuthNotify, TEXT("AuthNotify")},
{RASCS_AuthRetry, TEXT("AuthRetry")},
{RASCS_AuthCallback, TEXT("AuthCallback")},
{RASCS_AuthChangePassword, TEXT("AuthChangePassword")},
{RASCS_AuthProject, TEXT("AuthProject")},
{RASCS_AuthLinkSpeed, TEXT("AuthLinkSpeed")},
{RASCS_AuthAck, TEXT("AuthAck")},
{RASCS_ReAuthenticate, TEXT("ReAuthenticate")},
{RASCS_Authenticated, TEXT("Authenticated")},
{RASCS_PrepareForCallback, TEXT("PrepareForCallback")},
{RASCS_WaitForModemReset, TEXT("WaitForModemReset")},
{RASCS_WaitForCallback, TEXT("WaitForCallback")},
{RASCS_Projected, TEXT("Projected")},
{RASCS_Interactive, TEXT("Interactive")},
{RASCS_RetryAuthentication, TEXT("RetryAuthentication")},
{RASCS_CallbackSetByCaller, TEXT("CallbackSetByCaller")},
{RASCS_PasswordExpired, TEXT("PasswordExpired")},
{RASCS_Connected, TEXT("Connected")},
{RASCS_Disconnected, TEXT("Disconnected")},
{(DWORD)-1, TEXT("Unknown RAS Connection State")}
};
////////////////////////////////////////////////////////
//Embo
/***********************************************************************
PortInitialize (LPTSTR lpszPortName)
***********************************************************************/
HANDLE hPort = INVALID_HANDLE_VALUE;
BOOL PortInitialize (LPTSTR lpszPortName)
{
//DWORD dwError;
//dwThreadID;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
DWORD dwError, dwNumBytesWritten;
BOOL fRet;
BYTE Bytes[]={'A','T','D','T','*','9','9','#',0x0D};
DWORD BytesSize=sizeof(Bytes);
BOOL fCloseH=FALSE;
if(hPort != INVALID_HANDLE_VALUE)
{
BOOL fCloseH=FALSE;
do
{
fCloseH=CloseHandle(hPort);
Sleep(10);
}
while(fCloseH==TRUE);
hPort = INVALID_HANDLE_VALUE;
}
// Open the serial port.
hPort = CreateFile (lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// If it fails to open the port, return FALSE.
if ( hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
//MessageBox (NULL, TEXT("Unable to open the port"),
// TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hPort, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = 115200; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
// PortDCB.fDtrControl = DTR_CONTROL_DISABLE; //DTR_CONTROL_ENABLE;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
// PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hPort, &PortDCB))
{
// Could not create the read thread.
//MessageBox (NULL, TEXT("Unable to configure the serial port"),
// TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 10;//0;//MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 10;//0;
CommTimeouts.ReadTotalTimeoutConstant = 10;//00;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;//0;
CommTimeouts.WriteTotalTimeoutConstant = 100;//0;
// Set the time-out parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
// Could not create the read thread.
//MessageBox (NULL, TEXT("Unable to set the time-out parameters"),
// TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
// Direct the port to perform extended functions SETDTR and SETRTS
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
// EscapeCommFunction (hPort, SETDTR);
// EscapeCommFunction (hPort, SETRTS);
PurgeComm(hPort, PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT );
SetCommMask (hPort, EV_RXCHAR| EV_CTS | EV_DSR | EV_RLSD | EV_RING);//EV_DEFAULT);//EV_RXCHAR );//
// Create a read thread for reading data from the communication port.
/* if (hReadThread = CreateThread (NULL, 0, PortReadThread, 0, 0,
&dwThreadID))
{
CloseHandle (hReadThread);
}
else
{
// Could not create the read thread.
MessageBox (NULL, TEXT("Unable to create the read thread"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
*/
fRet=WriteFile (hPort, // Port handle
Bytes, // Pointer to the data to write
BytesSize, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes written
NULL); // Must be NULL for Windows CE
Sleep(200);
do
{
fCloseH=CloseHandle(hPort);
}
while(fCloseH==TRUE);
return TRUE;
}
////////////////////////////////////////////////////////
LPTSTR GetStateStr(DWORD dwState)
{
DWORD i;
for (i=0; RasStates[i].dwRasState != (DWORD)-1; i++) {
if (RasStates[i].dwRasState == dwState) {
break;
}
}
return RasStates[i].szState;
}
LRESULT CALLBACK
WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
RASCONNSTATE RasState = (RASCONNSTATE)wParam;
switch (Msg) {
case WM_RASDIALEVENT :
OutputMessage (TEXT("RasDial: "));
switch (RasState) {
case RASCS_AuthNotify :
OutputMessage (TEXT("%s: Ecode=%d"), GetStateStr(RasState), lParam);
switch (lParam) {
case ERROR_RESTRICTED_LOGON_HOURS:
OutputMessage (TEXT("\tAUTH_ERR_REST_HOUR"));
break;
case ERROR_ACCT_DISABLED:
OutputMessage (TEXT("\tAUTH_ERR_ACCT_DISABLED"));
break;
case ERROR_PASSWD_EXPIRED:
OutputMessage (TEXT("\tAUTH_ERR_PWD_EXP"));
break;
case ERROR_NO_DIALIN_PERMISSION:
OutputMessage (TEXT("\tAUTH_ERR_NO_DIALIN"));
break;
case ERROR_CHANGING_PASSWORD:
OutputMessage (TEXT("\tAUTH_ERR_CHG_PWD"));
break;
default :
OutputMessage (TEXT("\tAUTH_ERR_UNKNOWN"));
break;
}
break;
case RASCS_Connected :
OutputMessage (TEXT("Connected"));
fExit = TRUE;
DestroyWindow (hWnd);
break;
case RASCS_Disconnected :
OutputMessage (TEXT("Disconnected: Ecode %d"), lParam);
fExit = TRUE;
fDisconnected = TRUE;
DestroyWindow (hWnd);
break;
default :
OutputMessage (GetStateStr(RasState));
break;
}
OutputMessage (TEXT("\r\n"));
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -