IPacket

Actualización: noviembre 2007

Contiene datos y lo utilizan objetos que implementan IDevicePacketStream para transferir datos entre una aplicación de agente de dispositivos y un equipo de escritorio.

IPacket : public IUnknown

Métodos

Método

Descripción

IPacket::get_Count

Obtiene el número total de objetos de datos del paquete.

IPacket::IsEndOfPacket

Comprueba si el iterador interno ha llegado al final del paquete.

IPacket::ReadBool

Lee un valor booleano del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::ReadByte

Lee un byte del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::ReadBytes

Lee una matriz de bytes del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::ReadInt32

Lee un valor entero del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::ReadChar

Lee un carácter del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::ReadDataType

Devuelve el tipo de datos del objeto actual del paquete como un valor de enumeración.

IPacket::ReadString

Lee una cadena del objeto y apunta el iterador interno al siguiente objeto de datos del paquete.

IPacket::Reset

Restablece el iterador interno del paquete para que apunte al primer objeto de datos del paquete. La operación de lectura siguiente lee el primer objeto de datos del paquete.

IPacket::WriteBool

Escribe un valor booleano en el objeto.

IPacket::WriteByte

Escribe un byte en el objeto.

IPacket::WriteBytes

Escribe una matriz de bytes en el objeto.

IPacket::WriteChar

Escribe un carácter en el objeto.

IPacket::WriteInt32

Escribe un valor entero en el objeto.

IPacket::WriteString

Escribe una cadena en el objeto.

Comentarios

Para obtener un objeto que implemente esta interfaz, utilice GetNewPacket.

Los objetos que implementan esta interfaz sólo pueden contener objetos del tipo byte, byte[], string, int y char. Esto no es extensible a los objetos personalizados de lectura y escritura. No obstante, los usuarios aún pueden transferir objetos personalizados a través de la secuencia dividiendo el tipo de datos personalizados en tipos de datos básicos o serializando los tipos de datos personalizados en una matriz de bytes.

Ejemplo

El ejemplo siguiente es una aplicación de agente de dispositivos que escribe y lee un paquete en el escritorio.

#include "stdafx.h"

// Custom implementation of IAgentTransportShutdownCallback
class MyShutdownCallback: public IAgentTransportShutdownCallback
{
private:
    long ref;
public:
    HRESULT STDMETHODCALLTYPE Shutdown(IUnknown *in_pUnknown) 
    {
        // Add your cleanup code here 
        MessageBox(NULL,_T("conmanclient2 exited"),_T("conmanclient exited"),0);
        return 0;
    }

    // Must implement members from IUnknown
    HRESULT STDMETHODCALLTYPE QueryInterface( REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
    {
        return 0;
    }
    ULONG STDMETHODCALLTYPE AddRef( void)
    {
        return InterlockedIncrement(&ref);
    }

    ULONG STDMETHODCALLTYPE Release( void)
    {
        if(InterlockedDecrement(&ref) == 0)
        {
            delete this;
            return 0;
        }
        return ref;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    // Load the Device Agent Transport Library
    HINSTANCE hmod;
    hmod = LoadLibrary(L"DeviceAgentTransport.dll");

    // Get an instance of IDeviceAgentTransport
    GetDeviceAgentTransportFunc f1 = (GetDeviceAgentTransportFunc)
        ::GetProcAddress(hmod, L"GetDeviceAgentTransport");
    IDeviceAgentTransport *pTransport = NULL;
    f1(&pTransport);

    // Register the callback with the Device Agent Transport
    MyShutdownCallback *shutdownCallback = new MyShutdownCallback();
    pTransport->RegisterShutdownCallback(shutdownCallback,shutdownCallback);

    // Let the desktop application know that this remote agent was deployed successfully 
    // and that this remote agent will handle the supplied list of services.
    LPCOLESTR szServiceIds[] = {L"F85E57BA-5AE9-4FF7-8433-6AB7D991D033"};
    pTransport->AcknowledgeLaunch(1, szServiceIds);

    // Open a communcation stream with desktop application on the service.
    IDevicePacketStream *pStream = NULL;
    pTransport->AcceptConnectionEx(szServiceIds[0], &pStream);

    // Get an instance of IPacket
    GetNewPacketFunc f2 = (GetNewPacketFunc) ::  GetProcAddress(hmod, L"GetNewPacket");
    IPacket *pPacket = NULL;
    f2(&pPacket);

    // Write a message and sent the packet.
    pPacket->WriteBool(true);
    pPacket->WriteByte(0xff);
    pPacket->WriteChar('c');
    pPacket->WriteInt32(1024);
    pPacket->WriteString(L"Hello Desktop Computer");
    pStream->Write(pPacket);

    // Check for a packet while communication stream is connected.
    f2(&pPacket);
    VARIANT_BOOL connected;
    pStream->IsConnected(&connected);
    while(connected)
    {
        pStream->IsConnected(&connected);
        VARIANT_BOOL available;

        // If a packet is found, display the string.
        pStream->IsPacketAvailable(&available);
        if(available)
        {
            pStream->Read(&pPacket);
            VARIANT_BOOL endofpacket;
            pPacket->IsEndOfPacket(&endofpacket);
            while (!endofpacket) 

            {
                pPacket->IsEndOfPacket(&endofpacket);
                DataTypeEnum datatype;
                pPacket->ReadDataType(&datatype);
                switch (datatype)
                {
                    case DT_BYTE:
                        BYTE byteValue;
                        pPacket->ReadByte(&byteValue);
                        break;
                    case DT_INT32:
                        INT32 intValue;
                        pPacket->ReadInt32(&intValue);
                        break;
                    case DT_WIDECHAR:
                        wchar_t charValue;
                        pPacket->ReadChar(&charValue);
                        break;
                    case DT_BOOL:
                        VARIANT_BOOL boolValue;
                        pPacket->ReadBool(&boolValue);
                        break;
                    case DT_BYTEARRAY:
                        BYTE * buffer[100];
                        ULONG length;
                        pPacket->ReadBytes(buffer,&length);
                        break;
                    case DT_STRING:
                        LPWSTR string;
                        pPacket->ReadString(&string);
                        MessageBox(NULL, string,string,0);
                        break;
                    default:
                        break;
                }
            };
        }
    };
    return 0;
}

Equivalente administrado

Microsoft.SmartDevice.DeviceAgentTransport.IPacket

Requisitos

DeviceAgentTransport.h

Vea también

Otros recursos

API de conectividad de dispositivos inteligentes en dispositivos no administrados