Solved

RAS problem

Posted on 2000-03-24
4
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run Program using VBScript 3 80
Slow process to read Excel 15 149
Saving the SQL Login Credentials for Excel 2010 Microsoft Queries 1 188
Licensing Sharepoint 2016 for developers 11 77
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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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