Dialling up a modem using C++

Discussion in 'C++' started by ryanszeto, Jul 3, 2007.

  1. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi Guys. First time here. Nice to meet you.

    I am a new C++ programmer and I am starting to write a program to dial-up a modem. I don't know where to start. Any ideas?

    Thanks.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    This is a question that's entirely dependent upon your system.

    Hundreds of thousands of computers don't even have a modem. Your program won't even think about working on those. Systems that have a modem may have them hooked up as ports on the I/O bus. Others may have them hooked up to memory-mapped ports.

    Some of those systems won't have an OS (at least, not one worthy of the designation). That means you have to write what might be called an OS function. Those that have a relatively powerful OS might provide you with an existing API for such things. They might also require that you have the knowledge to access kernel-level code, which implies modes (real, or lower-level protected modes) that grant you access to facilities not granted for run-of-the-mill applications.

    You need to be very specific with the information you supply. These things are not part of the C++ language, simply because the language cannot infer what the hardware specifics of your system might be. In instances that can't be covered by a standardized approach, you must provide gobs of information. Breeze through the "Before you post a query" thread for some limited indications of the importance of information.
     
  3. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Here is my situation. I will be using the program in Windows platform. It is going to be a 56K external modem, connected by serial or USB cable. I thought I am going to use the RAS functions.

    Questions:
    1) Do I have to write the code to connect to the modem or to the serial port?

    2) Since the RAS function requires RAS library, which is part of the Windows Platform SDK. Do I have to download it (>900MB) from Microsoft each time I install the program into different computers? Or is there a way I can just install this particular library file into windows?

    3) The thing is I don't know where to start. Is there a sample code I can have a look?

    Just tell me if you need more information (Sorry, I don't know much about C++ and programming)

    Thanks
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Well, that's almost enough information. Enough to guess with, anyway. You say you're using Windows, which means I can guess a desktop/laptop system, running an Intel device. Could be a mobile application, but let's suppose not, just for the hell of it.

    RAS is a whole 'nother thang, and the serial port might play a part in establishing that remote access. You can find plenty on RAS at MSDN. As for the serial port, per se, you might want to start looking here.
     
  5. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I am writing the code already, but I have an issue.

    I am trying to import the library "rasapi32.dll" to get the RAS functions (RasDial, and some other functions) and use GetProAddress to get the functions. The library is successfully loaded, but it just can't find those functions.

    I have browsed through some websites and it talks about "Linking with rasapi32.LIB". What does that mean? Do I have to write the code "importLibrary("rasapi32.dll") instead? Or is there a code I have to add on to so that the functions can be found?

    Here is my code:

    typedef DWORD (*fptr1)(LPRASDIALEXTENSIONS, LPCTSTR, LPRASDIALPARAMS, DWORD, LPVOID, LPHRASCONN);
    fptr1 RasDial;

    HINSTANCE hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\rasapi32.DLL");
    RasDial = (fptr1)GetProcAddress(hinstLib, "RasDial");
    if (RasDial == NULL) {
    printf("ERROR: unable to find the function RasDial.\n");
    FreeLibrary(hinstLib);
    return ERR;
    }

    Thanks for your great help!

    Ryan
     
  6. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I am writing the code already, but I have an issue.

    I am trying to import the library "rasapi32.dll" to get the RAS functions (RasDial, and some other functions) and use GetProAddress to get the functions. The library is successfully loaded, but it just can't find those functions.

    I have browsed through some websites and it talks about "Linking with rasapi32.LIB". What does that mean? Do I have to write the code "importLibrary("rasapi32.LIB") instead? Or is there a code I have to add on to so that the functions can be found?

    Here is my code:

    typedef DWORD (*fptr1)(LPRASDIALEXTENSIONS, LPCTSTR, LPRASDIALPARAMS, DWORD, LPVOID, LPHRASCONN);
    fptr1 RasDial;

    HINSTANCE hinstLib = LoadLibrary("C:\\WINDOWS\\system32\\rasapi32.DLL");
    RasDial = (fptr1)GetProcAddress(hinstLib, "RasDial");
    if (RasDial == NULL) {
    printf("ERROR: unable to find the function RasDial.\n");
    FreeLibrary(hinstLib);
    return ERR;
    }

    Thanks for your great help!

    Ryan
     
  7. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    And one more question:

    What is the difference between DLL file "rasapi32.dll" and importLibrary "rasapi32.LIB"?

    What will they do? When am I going to use that? What functions will they perform?

    THX.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    From that other forum where you posted the same question:
     
  9. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Firstly, thanks for your help.

    I got the modem fired up for dialling up. I can make a call to my mobile phone already.

    I got an enquiry about the RasDial function. What I want to do is after the RasDial function is executed, the code will jump to the next line and continue running, with the modem stayed connected (to the other device). What happen right now is that after dialling up the modem, it connects but then just hangs there (I thought it is still stuck within the RasDial function), doing nothing.

    I have done some research in the MSDN library about the RasDial function but I am totally confused about it. What is the parameter LPVOID lpvNotifier about? What is the use of RasDialFunc()? How can I use that? What does a callback function do and when does that happen?

    Here is my code:
    RASDIALPARAMS rdParams;
    rdParams.dwSize = sizeof(RASDIALPARAMS);
    rdParams.szEntryName[0] = '\0';
    lstrcpy(rdParams.szPhoneNumber, szPhoneNumberToDial);
    rdParams.szCallbackNumber[0] = '\0';
    lstrcpy( rdParams.szUserName, szUserName );
    lstrcpy( rdParams.szPassword, szPassword );
    rdParams.szDomain[0] = '\0';

    HRASCONN hRasConn = NULL;
    DWORD dwRet = RasDial(NULL, NULL, &rdParams, 0L, NULL, &hRasConn);

    Thanks again.
     
  10. ryanszeto

    ryanszeto New Member

    Joined:
    Jul 3, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    New Circumstances

    Right now, when the RasDial function is run synchronously, the program is unusable unless it is disconnected.

    In contrast, when the RasDial function is run asynchronously, it immediately jumps to the callback function and doesn't dial up anymore. It returns success straight after the callback function is finished.

    I am a bit confused about the use of the callback function parameter in the RasDial function. Here is the RasDial function:

    Code:
    DWORD RasDial(
      LPRASDIALEXTENSIONS lpRasDialExtensions,
      LPCTSTR lpszPhonebook,
      LPRASDIALPARAMS lpRasDialParams,
      DWORD dwNotifierType,
      LPVOID lpvNotifier,
      LPHRASCONN lphRasConn
    );
    Is it like I have to set dwNotifierType to 0 and put a self-defined function into the lpvNotifier parameter, and then the RasDial function will do the dial-up while executing the callback function? Is there anything I have missed? Or is there something I have done wrong?

    By the way, I still don't understand the concept of RasDialFunc. What does the placeholder mean? What does that do? (Sorry, maybe this is a silly question. I am just a programming beginner).

    Here is my code:
    Code:
    typedef LPVOID (*RASDIALFUNC)(UINT, RASCONNSTATE, DWORD);
    
    LPVOID RasDialFunction(UINT unMsg, RASCONNSTATE rascs, DWORD dwError)
    {
         /* No coding at the moment */
    }
    
    bool DialUp(void) 
    {
        char szPhoneNumberToDial[RAS_MaxEntryName+1];
        char szUserName[UNLEN+1];
        char szPassword[PWLEN+1];
        
        printf("Please enter the telephone number: ");
        gets(szPhoneNumberToDial);
    
        // Fill RASDIALPARAMS structure
        RASDIALPARAMS rdParams;
        rdParams.dwSize = sizeof(RASDIALPARAMS);
        rdParams.szEntryName[0] = '\0';
        lstrcpy(rdParams.szPhoneNumber, szPhoneNumberToDial);
        rdParams.szCallbackNumber[0] = '\0';
        lstrcpy(rdParams.szUserName, szUserName);
        lstrcpy(rdParams.szPassword, szPassword);    
        rdParams.szDomain[0] = '\0';
    
        // Call RasDial Function
        HRASCONN hRasConn = NULL;
        RASDIALFUNC rdFunc;
        rdFunc = RasDialFunction;
    
        DWORD dwRet = RasDial(NULL, NULL, &rdParams, 0L, rdFunc(WM_RASDIALEVENT, RASCS_OpenPort, 0), &hRasConn);
    
        /* Further more codes...... */
    }
    I have been stucked for a few weeks already. So please HELP!
    Thanks!
     
    Last edited by a moderator: Jul 31, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice