Solved

RAS problem

Posted on 2000-03-24
4
310 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

707 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

19 Experts available now in Live!

Get 1:1 Help Now