Send.cpp

This example demonstrates how to send packets from C++.
See also:
PacketX , Adapter , SendPacket
00001 
00002 // File:         Send.cpp
00003 //
00004 // Description:  This sample demonstrates how to send packets from C++.
00005 //
00006 // Notes:        Compile with Microsoft Visual C++ 6.0 or .NET
00007 //
00008 // Created:      May 10, 2004
00009 //
00010 // Copyright (c) 2000-2004 BeeSync Technologies.
00012 
00013 #define WIN32_LEAN_AND_MEAN
00014 
00015 #ifndef _WIN32_WINNT
00016 #define _WIN32_WINNT 0x0400
00017 #endif
00018 
00019 #include <atlbase.h>
00020 #include <stdio.h>
00021 #include <time.h>
00022 
00023 #import "PacketX.dll" no_namespace, named_guids
00024 
00025 // Packet
00026 BYTE Packet[] =
00027 {
00028    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00,
00029    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00030    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00031 };
00032 #define PacketSize sizeof(Packet)
00033 
00034 // Main entry point
00035 int main(int argc, char* argv[])
00036 {
00037    // Initialize COM
00038    CoInitializeEx(NULL,COINIT_MULTITHREADED);
00039 
00040    // Create PacketX object
00041    IPktXPacketXPtr pPktX(__uuidof(PacketX));
00042 
00043    // Display network adapters
00044    LONG lCount= IPktXAdapterCollectionPtr(pPktX->GetAdapters())->GetCount();
00045    for (int i=0; i < lCount; i++)
00046    {
00047       // If adapter is good print adapter properties
00048       IPktXAdapterPtr pAdapter = IPktXAdapterCollectionPtr(pPktX->GetAdapters())->GetItem(i+1);
00049       if (pAdapter->GetIsGood())
00050       {
00051          _bstr_t bstrtDesciption = pAdapter->GetDescription();
00052          printf("(%d) %s\n",i+1,(char*)bstrtDesciption);
00053       }
00054    }
00055    // Select network adapter
00056    pPktX->PutAdapter(NULL);
00057    while (pPktX->GetAdapter() == NULL)
00058    {
00059       char buffer[256];
00060       printf("Choose adapter#");
00061       try { pPktX->PutAdapter(IPktXAdapterCollectionPtr(
00062          pPktX->GetAdapters())->GetItem(atoi(gets(buffer)))); }
00063       catch ( _com_error& ) {}
00064    }
00065    // Create safe array containing raw packet and fill it
00066    LPSAFEARRAY psa = SafeArrayCreateVector(VT_VARIANT,0,PacketSize);
00067    LPVARIANT rgElems;
00068    SafeArrayAccessData(psa,(LPVOID*)&rgElems);
00069    for (i=0; i < PacketSize; i++)
00070    {
00071       VariantInit(&rgElems[i]);
00072       V_VT(&rgElems[i])=VT_UI1;
00073       V_UI1(&rgElems[i])=Packet[i];
00074    }
00075    // Create variant for safearray
00076    VARIANT Buffer;
00077    VariantInit(&Buffer);
00078    Buffer.vt = VT_VARIANT|VT_ARRAY;
00079    Buffer.parray = psa;
00080 
00081    // Send raw packet, repeat 100 times
00082    IPktXAdapterPtr(pPktX->GetAdapter())->SendPacket(Buffer, CComVariant(100));
00083 
00084    // Cleanup
00085    SafeArrayUnaccessData(psa);
00086    SafeArrayDestroy(psa);
00087    pPktX = NULL;
00088 
00089    // Uninitialize COM
00090    CoUninitialize();
00091    return 0;
00092 }

Copyright © 2000-2007 beeSync. All rights reserved.