| ||
This chapter covers a narrow range of a wide programming area called network programming. The main topic that will be covered here is access to the resources of a local area network (LAN). Another aspect of network programming socket programming is too vast. Therefore, here, I cover only the basics of this topic.
In application programming, it is often necessary to detect network devices. Principally, this problem can be formulated more generally : How do you determine the type of a specific device? If you have some experience with programming for MS-DOS, you might remember that determining the device type in MS-DOS wasn't a trivial task. Windows simplifies this problem. In this operating system, there is a useful function GetDriveType. The only argument of this function is the string containing the name of the root directory of the device under consideration, for example, A:\ or D:\. The device type is determined by the value returned by this function (see the DRIV.INC file in Listing 17.1). The result of executing this program is shown in Fig. 17.1.
// The DRIV.RC file // Constant definitions #define WS_SYSMENU 0x00080000L #define WS_MINIMIZEBOX 0x00020000L #define WS_MAXIMIZEBOX 0x00010000L #define WS_VISIBLE 0x10000000L #define WS_TABSTOP 0x00010000L #define WS_VSCROLL 0x00200000L #define DS_3DLOOK 0x0004L #define LBS_NOTIFY 0x0001L #define LBS_SORT 0x0002L #define LBS_WANTKEYBOARDINPUT 0x0400L // Identifiers #define LIST1 101 //Dialog box definition DIAL1 DIALOG 0, 0, 180, 110 STYLE WS_SYSMENU WS_MINIMIZEBOX WS_MAXIMIZEBOX DS_3DLOOK CAPTION "Determining device types" FONT 8, "Arial" { CONTROL "ListBox1", LIST1, "listbox", WS_VISIBLE WS_TABSTOP WS_VSCROLL LBS_NOTIFY LBS_WANTKEYBOARDINPUT, 16, 16, 100, 75 } ; The DRIV.INC file ; Constants ; Values returned by the GetDriveType function ; Values 0 and 1 can be considered indications ; that the device is missing DRIVE_REMOVABLE equ 2 ; Floppy drive DRIVE_FIXED equ 3 ; Hard disk drive DRIVE_REMOTE equ 4 ; Network disk DRIVE_CDROM equ 5 ; CD-ROM drive DRIVE_RAMDISK equ 6 ; RAM disk ; This message arrives when the window is closed WM_CLOSE equ 10h WM_INITDIALOG equ 110h WM_COMMAND equ 111h LB_ADDSTRING equ 180h LB_RESETCONTENT equ 184h WM_LBUTTONDOWN equ 201h ; Prototypes of external procedures IFDEF MASM EXTERN lstrcpyA@8:NEAR EXTERN lstrcatA@8:NEAR EXTERN GetDriveTypeA@4:NEAR EXTERN ExitProcess@4:NEAR EXTERN GetModuleHandleA@4:NEAR EXTERN DialogBoxParamA@20:NEAR EXTERN EndDialog@8:NEAR EXTERN SendDlgItemMessageA@20:NEAR ELSE EXTERN lstrcpyA:NEAR EXTERN lstrcatA:NEAR EXTERN GetDriveTypeA:NEAR EXTERN ExitProcess:NEAR EXTERN GetModuleHandleA:NEAR EXTERN DialogBoxParamA:NEAR EXTERN EndDialog:NEAR EXTERN SendDlgItemMessageA:NEAR lstrcpyA@8 = lstrcpyA lstrcatA@8 = lstrcatA GetDriveTypeA@4 = GetDriveTypeA ExitProcess@4 = ExitProcess GetModuleHandleA@4 = GetModuleHandleA DialogBoxParamA@20 = DialogBoxParamA EndDialog@8 = EndDialog SendDlgItemMessageA@20 = SendDlgItemMessageA ENDIF ; Structures ; Message structure MSGSTRUCT STRUC MSHWND DD ? MSMESSAGE DD ? MSWPARAM DD ? MSLPARAM DD ? MSTIME DD ? MSPT DD ? MSGSTRUCT ENDS ; The DRIV.ASM file .586P ; Flat memory model .MODEL FLAT, stdcall include driv.inc ; INCLUDELIB directives for the linker IFDEF MASM includelib c:\masm32\lib\user32.lib includelib c:\masm32\lib\kernel32.lib ELSE includelib c:\tasm32\lib\import32.lib ENDIF ;--------------------------------------------- ; Data segment _DATA SEGMENT PRIZ DB 0 MSG MSGSTRUCT <?> HINST DD 0 ;Application descriptor PA DB "DIAL1", 0 ROO DB "?:\", 0 BUFER DB 40 DUP(0) TYP0 DB " Device missing", 0 TYP1 DB " Device missing", 0 TYP2 DB " Floppy disk", 0 TYP3 DB " Hard disk", 0 TYP4 DB " Network disk", 0 TYP5 DB " CD", 0 TYP6 DB " RAM drive", 0 INDEX DD OFFSET TYP0 DD OFFSET TYP1 DD OFFSET TYP2 DD OFFSET TYP3 DD OFFSET TYP4 DD OFFSET TYP5 DD OFFSET TYP6 _DATA ENDS ; Code segment _TEXT SEGMENT START: ; Get the application descriptor PUSH 0 CALL GetModuleHandleA@4 MOV [HINST], EAX ;------------------------------- PUSH 0 PUSH OFFSET WNDPROC PUSH 0 PUSH OFFSET PA PUSH [HINST] CALL DialogBoxParamA@20 CMP EAX, -1 JNE KOL ; Error message KOL: ;------------------------------- PUSH 0 CALL ExitProcess@4 ;------------------------------- ; Window procedure ; Position of parameters in the stack ; [BP+014H] ; LPARAM ; [BP+10H] ; WAPARAM ; [BP+0CH] ; MES ; [BP+8] ; HWND WNDPROC PROC PUSH EBP MOV EBP, ESP PUSH EBX PUSH ESI PUSH EDI ;------------------------------- CMP DWORD PTR [EBP+0CH], WM_CLOSE JNE L1 PUSH 0 PUSH DWORD PTR [EBP+08H] CALL EndDialog@8 JMP FINISH L1: CMP DWORD PTR [EBP+0CH], WM_INITDIALOG JNE L2 L4: ; Analyzing the devices and filling the list MOV ECX, 65 LOO: PUSH ECX MOV ROO, CL ; Determining the device type PUSH OFFSET ROO CALL GetDriveTypeA@4 ; Full list CMP PRIZ, 0 JZ _ALL CMP EAX, 2 JB L3 _ALL: ; Get the index SHL EAX, 2 PUSH EAX ; Create a string for the list PUSH OFFSET ROO PUSH OFFSET BUFER CALL lstrcpyA@8 POP EBX PUSH INDEX[EBX] PUSH OFFSET BUFER CALL lstrcatA@8 ; Send the string into the list PUSH OFFSET BUFER PUSH 0 PUSH LB_ADDSTRING PUSH 101 PUSH DWORD PTR [EBP+08H] CALL SendDlgItemMessageA@20 L3: ; Check whether the loop boundary has been reached POP ECX INC ECX CMP ECX, 91 JNE LOO JMP FINISH L2: CMP DWORD PTR [EBP+0CH],, WM_LBUTTONDOWN JNE FINISH PUSH 0 PUSH 0 PUSH LB_RESETCONTENT PUSH 101 PUSH DWORD PTR [EBP+08H] CALL SendDlgItemMessageA@20 CMP PRIZ, 0 JE YES_0 MOV PRIZ, 0 JMP L4 YES_0: MOV PRIZ, 1 JMP L4 FINISH: MOV EAX, 0 POP EDI POP ESI POP EBX POP EBP RET 16 WNDPROC ENDP _TEXT ENDS END START
To translate the program presented in Listing 17.1, issue the following commands for MASM32:
ml /c /coff /DMASM driv.asm rc driv.rc link /subsystem:windows driv.obj driv.res
Issue the following commands for TASM32:
tasm32 /ml driv.asm brcc32 driv.rc tlink32 -aa driv.obj,,,,,driv.res
Having determined that a specific device is a network device, you might ask the following: How do I determine the device status? In this case, I interpret the device status as one of the following possible situations: the device is available for reading and writing, the device is read-only, or the device is unavailable. To achieve this, I prefer the following approach: To check the device status, I use the functions CreateFile and GetDiskFreeSpace. You are already acquainted with the first function. The second function can be used to determine the amount of free disk space. If this device allows you to create a file (the best approach is creating a file with the "delete after closing" attribute, in which case the operating system will automatically delete the file after it is closed) and read information about the device, this means that the device is available for reading and writing. If the device allows you to read information about it but doesn't allow you to create a file, the device is read-only. Finally, if neither of the operations is allowed, the device is unavailable. This approach is simple and straightforward yet efficient.
| ||