|
Introduction
There was a time I needed to detect an internet connection using the modem. I've checked the RAS library and found 2 useful functions: RasEnumConnections and RasGetConnectStatus.
The first function (RasEnumConnections) retrieve a specified amount of connections: DWORD RasEnumConnections (
LPRASCONN lprasconn,
LPDWORD lpcb,
LPDWORD lpcConnections
);
lprasconn - Points to a buffer that receives an array of RASCONN structures, one for each RAS connection. Before calling the function, an application must set the dwSize member of the first RASCONN structure in the buffer to sizeof(RASCONN) in order to identify the version of the structure being passed.
lpcb - Points to a variable that contains the size, in bytes, of the buffer specified by lprasconn. On return, the function sets this variable to the number of bytes required to enumerate the RAS connections.
lpcConnections - Points to a variable that the function sets to the number of RASCONN structures written to the buffer specified by lprasconn.
Return Value: 0 - Succeed; Other - Error code
The RASCONN is a structure which hold the information on a single connection: typedef struct _RASCONN {
DWORD dwSize;
HRASCONN hrasconn;
TCHAR szEntryName[RAS_MaxEntryName + 1];
#if (WINVER >= 0x400)
CHAR szDeviceType[RAS_MaxDeviceType + 1];
CHAR szDeviceName[RAS_MaxDeviceName + 1];
#endif
} RASCONN ;
dwSize - Specifies the size, in bytes, of the RASCONN structure.
hrasconn - Specifies the remote access connection. This handle is used in other remote access API calls.
szEntryName - A string that specifies the phone-book entry used to establish the remote access connection. If the connection was established using an empty entry name, this string consists of a "." followed by the connection phone number.
szDeviceType - A null-terminated string that contains the device type through which the connection is made.
szDeviceName - A null-terminated string that contains the device name through which the connection is made.
The second function (RasGetConnectStatus) retrieves information on the current status of the specified connection DWORD RasGetConnectStatus (
HRASCONN hrasconn,
LPRASCONNSTATUS lprasconnstatus
);
hrasconn - Identifies the remote access connection for which to retrieve the status. This handle must have been obtained from RasDial or RasEnumConnections.
lprasconnstatus - Points to a RASCONNSTATUS structure that the function fills with status information. Before calling the function, an application must set the dwSize member of the structure to sizeof(RASCONNSTATUS) in order to identify the version of the structure being passed.
Return Value: 0 - Succeed; Other - Error code
The RASCONNSTATUS structure describes the current status of a connection. typedef struct _RASCONNSTATUS {
DWORD dwSize;
RASCONNSTATE rasconnstate;
DWORD dwError;
TCHAR szDeviceType[RAS_MaxDeviceType + 1];
TCHAR szDeviceName[RAS_MaxDeviceName + 1];
} RASCONNSTATUS;
To use the RAS library, you need to include <ras.h> in your source files and add rasapi32.lib to link with your application.
I've distributed my application and then I had a problem. One day, I got a mail which describes an error. It turns out that my client's computer missing rasapi32.dll. Only when you install the "Dial-out networking" you get rasapi32.dll. So instead linking rasapi32.lib to my application, I've decided to load it at runtime so I can detect if it is installed instead of getting an error message. What I had to remember is that rasapi32.dll contains 2 kinds of functions: ANSI and UNICODE, so the code need to know which one to use. I check the _UNICODE constant definition. Then, I load the library, look for the functions address and call them to get my connection status.
All you need to do to use the code, is to include "rasstatus.h" in your project. I also made a demo project but it is very simple and it only used to check that the code really works.
The code is not so hard to understand and I added some comments so you can understand easily.
#ifndef _RASSTATUS
#define _RASSTATUS
#include <ras.h>
typedef DWORD (WINAPI *RasEnumConnectionsType)(LPRASCONN lprasconn,
LPDWORD lpcb,LPDWORD lpcConnections);
typedef DWORD (WINAPI *RasGetConnectStatusType)(HRASCONN hrasconn,
LPRASCONNSTATUS lprasconnstatus);
#ifdef _UNICODE
#define RasFileName L"RASAPI32.DLL"
#define RasEnumConnectionsName L"RasEnumConnectionsW"
#define RasGetConnectStatusName L"RasGetConnectStatusW"
#else
#define RasFileName "RASAPI32.DLL"
#define RasEnumConnectionsName "RasEnumConnectionsA"
#define RasGetConnectStatusName "RasGetConnectStatusA"
#endif
BOOL IsRasConnected()
{
HINSTANCE hLib = LoadLibrary(RasFileName);
if (hLib == NULL)
return FALSE;
RasEnumConnectionsType _RasEnumConnections =
(RasEnumConnectionsType)GetProcAddress(hLib,RasEnumConnectionsName);
RasGetConnectStatusType _RasGetConnectStatus =
(RasGetConnectStatusType)GetProcAddress(hLib,RasGetConnectStatusName);
BOOL bResult = FALSE;
if (_RasEnumConnections && _RasGetConnectStatus)
{
RASCONN RasConn;
RASCONNSTATUS RasConnStatus;
RasConnStatus.dwSize = sizeof(RASCONNSTATUS);
DWORD dwConnSize = sizeof(RASCONN);
DWORD dwRasCount = 1;
RasConn.dwSize = dwConnSize;
bResult =
(((*_RasEnumConnections)(&RasConn,&dwConnSize,&dwRasCount)) == 0)
&& (((*_RasGetConnectStatus)(RasConn.hrasconn,&RasConnStatus)) == 0)
&& (RasConnStatus.rasconnstate == RASCS_Connected);
}
FreeLibrary(hLib);
return bResult;
}
#endif
All you need to do is to call IsRasConnected which returns TRUE is your are connected to the internet or FALSE otherwise. Hope it could help :) If you have any questions or comments, you can contact me: Gilad Novik - gilad@bmidas.com
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh) | FirstPrevNext |
|
 |
