⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rasconn.cpp

📁 WINDOWS CE 通信指南(附光盘) 本书配套光盘内容包括:本书的英文版电子书;SDK for Windows CE
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************

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.

Copyright(c)  1999  Microsoft Corporation.  All Rights Reserved.

MODULE: 
  RasConn.c

ABSTRACT: 
  This code sample shows how to enumerate, create, delete, copy, and 
  rename phone book entries, and establish and close a Remote Access 
  Service (RAS) connection.

***********************************************************************/

#include <windows.h>
#include <commctrl.h>
#include <ras.h>
#include <raserror.h>
#include "resource.h"

HWND hMainWnd;                        // Main window handle
HWND hCmdBarWnd;                      // Command bar window handle
HWND hDialingWnd;                     // Dialing window handle
HMENU hMainMenu;                      // Main menu handle
HINSTANCE hInst;                      // hInstance of the application
TCHAR szTitle[] = TEXT("RasConn");    // Main window name
TCHAR szAppName[] = TEXT("RasConn Sample Application");
                                      // Main window class name
TCHAR szDomain[100];                  // Domain name
TCHAR szUserName[100];                // User name
TCHAR szPassword[100];                // Password
BOOL bUseCurrent = FALSE;             // Indicate if the current user 
                                      // name and password are used
TCHAR szRasEntryName[RAS_MaxEntryName + 1];                
                                      // Current RAS entry name
HRASCONN hRasConn = NULL;             // RAS connection handle
RASDIALPARAMS RasDialParams;          // Parameters used by RasDial 
LPRASENTRYNAME lpRasEntryName = NULL; // Contains all entry names in a 
                                      // remote access phone book 
BOOL GetPhonebookEntries (HWND);
BOOL MakeRasDial (HWND);
BOOL CALLBACK PhoneBookDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AuthDlgProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialingDlgProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

/***********************************************************************

FUNCTION:
  RenameRasEntry

PURPOSE:
  This subroutine changes the name of an entry in the RAS phone book. 
  It first calls RasValidateEntryName to validate the new name and then 
  calls RasRenameEntry to change the name. The return value is TRUE if
  successful, and FALSE if not.

***********************************************************************/
BOOL RenameRasEntry (LPTSTR lpszOldName, LPTSTR lpszNewName)
{
  DWORD dwError;            // Returned code from functions 
  TCHAR szError[120];       // Buffer for error message 

  if (dwError = RasValidateEntryName (NULL, lpszNewName))
  {
    wsprintf (szError, TEXT("Entry name validation failed: %ld"), 
              dwError);
    return FALSE;
  }

  if (dwError = RasRenameEntry (NULL, lpszOldName, lpszNewName))
  {
    wsprintf (szError, TEXT("Unable to rename entry: %ld"), dwError);
    return FALSE;
  }

  return TRUE;
}


/***********************************************************************

FUNCTION:
  DeleteRasEntry

PURPOSE:
  This subroutine deletes an entry from the RAS phone book. The return 
  value is TRUE if successful, and FALSE if not.

***********************************************************************/
BOOL DeleteRasEntry (LPTSTR lpszName)
{
  DWORD dwError;            // Return code from function RasDeleteEntry
  TCHAR szError[100];       // Buffer for error message 

  if (dwError = RasDeleteEntry (NULL, lpszName))
  {
    wsprintf (szError, TEXT("Unable to delete entry: %ld"), dwError);
    return FALSE;
  }

  return TRUE;
}


