Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Dialling up a modem using C++ (http://www.go4expert.com/forums/dialling-modem-using-cpp-t5065/)

ryanszeto 3Jul2007 07:33

Dialling up a modem using C++
 
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.

DaWei 3Jul2007 07:57

Re: Dialling up a modem using C++
 
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.

ryanszeto 4Jul2007 07:22

Re: Dialling up a modem using C++
 
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

DaWei 4Jul2007 09:21

Re: Dialling up a modem using C++
 
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.

ryanszeto 10Jul2007 06:20

Re: Dialling up a modem using C++
 
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

ryanszeto 10Jul2007 06:23

Re: Dialling up a modem using C++
 
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

ryanszeto 10Jul2007 06:49

Re: Dialling up a modem using C++
 
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.

DaWei 10Jul2007 07:27

Re: Dialling up a modem using C++
 
From that other forum where you posted the same question:
Quote:

Originally Posted by dwise
The skinny:

A DLL is a "dynamically linked library". When the program runs it loads the DLL and links to it. If more than one program wants to use the same DLL, then only a single copy gets loaded into memory and all programs using it use the same copy.

A LIB is a statically linked library, which means that its code gets linked when you create your executable and that code resides in the .EXE file.

Loading and linking to a DLL and then calling the resources within the DLL requires certain operations which are a bit complex and cumbersome. Therefore, functions are written that do all that dirty work for you and those functions are placed in the LIB file.

Therefore, basically, the DLL contains the functional code and the LIB contains functions through which you call the DLL's functional code. Another way of putting it is that the API consists of the functions in the LIB. The code that you write will only call functions in the LIB (good thing, because all your calls need to be resolved or else the linker won't create the EXE).

Operationally, when you compile your program, you will need to tell the compiler to link in the LIB file. A common newbie mistake is to forget to link in the LIB, in which case the linker will report several "unresolved reference" errors. Then when you run it, you need to be sure that the DLL is loaded on the system.


ryanszeto 27Jul2007 13:05

Re: Dialling up a modem using C++
 
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.

ryanszeto 31Jul2007 11:01

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!


All times are GMT +5.5. The time now is 01:20.