Solved

RAS problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio Exe Issue, 12 27
Mixed results 10 79
mixing C++ & C# in Vis Studio 2013 7 139
T-SQL Debugging - Temp Object Content 2 52
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 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

12 Experts available now in Live!

Get 1:1 Help Now