Solved

Anyone have a C++ example of RasEnumConnections implementation?

Posted on 1998-09-29
4
1,230 Views
Last Modified: 2012-05-04
Anyone have a C++ example of RasEnumConnections implementation?  Also, how would you iterate through the returned connections to see if the connection you are trying to make is already connected?
0
Comment
Question by:afalvey
  • 2
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1174037
//
// Check the state of any RAS connection
//
char *CWindow::CheckRAS(DWORD *state, char *hostip)
{

      RASCONN *ras_buf;
      RASPPPIP *prasip;
      DWORD sz_ras_buf, numconn, errcd, szrasip;
      HRASCONN hrc;
      static char rasname[256];

      // Allocate space for RASCONN buffers
      ras_buf = (RASCONN *)malloc(32 * sizeof(RASCONN));
      sz_ras_buf = 32 * sizeof(RASCONN);

      ras_buf->dwSize = sizeof(RASCONN);

      if((errcd = RasEnumConnections(ras_buf, &sz_ras_buf, &numconn)) != 0){
          *state = RAS_ERROR;
          free(ras_buf);
            return NULL;
      }

      if(numconn > 0){
            hrc = ras_buf->hrasconn;

            prasip = (RASPPPIP *)malloc(sizeof(RASPPPIP));
            szrasip = sizeof(RASPPPIP);
            prasip->dwSize = szrasip;

            errcd = RasGetProjectionInfo(hrc, RASP_PppIp, prasip, &szrasip);
            switch(errcd){

                  default:
                      *state = RAS_ERROR;
                      free(ras_buf);
                      return NULL;
                  case 0:
                        *state = RAS_CONN;
                        strcpy(rasname, ras_buf->szEntryName);
                        strcpy(hostip, prasip->szIpAddress);
                        if(strlen(rasname) > 0){
                            free(ras_buf);
                            return(rasname);
                      }
                      else{
                          *state = RAS_ERROR;
                          free(ras_buf);
                          return NULL;
                      }

                  case 731:
                      *state = RAS_DIAL;
                      strcpy(rasname, ras_buf->szEntryName);
                        if(strlen(rasname) > 0){
                            free(ras_buf);
                            return(rasname);
                      }
                      else{
                          *state = RAS_ERROR;
                          free(ras_buf);
                          return NULL;
                      }
            }
      }
      else{
          *state = RAS_NOCONN;
          free(ras_buf);
          return NULL;
      }
}
0
 

Expert Comment

by:ext3awf
ID: 1174038
Do you have to include any headers other than:
ras.h and raserror.h?
0
 

Expert Comment

by:ext3awf
ID: 1174039
I've been trying this but I always get the default: state even when I'm connected.  Any suggestions?

LPRASENTRYNAME RasEntry;                        
                        DWORD Buffer;
                        DWORD Entry;
                        DWORD Retrn;
                        DWORD  Index;
                        
                        Buffer = sizeof(RASENTRYNAME);
                        
                        //RasEntry->dwSize = sizeof(RASENTRYNAME);

                        Retrn = RasENUMENTRIES( NULL, NULL, RasEntry, &Buffer, &Entry );                        
                        
                        //the buffer is too small
                        if (Retrn == 603)
                        {
                              RasEntry->dwSize = sizeof(RASENTRYNAME);
                              Retrn = RasENUMENTRIES( NULL, NULL, RasEntry, &Buffer, &Entry );                        
                        }

                        if ( Retrn != 0 )  //error
                        {
                              m_IAmConnected = FALSE;                                    
                              FireErrorLaunchingDialUpAccount(m_StringResource->LoadString(ERR_COMM_ERROR_LAUNCHING_DIAL_UP_ACCOUNT));                                                      
                        }

                        if ( Retrn == 0 )  // no errors
                        {
                              for ( Index = 0; Index < Entry; Index++ )
                              {                              
                                    if (rdParams.szEntryName == RasEntry[Index].szEntryName);
                                    {
                                          //get connection status for this entry
                                          RASCONNSTATUS ConnectionStatus;                        
                        
                                          ConnectionStatus.dwSize = sizeof(RASCONNSTATUS);

                                          RasCONNECTSTATUS(m_hRasConn,  // handle to RAS connection of interest
                                                                  &ConnectionStatus);      // buffer to receive status data
                                                                                                
                                          switch( ConnectionStatus.rasconnstate )
                                          {                                                
                                                case RASCS_Connected:
                                                      m_IAmConnected = TRUE;          
                                                
                                                //dial-up failed
                                                case RASCS_Disconnected:
                                                m_IAmConnected = FALSE;                                                      
                                                
                                                
                                                default:
                                                m_IAmConnected = FALSE;                                                                                                                              
                                          }                  
                                          
                                    }                              
                              }
                        }
0
 
LVL 32

Expert Comment

by:jhance
ID: 1174040
You have no "break;" in your "case:" statements!!!!!

switch( ConnectionStatus.rasconnstate )
{
case RASCS_Connected:
m_IAmConnected = TRUE;            
break;

//dial-up failed
case RASCS_Disconnected:
m_IAmConnected = FALSE;
break;

default:
m_IAmConnected = FALSE;
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now