Partager via


Custom-Scripting DLL Example (Windows Embedded CE 6.0)

1/6/2010

The following code example provides a skeleton for a custom-scripting DLL.

#include "windows.h"
#include "ras.h"
#include "raserror.h"
#include "rasscript.h"

DWORD 
RasCustomScriptExecute(
   HANDLE hPort,
   LPCWSTR lpszPhonebook,
   LPCWSTR lpszEntryName,
   PFNRASGETBUFFER pfnRasGetBuffer,
   PFNRASFREEBUFFER pfnRasFreeBuffer,
   PFNRASSENDBUFFER pfnRasSendBuffer,
   PFNRASRECEIVEBUFFER pfnRasReceiveBuffer,
   PFNRASRETRIEVEBUFFER pfnRasRetrieveBuffer,
   HWND hWnd,
   RASDIALPARAMS *pRasDialParams,
   RASCUSTOMSCRIPTEXTENSIONS *pRasCustomScriptExtensions
//
// Skeletal function for a RAS custom script
//
)
{
   DWORD dwResult = ERROR_SUCCESS;
   PBYTE pRecvBuffer = NULL,
         pSendBuffer = NULL;
   DWORD cbRecvBuffer,
         cbSendBuffer,
         dwBytesRead,
         dwTimeoutCount;
   HANDLE   hEvent = NULL;

   DEBUGMSG(1, (TEXT("+RasCustomScriptExecute entryname=%s\n"), lpszEntryName));

   do
   {
      //
      // Allocate receive packet buffer
      //
      cbRecvBuffer = 1500;
      dwResult = pfnRasGetBuffer(&pRecvBuffer, &cbRecvBuffer);
      if (dwResult != ERROR_SUCCESS)
      {
         DEBUGMSG(1, (TEXT("RasCustomScriptExecute pfnRasGetBuffer FAILED\n")));
         break;
      }

      //
      // Allocate send packet buffer
      //
      cbSendBuffer = 1500;
      dwResult = pfnRasGetBuffer(&pSendBuffer, &cbSendBuffer);
      if (dwResult != ERROR_SUCCESS)
      {
         DEBUGMSG(1, (TEXT("RasCustomScriptExecute pfnRasGetBuffer FAILED\n")));
         break;
      }

      //
      // Create an event on which to receive
      //
      hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
      if (hEvent == NULL)
      {
         DEBUGMSG(1, (TEXT("RasCustomScriptExecute CreateEvent FAILED\n")));
         dwResult = GetLastError();
         break;
      }

      //
      // Hook the event to hPort so that it is signalled when packets are received
      //
      dwResult =  pfnRasReceiveBuffer(hPort, pRecvBuffer, &dwBytesRead, 1000, hEvent);
      if (dwResult != ERROR_SUCCESS)
      {
         DEBUGMSG(1, (TEXT("RasCustomScriptExecute pfnRasReceiveBuffer FAILED\n")));
         break;
      }

      //
      // Send/receive packets to/from the server as necessary to the custom protocol
      // prior to the PPP LCP negotiations.
      //

      dwTimeoutCount = 0;
      while (!bDone)
      {
         //
         // Build a send packet in pSendBuffer
         //

         mySendPacketSize = MyBuildSendPacket(pSendBuffer);

         //
         // Send the packet
         //
         dwResult = pfnRasSendBuffer(hPort, pSendBuffer, mySendPacketSize);
         if (dwResult != ERROR_SUCCESS)
         {
            DEBUGMSG(1, (TEXT("RasCustomScriptExecute pfnRasSendBuffer FAILED\n")));
            break;
         }

         //
         // Wait for the server to send a response
         //

         dwResult = WaitForSingleObject(hEvent, TIMEOUT_MILLISECONDS);
         
         if (dwResult == WAIT_OBJECT_0)
         {
            //
            // Retrieve the received packet
            //
            dwResult = pfnRasRetrieveBuffer(hPort, pRecvBuffer, &dwBytesRead);
            if (dwResult != ERROR_SUCCESS)
            {
               DEBUGMSG(1, (TEXT("RasCustomScriptExecute pfnRasRetrieveBuffer FAILED %d\n"), dwResult));
               break;
            }

            //
            // Process the received packet
            //
            bDone = MyProcessReceivePacket(pRecvBuffer, dwBytesRead);
            dwTimeoutCount = 0;
         }
         else if (dwResult == WAIT_TIMEOUT)
         {
            //
            // Keep resending trying to elicit a response from the server, until
            // some maximum number of tries is reached.
            //
            dwTimeoutCount++;
            if (dwTimeoutCount > MAX_TIMEOUTS)
            {
               DEBUGMSG(1, (TEXT("RasCustomScriptExecute server not responding\n")));
               break;
            }
         }
         else
         {
            DEBUGMSG(1, (TEXT("RasCustomScriptExecute WaitForSingleObject FAILED\n")));
            break;
         }
      }
   }

   if (hEvent)
      CloseHandle(hEvent);
   if (pSendBuffer)
      pfnRasFreeBuffer(pSendBuffer);
   if (pRecvBuffer)
      pfnRasFreeBuffer(pRecvBuffer);


   DEBUGMSG(1, (TEXT("-RasCustomScriptExecute result=%d\n"), dwResult));

   return dwResult;
}

See Also

Concepts

RAS Custom Scripting