/***********************************************************************

FUNCTION:
  CreateRasEntry

PURPOSE:
  This function shows how to create a RAS entry.

***********************************************************************/
int CreateRasEntry (LPTSTR lpszName)
{
  DWORD dwSize,
        dwError;
  TCHAR szError[100];
  RASENTRY RasEntry;
  RASDIALPARAMS  RasDialParams;

  // Validate the format of a connection entry name.
  if (dwError = RasValidateEntryName (NULL, lpszName))
  {
    wsprintf (szError, TEXT("Unable to validate entry name.")
              TEXT(" Error %ld"), dwError);  

    return FALSE;
  }

  // Initialize the RASENTRY structure.
  memset (&RasEntry, 0, sizeof (RASENTRY));

  dwSize = sizeof (RASENTRY);
  RasEntry.dwSize = dwSize;

  // Retrieve the entry properties.
  if (dwError = RasGetEntryProperties (NULL, TEXT(""),      
                      (LPBYTE)&RasEntry, &dwSize, NULL, NULL))  
  {
    wsprintf (szError, TEXT("Unable to read default entry properties.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  // Insert code here to fill up the RASENTRY structure.
  // ...
  
  // Create a new phone book entry.
  if (dwError = RasSetEntryProperties (NULL, lpszName, 
                      (LPBYTE)&RasEntry, sizeof (RASENTRY), NULL, 0))
  {
    wsprintf (szError, TEXT("Unable to create the phonebook entry.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  // Initialize the RASDIALPARAMS structure.
  memset (&RasDialParams, 0, sizeof (RASDIALPARAMS));
  RasDialParams.dwSize = sizeof (RASDIALPARAMS);
  _tcscpy (RasDialParams.szEntryName, lpszName);
  
  // Insert code here to fill up the RASDIALPARAMS structure.
  // ...

  // Change the connection information.
  if (dwError = RasSetEntryDialParams (NULL, &RasDialParams, FALSE))
  {
    wsprintf (szError, TEXT("Unable to set the connection information.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  return TRUE;
}

/***********************************************************************

FUNCTION:
  CopyRasEntry

PURPOSE:
  This subroutine copies the RAS entry named lpszEntryName into a new
  entry named lpszEntryName1. Functions RasGetEntryProperties and
  RasSetEntryProperties are called to retrieve and save most entry
  information, while RasGetEntryDialParams and RasSetEntryDialParams 
  are called to handle user name and password information. The return 
  value is TRUE if successful, and FALSE if not.

***********************************************************************/
BOOL CopyRasEntry (LPTSTR lpszEntryName)
{
  BOOL bPasswordSaved;              
  TCHAR szNewEntryName[100];      // Name for the copied entry
  TCHAR szError[100];             // Buffer for the error message
  DWORD dwError,                  // Returned code from the functions
        dwRasEntrySize,           // Size of the RASENTRY structure
        dwDevConfigSize,          // Size of DevConfigBuf
        dwDeviceNum = 0xFFFFFFFF; // TAPI device number 
  BYTE DevConfigBuf[128];         // Buffer for device configuration 
                                  // information
  RASENTRY RasEntry;              // RASENTRY structure
  RASDIALPARAMS RasDialParams;    // RASDIALPARAMS structure 
  LPVARSTRING lpDevConfig = (LPVARSTRING)&DevConfigBuf;
                                  // Pointer to the memory location of
                                  // the device configuration structure

  // Assign the name for the copied phone book entry.
  wsprintf (szNewEntryName, TEXT("%s1"), lpszEntryName);
    
  // Validate the format of a connection entry name.
  if (dwError = RasValidateEntryName (NULL, szNewEntryName))
  {
    wsprintf (szError, TEXT("Unable to validate entry name.")
              TEXT(" Error %ld"), dwError);  

    return FALSE;
  }

  dwDevConfigSize = sizeof (DevConfigBuf);
  dwRasEntrySize = sizeof (RASENTRY);
  RasEntry.dwSize = dwRasEntrySize;

  // Retrieve the entry properties.
  if (dwError = RasGetEntryProperties (NULL,               
                                       lpszEntryName,      
                                       (LPBYTE)&RasEntry,  
                                       &dwRasEntrySize,    
                                       DevConfigBuf,       
                                       &dwDevConfigSize))  
  {
    wsprintf (szError, TEXT("Unable to read entry properties.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  memset (&RasDialParams, 0, sizeof (RasDialParams));
  RasDialParams.dwSize = sizeof (RASDIALPARAMS);
  _tcscpy (RasDialParams.szEntryName, lpszEntryName);

  // Retrieve the connection information.
  if (dwError = RasGetEntryDialParams (NULL, &RasDialParams, 
                      &bPasswordSaved))
  {
    wsprintf (szError, TEXT("Unable to get the connection information.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  // Create a new phone book entry.
  if (dwError = RasSetEntryProperties (NULL, 
                                       szNewEntryName, 
                                       (LPBYTE)&RasEntry,
                                       dwRasEntrySize, 
                                       DevConfigBuf, 
                                       dwDevConfigSize))
  {
    wsprintf (szError, TEXT("Unable to copy the phonebook entry.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }
  
  _tcscpy (RasDialParams.szEntryName, szNewEntryName);

  // Change the connection information.
  if (dwError = RasSetEntryDialParams (NULL, &RasDialParams, FALSE))
  {
    wsprintf (szError, TEXT("Unable to set the connection information.")
              TEXT(" Error %ld"), dwError);
    return FALSE;
  }

  return TRUE;
}


/***********************************************************************

FUNCTION:
  CloseRasConnections

PURPOSE:
  Checks for the open RAS connections and attempts to close any that 
  are found. The return value is an error code (0 if no error).

***********************************************************************/
DWORD CloseRasConnections ()
{
  int index;                // Integer index
  TCHAR szError[100];       // Buffer for error codes 
  DWORD dwError,            // Error code from a function call 
        dwRasConnSize,      // Size of RasConn in bytes
        dwNumConnections;   // Number of connections found 
  RASCONN RasConn[20];      // Buffer for connection state information 
                            // Assume the maximum number of entry is 20

  // Assume there are no more than 20 connections.
  RasConn[0].dwSize = sizeof (RASCONN);
  dwRasConnSize = 20 * sizeof (RASCONN);

  // Find all of the connections.
  if (dwError = RasEnumConnections (RasConn, &dwRasConnSize, 
                                    &dwNumConnections))
  {
    wsprintf (szError, TEXT("RasEnumConnections Error: %ld"), dwError);
    return dwError;
  }

  // If there are no connections, return 0.
  if (!dwNumConnections)
  {
    wsprintf (szError, TEXT("No open RAS connections"));
    return 0;
  }

  // Terminate all of the remote access connections.
  for (index = 0; index < (int)dwNumConnections; ++index)
  {
    if (dwError = RasHangUp (RasConn[index].hrasconn))
    {
      wsprintf (szError, TEXT("RasHangUp Error: %ld"), dwError);
      return dwError;
    }
  }

  return 0;
}

/***********************************************************************

FUNCTION:
  GetPhonebookEntries

PURPOSE:
  Shows how to enumerate the phone book entries and add
  all entry names to a list control.

***********************************************************************/
BOOL GetPhonebookEntries (HWND hDlgWnd)
{
  int index;
  DWORD dwSize, 
        dwEntries;
  HWND hWndListBox;

  // Allocate an array of RASENTRYNAME structures. Assume that there
  // will be no more than 20 entries configured on the windows CE 
  // device.
  if (!(lpRasEntryName = new RASENTRYNAME [20]))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -