Click here to Skip to main content
11,718,589 members (85,681 online)
Click here to Skip to main content

Auto connecting with DUN using InternetAutodial

, 18 Sep 2003 69.3K 1K 18
Rate this:
Please Sign up or sign in to vote.
Quick and dirty hack for automatically bringing DUN dialog if not connected to the internet.
<!-- Add the rest of your HTML here -->

Introduction

After spending approximately 2 hours looking for a way to get Windows XP to automatically dial my ISP when I started a program that needs to be connected to the internet, I gave up and decided to try and write a quick and dirty hack.

For those interested, I've linked to the source above.  It's very small and needs to be linked with WinInet.lib (as well as the "standard" libraries.)  Simply create a Win32 Application and add the code below.

The application, small as it is, is meant to accomplish one or two things. It will:

  • Connect to the internet automatically using the default dial-up network setting
  • Start a program using CreateProcess after a successful connection has been established. This item is optional.


The application starts by calling InternetAutodial passing INTERNET_AUTODIAL_FORCE_UNATTENDED as the first argument. This basically says, "start the default dial-up connection as-is. I don't want to be prompted to do anything." The second argument is a window handle, which in this case is NULL.

If the call to InternetAutodial is successful and an argument was passed to the program, the application attempts to run that argument as an application using CreateProcess. If the application cannot start, a message box appears stating this fact.

Details

There isn't much more to explain code-wise. When using the application, you can either run it with or without a command line. If you want to start an internet-based application that needs an internet connection, the command line to LD needs to contain the name of the application in quotes followed by any command line arguments for the program to start.

For example:

LD "program.exe" program_arguments

The quotes around the program name are necessary if you use a long filename because of the way CreateProcess handles the lpCommandLine parameter (i.e. the second parameter passed to CreateProcess.)

Starting LD by itself simply brings up the default Dial-up Networking dialog if you aren't already connected to the internet.

Once compiled, you can call the application from a shortcut or the command prompt.

NOTE:  There has been an occassion or two where the dialer failed to connect.  The dialup window appeared, but failed to verify the username/password with the ISP.

// LD.cpp : Defines the entry point for the application.
//

#include "stdafx.h" 

#include "windows.h"
#include "wininet.h" 

  

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  BOOL b = InternetAutodial(INTERNET_AUTODIAL_FORCE_UNATTENDED, NULL);

  // not really necessary, but good to know.
  OutputDebugString(b ? "TRUE\n" : "FALSE\n");

  // If a command line was passed to the application, then try and
  // run the application using CreateProcess

  if (*lpCmdLine)
  {
    BOOL success = FALSE;
    STARTUPINFO si = {0};
    PROCESS_INFORMATION pi = {0};

    // As far as I can tell, the STARTUPINFO structure really only needs
    // the cb member filled.  Please correct me if I'm wrong here.

    si.cb = sizeof(si);
    success = CreateProcess(NULL,
                            lpCmdLine,
                            NULL,
                            NULL,
                            FALSE,
                            NORMAL_PRIORITY_CLASS,
                            NULL,
                            NULL,
                            &si,
                            &pi);

    if (FALSE == success)
    {
      MessageBox(NULL, "Couldn't start application", "Error", MB_OK);
    }
  }

 return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Brian Davis
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionProgrmatically Creating New Modem Pin
Rangaraman12-Jul-12 3:36
memberRangaraman12-Jul-12 3:36 
QuestionHow provide Internet service on the remote modem Pin
captainc/c++26-Jun-08 21:36
membercaptainc/c++26-Jun-08 21:36 
Generalnothing happen Pin
kkyirui18-Feb-05 15:39
memberkkyirui18-Feb-05 15:39 
GeneralC# version Pin
rcmetta5-May-04 2:55
memberrcmetta5-May-04 2:55 
GeneralRe: C# version Pin
Teh Wee Meng4-Dec-06 18:41
memberTeh Wee Meng4-Dec-06 18:41 
GeneralUsing, but its not working Pin
Gunn224-Dec-03 18:29
sussGunn224-Dec-03 18:29 
GeneralOne more thing. Pin
Gunn224-Dec-03 18:36
sussGunn224-Dec-03 18:36 
GeneralRe: Using, but its not working Pin
rameshchandra18-May-04 20:35
memberrameshchandra18-May-04 20:35 
GeneralRe: Using, but its not working Pin
java_usuario18-Jan-07 6:08
memberjava_usuario18-Jan-07 6:08 
GeneralSemi Auto Connection to DUN Pin
dinesh_ms20-Sep-03 1:50
memberdinesh_ms20-Sep-03 1:50 
Generalgood, but Pin
geo_m18-Sep-03 21:31
membergeo_m18-Sep-03 21:31 
GeneralRe: good, but Pin
Brad Bruce19-Sep-03 2:03
memberBrad Bruce19-Sep-03 2:03 
GeneralRe: good, but Pin
geo_m19-Sep-03 2:13
membergeo_m19-Sep-03 2:13 
GeneralRe: good, but Pin
Brian Davis19-Sep-03 5:24
memberBrian Davis19-Sep-03 5:24 
GeneralRe: good, but Pin
geo_m19-Sep-03 5:37
membergeo_m19-Sep-03 5:37 
GeneralRe: good, but Pin
Brian Davis19-Sep-03 9:27
memberBrian Davis19-Sep-03 9:27 
GeneralRe: good, but Pin
geo_m19-Sep-03 21:05
membergeo_m19-Sep-03 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 19 Sep 2003
Article Copyright 2003 by Brian Davis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid