home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / wmseri / wconnect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  3.5 KB  |  131 lines

  1. //
  2. //     W M S E R I A L - Windows Modem Example
  3. //
  4. // This is only part of a multiple source project.  To build the
  5. // executable please use the makefile provided or construct
  6. // a project file containing the files and settings listed in
  7. // PROJECT.TXT.
  8. //
  9. // Remember to modify the directories to reflect your configuration.
  10. //
  11. //                                                     - J A A
  12.  
  13. // WCONNECT.C
  14.  
  15. #include <windows.h>
  16. #include <string.h>    // for strstr()
  17. #include "wmserial.h"
  18.  
  19. static HWND hConnectWnd;
  20.  
  21. BOOL Connect(HWND hWnd)
  22.    {
  23.    BOOL bResult;
  24.  
  25.    // Save hWnd
  26.    hConnectWnd = hWnd;
  27.  
  28.    if (!nActiveComPort)
  29.        {
  30.        nActiveComPort = InitComPort(hWnd,COMPORT);
  31.        if (nActiveComPort < 0)
  32.            {
  33.            MessageBeep(MB_ICONEXCLAMATION);
  34.            MessageBox(hWnd,"Could not activate COM port.","Example Windows Terminal",MB_ICONSTOP);
  35.            PostMessage(hWnd,WM_CLOSE,0,0L);
  36.            return FALSE;
  37.            }
  38.        }
  39.  
  40.    if (!bConnected)
  41.        {
  42.        bResult = InitModem(hWnd,nActiveComPort);
  43.        if (!bResult)
  44.            {
  45.            MessageBeep(MB_ICONEXCLAMATION);
  46.            MessageBox(hWnd,"Could not initialize modem.","Example Windows Terminal",MB_ICONSTOP);
  47.            PostMessage(hWnd,WM_CLOSE,0,0L);
  48.            return FALSE;
  49.            }
  50.  
  51.        bConnected = Dial();
  52.        if (!bConnected)
  53.            {
  54.            MessageBeep(MB_ICONEXCLAMATION);
  55.            MessageBox(hWnd,"Error connecting.","Example Windows Terminal",MB_ICONSTOP);
  56.            PostMessage(hWnd,WM_CLOSE,0,0L);
  57.            return FALSE;
  58.            }
  59.        else
  60.            {
  61.            if (!EnableCommNotification(nActiveComPort,hWnd,32,32))
  62.                {
  63.                MessageBeep(MB_ICONEXCLAMATION);
  64.                MessageBox(hWnd,"Unable to initialize Comm messages.",
  65.                                "Example Windows Terminal",MB_ICONSTOP);
  66.                PostMessage(hWnd,WM_CLOSE,0,0L);
  67.                }
  68.            bEcho = FALSE;
  69.            } 
  70.        }
  71.  
  72.    return TRUE;
  73.    }
  74.  
  75.  
  76.  
  77. BOOL Dial(void)
  78.    {
  79.    int nResult;
  80.    BOOL bReturnValue;
  81.    HANDLE hBufferMem;
  82.    LPSTR szModemResponse;
  83.  
  84.    // This dials Borland's Online Automated Support Service
  85.    // change the string to ATL1M1DT14084315250\r if 
  86.    // long distance is necessary.
  87.    char szDialString[] = "ATL1M1DT4315250\r";
  88.  
  89.    // Get memory for response
  90.    hBufferMem = GlobalAlloc( GMEM_MOVEABLE, QBUFFSIZE );
  91.    if (!hBufferMem)
  92.        {
  93.        MessageBeep(MB_ICONEXCLAMATION);
  94.        MessageBox(hConnectWnd,"Not enough memory for initializing modem.","Example Windows Terminal",MB_ICONSTOP);
  95.        PostMessage(hConnectWnd,WM_CLOSE,0,0L);
  96.        return FALSE;
  97.        }
  98.  
  99.    szModemResponse = (LPSTR) GlobalLock(hBufferMem);
  100.  
  101.    // Send Initialization String
  102.    nResult = WriteComPort(nActiveComPort,szDialString,lstrlen(szDialString));
  103.    CheckComPort(nActiveComPort,NULL);
  104.    TTYWriteScreen(szDialString);
  105.    if (nResult < 0) 
  106.        return FALSE;
  107.  
  108.    // Get the response
  109.    nResult = GetModemResponse(nActiveComPort,szModemResponse,QBUFFSIZE,60);
  110.    if (nResult <= 0) 
  111.        return FALSE;
  112.    else
  113.        TTYWriteScreen(szModemResponse);
  114.  
  115.    if (strstr(szModemResponse,"CONNECT"))
  116.        bReturnValue = TRUE;
  117.    else
  118.        bReturnValue = FALSE;
  119.  
  120.    GlobalUnlock(hBufferMem);
  121.    return bReturnValue;
  122.    }
  123.  
  124.  
  125.  
  126. void Disconnect(void)
  127.    {
  128.    bConnected = HangUpModem(nActiveComPort);
  129.    nActiveComPort = CloseComPort(nActiveComPort);
  130.    }
  131.