Annotation of ntddk/src/network/packet/testapp/hellowin.c, revision 1.1.1.1

1.1       root        1: #include <windows.h>
                      2: 
                      3: #include "hellowin.h"
                      4: 
                      5: ULONG   Filters[6]={0,
                      6:                     NDIS_PACKET_TYPE_DIRECTED,
                      7:                     NDIS_PACKET_TYPE_MULTICAST,
                      8:                     NDIS_PACKET_TYPE_BROADCAST,
                      9:                     NDIS_PACKET_TYPE_ALL_MULTICAST,
                     10:                     NDIS_PACKET_TYPE_PROMISCUOUS};
                     11: 
                     12: static  char     szbuff[40];
                     13: 
                     14: char    szChildAppName[]="hexdump";
                     15: 
                     16: 
                     17: HFONT   hFont;
                     18: HWND    hwndchild;
                     19: 
                     20: HINSTANCE  hInst;
                     21: 
                     22: ADAPTER Adapter;
                     23: 
                     24: UINT    showdump=0;
                     25: 
                     26: LRESULT FAR PASCAL WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
                     27: 
                     28: LRESULT FAR PASCAL ChildWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
                     29: 
                     30: 
                     31: HFONT GetFont(void);
                     32: 
                     33: DWORD GetTextSize(HWND hWnd, HFONT hFont);
                     34: 
                     35: LRESULT
                     36: HandleCommands(
                     37:     HWND     hWnd,
                     38:     UINT     uMsg,
                     39:     UINT     wParam,
                     40:     LONG     lParam
                     41:     );
                     42: 
                     43: BOOLEAN
                     44: CreateAdapter(
                     45:     IN PADAPTER  pAdapter
                     46:     );
                     47: 
                     48: BOOLEAN
                     49: CloseAdapter(
                     50:     IN PADAPTER  pAdapter
                     51:     );
                     52: 
                     53: 
                     54: int PASCAL WinMain (HINSTANCE hInstance,
                     55:                    HINSTANCE hPrevInstance,
                     56:                     LPSTR     lpszCmdParam,
                     57:                     int       nCmdShow)
                     58: 
                     59: 
                     60: 
                     61: 
                     62:   {
                     63:     static    char szAppName[]="HelloWin";
                     64:     HWND      hwnd;
                     65:     MSG       msg;
                     66:     WNDCLASS  wndclass;
                     67: 
                     68:     hInst=hInstance;
                     69: 
                     70:     if (!hPrevInstance)
                     71:        {
                     72:          wndclass.style        =  CS_HREDRAW | CS_VREDRAW;
                     73:          wndclass.lpfnWndProc  =  WndProc;
                     74:          wndclass.cbClsExtra   =  0;
                     75:          wndclass.cbWndExtra   =  0;
                     76:          wndclass.hInstance    =  hInstance;
                     77:          wndclass.hIcon        =  LoadIcon (NULL, IDI_APPLICATION);
                     78:          wndclass.hCursor      =  LoadCursor(NULL, IDC_ARROW);
                     79:          wndclass.hbrBackground=  GetStockObject(WHITE_BRUSH);
                     80:         wndclass.lpszMenuName =  "GenericMenu";
                     81:         wndclass.lpszClassName=  szAppName;
                     82: 
                     83:          RegisterClass(&wndclass);
                     84:        }
                     85: 
                     86: 
                     87:     if (!hPrevInstance)
                     88:        {
                     89:          wndclass.style        =  CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS |
                     90:                                   CS_BYTEALIGNCLIENT;
                     91:          wndclass.lpfnWndProc  =  ChildWndProc;
                     92:          wndclass.cbClsExtra   =  0;
                     93:          wndclass.cbWndExtra   =  0;
                     94:          wndclass.hInstance    =  hInstance;
                     95:          wndclass.hIcon        =  LoadIcon (hInstance,MAKEINTRESOURCE(1000));
                     96:          wndclass.hCursor      =  LoadCursor(NULL, IDC_ARROW);
                     97:          wndclass.hbrBackground=  GetStockObject(WHITE_BRUSH);
                     98:         wndclass.lpszMenuName =  NULL;
                     99:         wndclass.lpszClassName=  szChildAppName;
                    100: 
                    101:          RegisterClass(&wndclass);
                    102: 
                    103:        }
                    104: 
                    105: 
                    106: 
                    107:     hwnd = CreateWindow (szAppName,
                    108:                          "the hello world program",
                    109:                          WS_OVERLAPPEDWINDOW,
                    110:                          CW_USEDEFAULT,
                    111:                          CW_USEDEFAULT,
                    112:                          CW_USEDEFAULT,
                    113:                          CW_USEDEFAULT,
                    114:                          NULL,
                    115:                          NULL,
                    116:                          hInstance,
                    117:                          NULL);
                    118: 
                    119:     ShowWindow (hwnd,nCmdShow);
                    120:     UpdateWindow(hwnd);
                    121: 
                    122:     while (GetMessage (&msg, NULL, 0,0))
                    123:       {
                    124:         TranslateMessage(&msg);
                    125:         DispatchMessage(&msg);
                    126:       }
                    127: 
                    128:     return (msg.wParam);
                    129:   }
                    130: 
                    131: LRESULT FAR PASCAL WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                    132: 
                    133:   {
                    134:     HDC       hdc;
                    135:     PAINTSTRUCT  ps;
                    136:     RECT         rect;
                    137: 
                    138: 
                    139:     switch (message)
                    140:       {
                    141:         case WM_COMMAND:
                    142:             HandleCommands(hwnd, message, wParam, lParam);
                    143:             return 0;
                    144: 
                    145:         case WM_LBUTTONDOWN:
                    146:           if (!showdump) {
                    147:                hwndchild = CreateWindow (szChildAppName,
                    148:                                "Hex Dump",
                    149:                                WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_VSCROLL |
                    150:                                WS_VISIBLE |  WS_THICKFRAME | WS_CLIPSIBLINGS,
                    151:                                0,0,
                    152:                                300,100,
                    153:                                hwnd,
                    154:                                (HMENU) 1,
                    155:                                hInst,
                    156:                                NULL);
                    157:           } else {
                    158: 
                    159:               SendMessage(hwndchild,WM_DUMPCHANGE,0,0l);
                    160:           }
                    161: 
                    162:           showdump=1;
                    163:           return 0;
                    164: 
                    165: 
                    166: 
                    167:         case WM_CREATE:
                    168: 
                    169:             /* Open a handle to the SC Manager database. */
                    170: 
                    171:             Adapter.schSCManager = OpenSCManager(
                    172:                                          NULL,                   /* local machine           */
                    173:                                          NULL,                   /* ServicesActive database */
                    174:                                          SC_MANAGER_ALL_ACCESS); /* full access rights      */
                    175: 
                    176:             if (Adapter.schSCManager == NULL) {
                    177: 
                    178:                 MessageBox(NULL,TEXT("Could not open SC"),TEXT("Hellwin"),MB_OK);
                    179: 
                    180:             } else {
                    181: 
                    182:                 Adapter.ServiceHandle=OpenService(Adapter.schSCManager,
                    183:                                                   TEXT("Packet"),
                    184:                                                   SERVICE_START);
                    185: 
                    186:                 if (Adapter.ServiceHandle == NULL) {
                    187: 
                    188:                     MessageBox(NULL,TEXT("Could not open service"),TEXT("Hellwin"),MB_OK);
                    189: 
                    190:                 }
                    191: 
                    192:                 StartService(Adapter.ServiceHandle,
                    193:                              0,
                    194:                              NULL
                    195:                              );
                    196: 
                    197:             }
                    198: 
                    199: 
                    200:           lstrcpy(Adapter.AdapterName,
                    201:                   "\\\\.\\Packet");
                    202: 
                    203:           Adapter.BufferSize=1514;
                    204: 
                    205:           Adapter.hMem=GlobalAlloc(GMEM_MOVEABLE,1514);
                    206: 
                    207:           Adapter.lpMem=GlobalLock(Adapter.hMem);
                    208: 
                    209:           Adapter.hMem2=GlobalAlloc(GMEM_MOVEABLE,1514);
                    210: 
                    211:           Adapter.lpMem2=GlobalLock(Adapter.hMem2);
                    212: 
                    213: 
                    214:           return 0;
                    215: 
                    216: 
                    217: 
                    218:         case WM_KEYDOWN:
                    219: 
                    220: 
                    221: 
                    222:           return 0;
                    223: 
                    224:        case WM_PAINT:
                    225:           hdc=BeginPaint(hwnd,&ps);
                    226:           GetClientRect (hwnd,&rect);
                    227: 
                    228:          EndPaint(hwnd,&ps);
                    229:           return 0;
                    230: 
                    231: 
                    232:        case WM_DESTROY:
                    233:           PostQuitMessage(0);
                    234:           return 0;
                    235:       }
                    236:     return DefWindowProc(hwnd,message, wParam, lParam);
                    237:   }
                    238: 
                    239: 
                    240: 
                    241: 
                    242: 
                    243: LRESULT
                    244: HandleCommands(
                    245:     HWND     hWnd,
                    246:     UINT     uMsg,
                    247:     UINT     wParam,
                    248:     LONG     lParam
                    249:     )
                    250: 
                    251: {
                    252:     ULONG    Filter;
                    253:     ULONG    BytesReturned;
                    254:     BOOLEAN  Result;
                    255: 
                    256: 
                    257:     UINT     i;
                    258: 
                    259: 
                    260:     OVERLAPPED   OverLapped;
                    261: 
                    262:     switch (wParam) {
                    263: 
                    264:         case IDM_OPEN:
                    265: 
                    266:             Result=CreateAdapter(&Adapter);
                    267: 
                    268:             if (Result) {
                    269: 
                    270:                 SetWindowText(hWnd,Adapter.AdapterName);
                    271: 
                    272:             }
                    273: 
                    274: 
                    275:             return TRUE;
                    276: 
                    277: 
                    278:         case IDM_CLOSE:
                    279: 
                    280:             CloseHandle(Adapter.hFile);
                    281: 
                    282:             return TRUE;
                    283: 
                    284: 
                    285:         case  IDM_NO_FILTER:
                    286: 
                    287:         case  IDM_DIRECTED:
                    288: 
                    289:         case  IDM_MULTICAST:
                    290: 
                    291:         case  IDM_BROADCAST:
                    292: 
                    293:         case  IDM_ALL_MULTICAST:
                    294: 
                    295:         case  IDM_PROMISCUOUS:
                    296: 
                    297: 
                    298:             Filter=Filters[wParam-IDM_NO_FILTER];
                    299: 
                    300:             DeviceIoControl(Adapter.hFile,
                    301:                             (DWORD)IOCTL_PROTOCOL_SET_FILTER,
                    302:                             &Filter,
                    303:                             4,
                    304:                             NULL,
                    305:                             0,
                    306:                             &BytesReturned,
                    307:                             NULL);
                    308: 
                    309: 
                    310: 
                    311: 
                    312: 
                    313:             return TRUE;
                    314: 
                    315:         case IDM_RESET:
                    316: 
                    317:             DeviceIoControl(Adapter.hFile,
                    318:                             (DWORD)IOCTL_PROTOCOL_RESET,
                    319:                             &Filter,
                    320:                             4,
                    321:                             NULL,
                    322:                             0,
                    323:                             &BytesReturned,
                    324:                             NULL);
                    325: 
                    326: 
                    327: 
                    328: 
                    329: 
                    330:             return TRUE;
                    331: 
                    332: 
                    333: 
                    334:         case IDM_READ:
                    335: 
                    336: 
                    337: 
                    338:           OverLapped.Offset=0;
                    339:           OverLapped.OffsetHigh=0;
                    340:           OverLapped.hEvent=Adapter.hEvent;
                    341: 
                    342: 
                    343:           Result=ReadFile(Adapter.hFile,
                    344:                         Adapter.lpMem,
                    345:                         1500,
                    346:                         &BytesReturned,
                    347:                         &OverLapped);
                    348: 
                    349: 
                    350:           return TRUE;
                    351: 
                    352: 
                    353:         case IDM_SEND:
                    354: 
                    355:           OverLapped.Offset=0;
                    356:           OverLapped.OffsetHigh=0;
                    357:           OverLapped.hEvent=Adapter.hEvent;
                    358: 
                    359: 
                    360: 
                    361:           Adapter.lpMem2[0]=(UCHAR)0xff;
                    362:           Adapter.lpMem2[1]=(UCHAR)0xff;
                    363:           Adapter.lpMem2[2]=(UCHAR)0xff;
                    364:           Adapter.lpMem2[3]=(UCHAR)0xff;
                    365:           Adapter.lpMem2[4]=(UCHAR)0xff;
                    366:           Adapter.lpMem2[5]=(UCHAR)0xff;
                    367: 
                    368:           for (i=0;i<5;i++) {
                    369: 
                    370:               Result=WriteFile(Adapter.hFile,
                    371:                         Adapter.lpMem2,
                    372:                         64,
                    373:                         &BytesReturned,
                    374:                         &OverLapped);
                    375:           }
                    376: 
                    377: 
                    378:           return TRUE;
                    379: 
                    380: 
                    381:         default:
                    382: 
                    383:             return 0;
                    384: 
                    385:     }
                    386: 
                    387: 
                    388: }
                    389: 
                    390: 
                    391: 
                    392: 
                    393: BOOLEAN
                    394: CreateAdapter(
                    395:     IN PADAPTER  pAdapter
                    396:     )
                    397: 
                    398: {
                    399: 
                    400:     pAdapter->hEvent=CreateEvent(NULL,
                    401:                        FALSE,
                    402:                        FALSE,
                    403:                        "Protocol_Event");
                    404: 
                    405:     if (pAdapter->hEvent == NULL) {
                    406:         return 0;
                    407:     }
                    408: 
                    409:     pAdapter->hFile=CreateFile(pAdapter->AdapterName,
                    410:                     GENERIC_WRITE | GENERIC_READ,
                    411:                     0,
                    412:                     NULL,
                    413:                     CREATE_ALWAYS,
                    414:                     FILE_FLAG_OVERLAPPED,
                    415:                     0
                    416:                     );
                    417: 
                    418:     if (pAdapter->hFile == INVALID_HANDLE_VALUE) {
                    419:         CloseHandle(pAdapter->hEvent);
                    420:         return 0;
                    421:     }
                    422: 
                    423: 
                    424: 
                    425: 
                    426:     return TRUE;
                    427: 
                    428: }
                    429: 
                    430: 
                    431: 
                    432: BOOLEAN
                    433: CloseAdapter(
                    434:     IN PADAPTER  pAdapter
                    435:     )
                    436: 
                    437: {
                    438: 
                    439:     CloseHandle(pAdapter->hFile);
                    440: 
                    441:     CloseHandle(pAdapter->hEvent);
                    442: 
                    443:     pAdapter->hFile=NULL;
                    444: 
                    445:     return TRUE;
                    446: }
                    447: 
                    448: 
                    449: 
                    450: 
                    451: 
                    452: 
                    453: 
                    454: 
                    455: LRESULT FAR PASCAL ChildWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                    456: 
                    457:   {
                    458:     HDC           hdc;
                    459:     PAINTSTRUCT   ps;
                    460:     RECT          rect;
                    461:     UINT          i,j;
                    462:     char          szbuff[100],szbuff2[8],szbuff3[17];
                    463:     UINT          limit;
                    464:     LPSTR         lpMem;
                    465: static HFONT  hOldFont;
                    466: static UINT       cyclient,cxclient,lines,dumppos,oldpos,paragphs,cychar;
                    467: static WORD   start;
                    468: 
                    469:     switch (message)
                    470:       {
                    471:         case WM_CREATE:
                    472:           cychar = HIWORD(GetTextSize(hwnd,hFont));
                    473: 
                    474: 
                    475:         case WM_DUMPCHANGE:
                    476:           oldpos=0;
                    477:           dumppos=0;
                    478:           wsprintf(szbuff,"Hex Dump--");
                    479:           SetWindowText(hwnd,szbuff);
                    480: 
                    481:           limit=Adapter.BufferSize;
                    482: 
                    483:           paragphs=(short)((limit & 0xffff) >> 4);
                    484:           SetScrollRange(hwnd,SB_VERT,0,(WORD)paragphs,0);
                    485:           SetScrollPos(hwnd,SB_VERT,(WORD)dumppos,1);
                    486: 
                    487:           InvalidateRect(hwnd,NULL,1);
                    488:           return 0;
                    489: 
                    490:         case WM_SIZE:
                    491:           cyclient=HIWORD(lParam);
                    492:           cxclient=LOWORD(lParam);
                    493:           lines=cyclient/cychar;
                    494:           return 0;
                    495: 
                    496: 
                    497:        case WM_PAINT:
                    498:           hdc=BeginPaint(hwnd,&ps);
                    499:           hOldFont=SelectObject(hdc,hFont);
                    500: 
                    501:           if (1)
                    502:              {
                    503:                limit=Adapter.BufferSize;
                    504: 
                    505: 
                    506:                limit=limit & 0xffff;
                    507: 
                    508:                lpMem=(LPSTR)Adapter.lpMem;
                    509: 
                    510:                start=dumppos*cychar;
                    511: 
                    512:                for (i=start ;((i<limit) && ((i-start)/cychar<lines));i+=16)
                    513:                  {
                    514:                    wsprintf(szbuff,"%08lx  ",i);
                    515:                    for (j=0;(j<16 && (j+i<limit));j++)
                    516:                      {
                    517:                        wsprintf(szbuff2,"%02hX ",((WORD)lpMem[i+j] & 0xff));
                    518:                        lstrcat(szbuff,szbuff2);
                    519:                        if (j==3 || j==7 || j==11)
                    520:                           lstrcat(szbuff," ");
                    521:                        wsprintf(&szbuff3[j],"%c",lpMem[i+j]);
                    522:                        if (szbuff3[j]=='\0')
                    523:                           szbuff3[j]='.';
                    524:                      }
                    525:                    szbuff3[j]='\0';
                    526:                    lstrcat(szbuff,szbuff3);
                    527:                    TextOut(hdc,0,i-start,szbuff,strlen(szbuff));
                    528:                  }
                    529:               }
                    530: 
                    531: 
                    532:           SelectObject(hdc,hOldFont);
                    533:          EndPaint(hwnd,&ps);
                    534:           return 0;
                    535: 
                    536:         case WM_VSCROLL:
                    537:           oldpos=dumppos;
                    538:           switch(wParam)
                    539:             {
                    540:               case SB_PAGEDOWN:
                    541:                 dumppos+=lines;
                    542:                 break;
                    543: 
                    544:               case SB_LINEDOWN:
                    545:                 dumppos+=1;
                    546:                 break;
                    547: 
                    548:               case SB_THUMBPOSITION:
                    549:                 dumppos=LOWORD(lParam);
                    550:                 break;
                    551: 
                    552:               case SB_LINEUP:
                    553:                 dumppos-=1;
                    554:                 break;
                    555: 
                    556:               case SB_PAGEUP:
                    557:                 dumppos-=lines;
                    558:                 break;
                    559: 
                    560:               default:
                    561:                 return 0;
                    562:             }
                    563: 
                    564:           limit=Adapter.BufferSize;
                    565: 
                    566:           paragphs=(short)((limit & 0xffff) >> 4);
                    567: 
                    568:           if (dumppos<0)
                    569:              dumppos=0;
                    570:            else
                    571:               if (dumppos>paragphs)
                    572:                  dumppos=paragphs;
                    573: 
                    574:           if (((oldpos-dumppos==1) || (dumppos-oldpos==1)))
                    575:              {
                    576:                rect.left=0;
                    577:                rect.top=0;
                    578:                rect.right=cxclient;
                    579:                rect.bottom=lines*cychar;
                    580:                ScrollWindow(hwnd,0,cychar*(oldpos-dumppos),&rect,NULL);
                    581:                UpdateWindow(hwnd);
                    582:              }
                    583:            else
                    584:              InvalidateRect(hwnd,NULL,1);
                    585: 
                    586:           SetScrollPos(hwnd,SB_VERT,(WORD)dumppos,1);
                    587:           return 0;
                    588: 
                    589: 
                    590:       }
                    591:     return DefWindowProc(hwnd,message, wParam, lParam);
                    592:   }
                    593: 
                    594: 
                    595: HFONT GetFont(void)
                    596:   {
                    597:     static LOGFONT logfont;
                    598: 
                    599:     logfont.lfHeight=16;
                    600:     logfont.lfCharSet=ANSI_CHARSET;
                    601:     logfont.lfQuality=PROOF_QUALITY;
                    602:     logfont.lfPitchAndFamily=FIXED_PITCH | FF_MODERN;
                    603:     lstrcpy((LPSTR)&logfont.lfFaceName,(LPSTR)"Courier");
                    604: 
                    605:     return CreateFontIndirect(&logfont);
                    606:   }
                    607: 
                    608: DWORD GetTextSize(HWND hWnd, HFONT hFont)
                    609:   {
                    610:     TEXTMETRIC     Metrics;
                    611:     HDC            hDC;
                    612:     HFONT          hOldFont;
                    613: 
                    614:     hDC = GetDC(hWnd);
                    615:     hOldFont=SelectObject(hDC,hFont);
                    616:     GetTextMetrics(hDC,&Metrics);
                    617:     SelectObject(hDC,hOldFont);
                    618:     ReleaseDC(hWnd,hDC);
                    619: 
                    620:     return MAKELONG(Metrics.tmAveCharWidth,
                    621:                     Metrics.tmHeight+Metrics.tmExternalLeading);
                    622: 
                    623:   }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.