Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29241 Discussions

Checking internet connection from fortran

acar
Beginner
2,183 Views
Can anyone point me to a reliable method of getting the internet status of the machine that my application is running on. I've tried such MS functions as InternetGetConnectedStatus but it doesn't appear to exist anymore.
Thanks, ACAR.
0 Kudos
9 Replies
anthonyrichards
New Contributor III
2,183 Views
The function you want is in WININET.DLL (in c:\windows\system32\). The following code works for me as a console project when compiled and linked using multithreaded dll libraries:
(p.s. You may need to add WININET.LIB to the link library list for the project)

program Testinternetconnection
use dfwin
use dflib

implicit none
interface
Integer function InternetGetConnectedState(result, dummy)
!DEC$ ATTRIBUTES C, ALIAS :'_InternetGetConnectedState@8'::InternetGetConnectedState
!DEC$ ATTRIBUTES REFERENCE:: RESULT
!DEC$ ATTRIBUTES VALUE:: DUMMY
integer dummy
integer result
end function
end interface

INTEGER, PARAMETER :: INTERNET_CONNECTION_CONFIGURED = Z'40'
INTEGER, PARAMETER :: INTERNET_CONNECTION_LAN = Z'2'
INTEGER, PARAMETER :: INTERNET_CONNECTION_MODEM = Z'1'
INTEGER, PARAMETER :: INTERNET_CONNECTION_OFFLINE = Z'20'
INTEGER, PARAMETER :: INTERNET_CONNECTION_PROXY = Z'4'
INTEGER, PARAMETER :: INTERNET_RAS_INSTALLED = Z'10'

integer iret, lret
character(3) chiret
integer result

iret=InternetGetConnectedState(result, 0)
!
write(chiret,'(i3)') iret
lret=MessageBox(0,'iret = '//chiret//char(0),'Internetconnection status'c,MB_OK)

If ((Result.AND.INTERNET_CONNECTION_CONFIGURED) .EQ. INTERNET_CONNECTION_CONFIGURED) Then
lret=MessageBox(0,"Local system has a valid connection to the Internet,/r but it may or may not be currently connected."c, & 'Internet Connection Status'c, mb_OK)
endif
If ((Result.AND.INTERNET_CONNECTION_LAN) .EQ. INTERNET_CONNECTION_LAN) Then
lret=MessageBox(0,"Local system uses a local area network to connect to the Internet."c, &
'Internet Connection Status'c, mb_OK)
endif
If ((Result.AND.INTERNET_CONNECTION_MODEM) .EQ. INTERNET_CONNECTION_MODEM) Then
lret=MessageBox(0,"Local system uses a modem to connect to the Internet."c, &
'Internet Connection Status'c, mb_OK)
endif
If ((Result.AND.INTERNET_CONNECTION_OFFLINE) .EQ. INTERNET_CONNECTION_OFFLINE) Then
lret=MessageBox(0,"Local system is in offline mode."c, &
'Internet Connection Status'c, mb_OK)
endif
If ((Result.AND.INTERNET_CONNECTION_PROXY) .EQ. INTERNET_CONNECTION_PROXY) Then
lret=MessageBox(0,"Local system uses a proxy server to connect to the Internet."c, &
'Internet Connection Status'c, mb_OK)
endif
If ((Result.AND.INTERNET_RAS_INSTALLED) .EQ. INTERNET_RAS_INSTALLED) Then
lret=MessageBox(0,"Local system has RAS installed."c, &
'Internet Connection Status'c, mb_OK)
endif

end program Testinternetconnection

(P.S. Later bug-tracing shows that the above has the wrong calling convention: Please replace
!DEC$ ATTRIBUTES C, ALIAS :'_InternetGetConnectedState@8'::InternetGetConnectedState
with
!DEC$ ATTRIBUTES STDCALL, ALIAS :'_InternetGetConnectedState@8'::InternetGetConnectedState
)

0 Kudos
acar
Beginner
2,183 Views
Most grateful for your response Anthony and apologies if I'm being a bit dim but I still can't get the call to InternetGetConnectedState to work - I copied your code verbatim and I get a linker error LNK2019: unresolved external on the function _InternetGetConnectedState@8. WININET.dll exists on my system in the location you suggested so I wonder what the issue is here? I note that if I do a search for this function in my version of VS2008 is also does not find it. Do I somehow need to register the dll in my application (or your console app)? Thanks, ACAR.
0 Kudos
Les_Neilson
Valued Contributor II
2,183 Views
Did you also add the WINNET.LIB (note the extension!) to the libraries in the link step? as Anthony suggested (in his PS)

Les
0 Kudos
anthonyrichards
New Contributor III
2,183 Views
Les is correct. My original posted project was created using CVF. An identical project using the identical code created using IVF and VS2005 Pro showed that WININET.LIB had to be added under project...properties...Linker...Input...additional dependencies, as the attached screen shot shows. This needs to be done for both Debug and Release configurations.

The IVF solution showED one bug, in that the ' iret =' part of the first messageBox text string is screwed up (see attachment) although subsequent messageboxes show correct text(see attachment). If I put the concatenated text into a seperate character string and include that into the message box instead, I get an access violation. Wierd! I will investigate this further.

Well I investigated further and I think it is a stack problem caused by the wrong calling convention.
Please replace
!DEC$ ATTRIBUTES C, ALIAS :'_InternetGetConnectedState@8'::InternetGetConnectedState
with
!DEC$ ATTRIBUTES STDCALL, ALIAS :'_InternetGetConnectedState@8'::InternetGetConnectedState


0 Kudos
acar
Beginner
2,183 Views
Okay, thanks Les and Anthony. I've now made the change to the !DEC$ ATTRIBUTES and added the static library to the link process and it runs fine for me - I don't get the screw-up you see with the first dialog it returning 1 or 0 depending on whether or not I'm connected. Out of interest why are we not able to make use of the wininet.dll?
0 Kudos
anthonyrichards
New Contributor III
2,183 Views
If you right-click on the executable and select 'View Dependencies', Dependency Walker should open (always supposing you have it installed) and it will show you that WININET.DLL is indeed required.

I have no idea how my VS 2005 knows where the export library WININET.LIB associated with the DLL is located on my system and therefore have no idea how VS automatically finds it when it is listed as an Additional dependency. Perhaps Steve can oblige?. On my system, I have found that WININET.LIB is located at a couple of places, both in a PlatformSDK /Lib/ folder.
0 Kudos
Steven_L_Intel1
Employee
2,183 Views
WININET.LIB is part of the Platform SDK (now Windows SDK) and the path to this is part of the default set of library folders. However, I keep encountering customers who for some reason don't get that set up correctly. The DLL it "exports" is a system DLL.
0 Kudos
anthonyrichards
New Contributor III
2,183 Views
Thanks. A little delving using the Visual Studio menu items Tools...Options...Projects+Solutions...VC++ Directories...Show directories for...Libraries gives the list of library search paths using VS environment variables, Variables such as VSInstallDir.

The only way I have found of actually viewing the contents of a particular VS environment variable was to open the VSVARS32.BAT in the \Program Files\Visual Studio 8\common7\tools\ folder and browse it in an Editor. Is there a way from within Visual Studio?
0 Kudos
Steven_L_Intel1
Employee
2,183 Views
Tools > Options > Intel Visual Fortran > General > Build Options > Show environment in log

Then do a build.

Reading the .bat file will not help you here - some of the variables are generated dynamically by Visual Studio.
0 Kudos
Reply