⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 C++Builder网络应用例程. C++Builder网络应用例程.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;

void CALLBACK lineCallback(DWORD hDevice, DWORD dwMsg, DWORD dwCallbackInstance
                            , DWORD dwParam1, DWORD dwParam2, DWORD dwParam3)
{
  String s;
  HCALL hCall;
  char LineCallBuf[1024];

  switch (dwMsg)
  {
    //Replys to asynchronous function calls
    // < 0 is an error
    case LINE_REPLY:
      if (dwParam2 < 0)
        Form1->mMessage->Lines->Add("Reply error");
      else
        Form1->mMessage->Lines->Add("LINE_REPLY Ok");
      break;
    //Status of call has changed
    case LINE_CALLSTATE:
      //Call is specified in the hDevice parameter
      hCall = (HCALL)hDevice;
      switch (dwParam1)
      {
        //Call has gone idle
        case LINECALLSTATE_IDLE:
          if (hCall)
          {
            //Free the memory associated with the call
            lineDeallocateCall(hCall);
            //Tell the user that the call memory was freed
            Form1->mMessage->Lines->Add("Idle -- Call deallocated");
            //Adjust the buttons
            Form1->bCall->Enabled = TRUE;
            Form1->HangUp->Enabled = FALSE;
            //if the terminal is connected, shut it down
            Form1->ShutdownTerminal();
          }
          break;
        //Call has connected
        case LINECALLSTATE_CONNECTED:
          if (hCall)
          {
            //Start the connected message off with this string
            s = "Connected: ";
            //Tell the line call status buffer its total size
            ((LPLINECALLINFO)LineCallBuf)->dwTotalSize = sizeof(LineCallBuf);
            //Get the call status (0 indicates success)
            if (lineGetCallInfo(hCall, (LPLINECALLINFO)LineCallBuf) == 0)
            {
              //Check to see if there is an application name associated
              //with the call
              if (((LPLINECALLINFO)LineCallBuf)->dwAppNameSize > 0)
              {
                //Add the application name to the message
                s = s + (LineCallBuf +
                  ((LPLINECALLINFO)LineCallBuf)->dwAppNameOffset);
                //Display the message
                Form1->mMessage->Lines->Add(s);
              }
              //Open the communications port
              Form1->ConnectTerminal();
            }
          }
          break;
        //Dialing is complete and the call is on the way
        case LINECALLSTATE_PROCEEDING:
          Form1->mMessage->Lines->Add("Proceeding");
          break;
        //Phone is dialing
        case LINECALLSTATE_DIALING:
          Form1->mMessage->Lines->Add("Dialing");
          //Set up the buttons
          Form1->bCall->Enabled = FALSE;
          Form1->HangUp->Enabled = TRUE;
          break;
        //Line was busy
        case LINECALLSTATE_BUSY:
          Form1->mMessage->Lines->Add("Busy");
          break;
        //Call has disconnected
        case LINECALLSTATE_DISCONNECTED:
          //Start the disconneted message
          s = "Disconnected: ";
          //Why did the call disconnect?
          switch (dwParam2)
          {
            //Normal disconnect
            case LINEDISCONNECTMODE_NORMAL:
              s = s + "normal";
              break;
            //Line was busy and disconnected
            case LINEDISCONNECTMODE_BUSY:
              s = s + "busy";
              break;
          }
          //Tell the user about the disconnect
          Form1->mMessage->Lines->Add(s);
          //Disconnect the call if required
          if (hCall)
            lineDrop(hCall, NULL, 0);
          //Set up the buttons
          Form1->bCall->Enabled = TRUE;
          Form1->HangUp->Enabled = FALSE;
          //If the terminal is conneted, shut it down
          Form1->ShutdownTerminal();
          break;
      }
      break;
  }
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
  //Initialize the state of the terminal
  TerminalConnected = FALSE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  DWORD nDevs, tapiVersion;
  LINEEXTENSIONID extid;

  //Initialize the call parameters
  strnset((LPSTR)&CallParams, '0', sizeof(CallParams));

  //Set the total size of the structure
  CallParams.dwTotalSize = sizeof(CallParams);
  //This is a voice telephone line
  CallParams.dwBearerMode = LINEBEARERMODE_VOICE;
  //Modems will be speaking on the line
  CallParams.dwMediaMode = LINEMEDIAMODE_DATAMODEM;

  //initialize connection to TAPI (Return < 0 indicates an error)
  if (lineInitialize(&LineApp, HInstance, lineCallback, "Dialer", &nDevs) < 0)
  {
    //Indicate the error to this application
    LineApp = 0;
  }
  //Make sure there are some TAPI devices
  else if (nDevs == 0)
  {
    //Close the connection to TAPI
    lineShutdown(LineApp);
    //Indicate the error to this application
    LineApp = 0;
  }
  //Application and TAPI negotiate to agree on a TAPI version
  //< 0 indicates error
  else if (lineNegotiateAPIVersion(LineApp, 0, 0x00010000, 0x00090000,
                                  &tapiVersion, &extid) < 0)
  {
    //Close the connection to TAPI
    lineShutdown(LineApp);
    //Indicate the error to this application
    LineApp = 0;
  }
  //Open the line using a device that supports the
  //parameters in the CallParams parameter(< 0 indicates failure)
  else if (lineOpen(LineApp, LINEMAPPER, &Line, tapiVersion, 0, 0,
                   LINECALLPRIVILEGE_NONE, 0, &CallParams) < 0)
  {
    //Close the connection to TAPI
    lineShutdown(LineApp);
    //Indicate the error to this application
    LineApp = 0;
    Line = 0;
  }

  //If an error occured above, tell the user
  if (Line == 0)
    mMessage->Lines->Add("Error");

}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  //Close the line if it is open
  if (Line)
    lineClose(Line);

  //Close the connection to TAPI if it is open
  if (LineApp)
    lineShutdown(LineApp);

  //Close the terminal if it is open
  if (TerminalConnected)
    ShutdownTerminal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::bCallClick(TObject *Sender)
{
  //If the user entered something in the number edit box
  if (Edit1->Text.Length())
  {
    //Clear the status memo box
    mMessage->Lines->Clear();
    //Make a call on the line (< 0 is an error)
    if (lineMakeCall(Line, &Call, Edit1->Text.c_str(), 0, &CallParams) < 0)
      //Indicate the error
      mMessage->Lines->Add("Could not initiate call");
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
  //Put the cursor in the phone number edit box
  Edit1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HangUpClick(TObject *Sender)
{
  //If a call is open
  if (Call)
    //Drop it (return < 0 is an error)
    if (lineDrop(Call, NULL, 0) < 0)
      //If an error occurs, report it
      Form1->mMessage->Lines->Add("Error on drop call " + IntToStr(GetLastError()));
}
//----------------------------------------------------------------------------
void __fastcall TForm1::InputKeyPress(TObject *Sender, char &Key)
{
  //When the user presses <ENTER>
  if (Key == 13)
    //Write the last memo line to the communications file
    //(append the carrige return)
    FileWriteThread->
      WriteData(Input->Lines->Strings[Input->Lines->Count - 1] + "\r");
}
//---------------------------------------------------------------------------
//Start the dumb terminal
void __fastcall TForm1::ConnectTerminal(void)
{
  DCB dcb;
  COMMTIMEOUTS ct;
  MODEM_INFO ModemInfo;

  //If the terminal is already running return
  if (TerminalConnected)
    return;

  //Set the total size of the modem information buffer
  ModemInfo.vs.dwTotalSize = sizeof(MODEM_INFO);
  //Set the format of the buffer
  ModemInfo.vs.dwStringFormat = STRINGFORMAT_BINARY;

  //Clear the input and output memos
  Input->Lines->Clear();
  Output->Lines->Clear();

  //Get the device information of the line which is connected to a modem

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -