Solved

RAS problem

Posted on 2000-03-24
4
316 Views
Last Modified: 2013-12-03
I'm having a problem to establish a dial-up connection while using this code. The state goes 'opening port', 'port opened', 'connecting device', 'disconnecting', with error=630 'closing due to hardware failure'. When I just click on the connection, it connects normaly. The code is located in a dll, just in case it's relevant.

---------------------------
#include "general.h"
#include <ras.h>
CDataQueue NotifyQueue;
HRASCONN connection=0;
char szdunLastError[256];


//Executed in the main thread
void
dunNotifyAll(LONG notifycode)
{
      while(!NotifyQueue.IsEmpty())
      {
            LONG address=0;
            NotifyQueue.Get(&address, 4);
            CDUNNotify *nt=(CDUNNotify*)address;
            if(nt!=NULL) nt->NotifyDUN(notifycode);
      }
}

//Callback function for RASDial.
void
dunNotifyStateChanged(UINT uMsg, RASCONNSTATE state, DWORD dwError)
{
      
      //Store error string so it can be later retrieved by dunGetError()
      if(dwError)
      {
            RASCONNSTATUS status;
            status.dwSize = sizeof(status);
            RasGetConnectStatus(connection, &status);
            
            RasGetErrorString(dwError, szdunLastError, sizeof(szdunLastError));
            RasHangUp(connection);
            
            //Notify all that an error has occured. Extened error information can be retrieved with dunGetError()
            dunNotifyAll(ERR_GENERAL);
            return;
      }

      //If connection established succesfully
      if(state==RASCS_Connected)
      {
            dunNotifyAll(ERR_SUCCESS);
      }
}


//Asynchronically starts a connection
void
dunConnect(CDUNNotify *object)
{
      //Get dial-up entry value
      CString strDUN=iniGet("DUNEntry", "LAN");
      
      //If it's 'LAN', no need to connect, notify right away, and exit
      if(strDUN=="LAN")
      {
            object->NotifyDUN(ERR_SUCCESS);
            return;
      }
      
      //Add notification client
      LONG address = (LONG)object;
      NotifyQueue.Append(&address, 4);

      //If there are no ongoing dialup, this is the only request from this dll
      if(NotifyQueue.GetSize()==4)
      {
            //dial
            RASDIALPARAMS params;
            CLEAN(&params);
            params.dwSize = sizeof(RASDIALPARAMS);
            strcpy(params.szEntryName, strDUN);
            int nResult = RasDial(NULL, NULL, &params, 0, &dunNotifyStateChanged, &connection);
            
            //If error occured, callback won't be called, so call it right away
            if(nResult!=0) dunNotifyStateChanged(0, (RASCONNSTATE)0, nResult);
      }
}


//Disconnect
void
dunDisconnect()
{
      RasHangUp(connection);
}


//Terminates the connection in case it was established by the application
void
dunCleanUp()
{
      if(connection!=0) dunDisconnect();
}


void
dunGetError(char *buf)
{
      strcpy(buf, szdunLastError);      
}
0
Comment
Question by:MikeP090797
  • 2
  • 2
4 Comments
 
LVL 8

Author Comment

by:MikeP090797
ID: 2654793
Adjusted points from 300 to 500
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2655334
Callback functions must be defined as WINAPI:



void WINAPI dunNotifyStateChanged(...
0
 
LVL 15

Accepted Solution

by:
NickRepin earned 500 total points
ID: 2655450
You have to:
1) Declare the callback function as WINAPI:  

void WINAPI dunNotifyStateChanged(...

2) Probably, provide the user name/password.

In other respects, your code works fine.
The test program is below.

The problem can be also in NotifyQueue and dunNotifyAll().
Try to comment these lines out.


#include <windows.h>
#include <ras.h>
#include <iostream.h>
#include <conio.h>

//CDataQueue NotifyQueue;

HRASCONN connection=0;
char szdunLastError[256];


//Callback function for RASDial.
void WINAPI
dunNotifyStateChanged(UINT uMsg, RASCONNSTATE state, DWORD dwError)
{

   //Store error string so it can be later retrieved by dunGetError()
   if(dwError) {
      RASCONNSTATUS status;
      status.dwSize = sizeof(status);
      RasGetConnectStatus(connection, &status);

      RasGetErrorString(dwError, szdunLastError, sizeof(szdunLastError));
      RasHangUp(connection);
      cout<<"Callback: ERROR "<<dwError<<endl;
      return;
   }

   cout<<state<<endl;

   //If connection established succesfully
   if(state==RASCS_Connected)
   {
      cout<<"Callback: CONNECTED"<<endl;
   }

}


//Asynchronically starts a connection
void
dunConnect()
{

   RASDIALPARAMS params;
   memset(&params,0,sizeof(params));
   params.dwSize = sizeof(RASDIALPARAMS);
   strcpy(params.szEntryName,"User");
   strcpy(params.szUserName,"Password");
   strcpy(params.szPassword,"HaTf432");
   DWORD nResult = RasDial(NULL, NULL, &params, 0,
      &dunNotifyStateChanged, &connection);
   
   if(nResult) cout<<"Unable to dial"<<endl;
}


//Disconnect
void
dunDisconnect()
{
RasHangUp(connection);
}


void main(void) {
   dunConnect();
   _getch();
   dunDisconnect() ;
}


0
 
LVL 8

Author Comment

by:MikeP090797
ID: 2656425
Great!! It works now.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 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