|
|
Hi people, I have been trying to use rasapi32.dll file on a simple win32 console (c/c++) program...
I include ras.h file and try to use it..
but during final run a link time error says that the file is corrupt ? anyidea...? I am using windows xp, and I dont want to use getProcAddress as it makes the internal c compiler (cl.exe ) crash...
im using winxp and vs2k5..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi,my friend,
I met a question,and i can't find the answer!
Can U give me some instructions?Thanks,first!
My source code:
void Test() { DWORD re; RASCONN m_RasConn[10]; HRASCONN m_hRasConn; DWORD ConBufSize; m_RasConn[0].dwSize = sizeof(RASCONN); ConBufSize = 10 * m_RasConn[0].dwSize; ConBufSize = 10* m_RasConn[0].dwSize; re = RasEnumConnections(&m_RasConn[0];&ConBufSize;&m_nRasConnCount); //if re not equal to 0,the something wrong); if(re != 0) { RasGetErrorString(re,buf,127);//Interpret the wrong return value } }
Now,I use the PC and Modem to successed dial.So,in my opinion,in this case,the re above should't be nonzero.But,regretedly,the re's value is 632,which standard for "An incorrect structure size was detected".
so,wy?
I cann't understand!
Can you give me some instructions?Thanks!
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Is it possible to get modem connection speed of active connection using RAS or some other library, etc?
"A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible." - Freeman Dyson
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Unluckily this way of checking a connection fails in some circumstances, especially when using a wireless connection modem like for GSM/GPRS.
When a connection has first been established succesfully, and then network coverage is lost (like often happens for GSM/GPRS), the RAS status as retrieved with RasGetConnectStatus is still RASCS_Connected for a while, while the modem has already dropped the connection. There seems to be a delay in the RAS status in Windows being updated.
I am still looking for a way to solve this. Usually I can do with an ICMP echo, or 'get host by name' DNS query, but I also have to deal with networks / domains where these are blocked / not present.
Anybody any comments and / or ideas?
Thanks
Ruud
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I ran the exe on win2k and got "offline"...... when i was Online. ???
Does it work for only certain OS or Circumstances?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
how can i get the value of all vpn configurations in my computer? i want to get the login name and password,and then connect directly to the server.thanks for taking a look at this thread.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
I assume that all login information is encrypted in registry. I'm not sure you can extract it via API, but I'v never tried it.
Whoa! The internet is even on computers now!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
how about rasphone.exe?i try to use it in WinMe and Win95,but it didn't work.it can be run in win2000,NT or winXP...what is the command in Win95 or Winme?i have checked that win95,98,WinMe support vpn. Even if it be supported,i still don't know how to setup it just like use rasphone.exe in win2000. my english is not good enough.thanks for taking a look at this article.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I am using RasEnumConnections function of SDK to get list of active DUP connections. Its working fine under WINXP but under WIN2K it gives error code 632 or 0x278 which is ERROR_INVALID_EA_HANDLE. Can any one tell me whats wrong with that. Same application is running on both OS, and according to microsoft both OS are supported.
|
| Sign In·View Thread·PermaLink | 3.33/5 (3 votes) |
|
|
|
 |
|
|
I am trying to compile this code with Borland C++ Builder 5.5 But things don't work out.
I get a 610 return code when I call RasEnumConnections, instead of 0, It seems that 610 means Procedure not found !!
Although I have rasapi32.dll on the PC, which is win98, ras.h exist with the libraries, also rasapi32.lib, Dialup networking is installed on the PC, and I am connecting from it now!
I do not know why is this error, is it something related to the compiler ? I compile the same code that exist here without any changes. I run bcc32 -tW rasconnection.cpp
Also the release exe with the code works fine on the PC, by the executable I get after compile always say not connected because it gets 610 from RasEnumConnections return code instead of 0.
Sherif
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Don't link against the library file. Try to load rasapi32.dll at runtime and call the functions using GetProcAddress. Also, try to use DependencyViewer to examine rasapi32.dll.
Whoa! The internet is even on computers now!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It works perfectly under VC6 ... But it doesn't works on VC7 ( It always returns that there's no connection active ) . It's strange 'cause there's no warning or something like that during compilation.
Anyone knows how can i use it on VC7 ?? 
thanks a lot!
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I can't receive the handlers for the incoming calls (so I can't disconnect them). The outgoing calls I can see. Does anyone have a clue ??? Thanks advanced.
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
I have a need to detect whether the machine has Internet connection or not. For that I can use Microsofts (unreliable) InternetHasConnection and such methods or ping.
But when my machine has multiple ways to connect to the Internet (or any IP network) (say VLAN and GSM modem,...), how do I detect which connection is in use?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
You can use the API call "IsNetworkAlive(LPDWORD lpdwFlags)".
Documentation on it is in: http://msdn.microsoft.com/library/default.asp?url=/library/ en-us/syncmgr/syncmgr/isnetworkalive.asp
(Sorry for link wrapping)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have a network client that needs to know whether or not it is connected to the net. This code fragment is excellent for determining a connection on a RAS machine. However, I'm not sure of the right way to detect a connection if I am on a LAN.
I can't just ping because that will cause a dial-up attempt on a RAS client. Any suggestions are appreciated.
Thanks
Drew Berkemeyer
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Well, after much research I have answered my own question. I'll post my findings here so that maybe someone else will benefit from it.
Basically, you have a few choices. You can use some of the Microsoft Inet calls which include InternetGetConnectedState() and InternetCheckConnection() or you can create a RAW socket and try and ping one of the Internet root servers. Each of these methods will allow you to test for an Internet connection (LAN or Dial-up) without opening the Dial-up networking dialog.
Here are some useful links:
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Microsoft's Inet functions are not reliable. When your server is down, it reports that you are connected and your application my hang. It also has some other bugs. You better use ping
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
If you go to another project in Code Project called CPing v1.22 you will see a thread at the bottom that discusses problems with using RAW sockets in a non-administrative account.
The KB article on Microsofts site states that:
"In Windows 2000, there is no way to disable this security check. Access to Raw Sockets is granted on a per-transport basis. For the address family AF_INET, only administrators have the access necessary to create Raw Sockets."
This is the reason I was avoiding ping. Can you give me some advice on how to avoid the problem outlined in the thread at the bottom of that article so that my application will not fail for users that have non-administrative accounts?
Thank you in advance.
Drew Berkemeyer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
When you try to connect to a server, and your network is down, your application hangs until the timeout occurs (which can take forever)
I used a technique to set the timeout.
Create a thread called Thread1 which should try to connect the server.
DWORD dwTimeout = 10000; (10 seconds) if (WaitForSingleObject(hThread1,dwTimeout)==WAIT_TIMEOUT) { // Close the connection, network is not available } else { // Proceed, we have a connection
}
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
A further issue to consider is that if the user isn't connected, and they use a dialup connection, this will launch their "connect as" dialup dialog. If you want to check if the user is connected to the internet silently, you'd better find a different way 
You could modify the following key before making the call :
HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\EnableAutodial
Set it to FALSE before making the call, and then set it back to it's original setting afterwards. This prevents the "connect as" dialog appearing. But you are still left with the timeout problem, and even using WaitForSingleObject for a set number of seconds isn't an ideal method. I want to just ask the os "am i connected", and it return "yes i am", or "no i'm not", in quick time, with no hangups, and no misreporting of connected state.
Also the RAS functions do not work with Cable DSL, as provided by Blue Yonder and NTL in the UK ; i imagine a similar situation is likely with US providers. It says you are not connected, when you are.
I have yet to find the perfect solution for what should really be the simplest of operations. A big shiny gold star goes to the developer who can post some code, or a class which is foolproof ...
Jase
------------------------------------------------------------------------------------------------------------------------------------------------------------------- View your digital photos and images with ease using the ultimate desktop image manager for Microsoft Windows Download your free copy of SlideShow Desktop today from http://www.slideshowdesktop.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Just a note - you need to zero-fill the RASCONN struct before calling the RasXXX() functions. I was helping a coworker with a RAS bug yesterday, and it turns out that on Win 98, the APIs will fail, even if you set the size member correctly. On Win 2K (and even Win 98, with an updated DUN installed) the code worked fine.
--Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall." -- Lt. Cmdr. Worf
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I use Win98 based machine and I never had that problem. Anyway, it is not so hard to add a "memset" call just before the RAS API calls.
Thanks
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|