调用VPacket.VxD同步事件最简化例子
unit Unit2;
interface
uses Windows;
const
FILE_DEVICE_PROTOCOL = $8000;
METHOD_BUFFERED = 0;
FILE_ANY_ACCESS = 0;
IOCTL_PROTOCOL_QUERY_OID = 0;
IOCTL_PROTOCOL_SET_OID = 1;
IOCTL_PROTOCOL_STATISTICS = 2;
IOCTL_PROTOCOL_RESET = 3;
IOCTL_PROTOCOL_READ = 4;
IOCTL_PROTOCOL_WRITE = 5;
IOCTL_PROTOCOL_MACNAME = 6;
IOCTL_PROTOCOL_BIND = 7;
IOCTL_PROTOCOL_UNBIND = 8;
type
TVPacket = class(TObject)
private
public
hVxd: THandle;
ErrorCode: Integer;
constructor Create;
destructor Destroy; override;
function CTL_CODE(DeviceType, Func, Meth, Access: integer): DWORD;
function Bind(inBuffer: PChar): DWORD;
function GetPacket(var inBuffer: Pointer; cbIn: DWORD): DWORD;
end;
implementation
function TVPacket.CTL_CODE(DeviceType, Func, Meth, Access: Integer): DWORD;
begin
Result := (DeviceType shl 16) or (Access shl 14) or (Func shl 2) or (Meth);
end;
function TVPacket.Bind(inBuffer: PChar): DWORD;
var
cbin: Integer;
res: Boolean;
begin
cbIn := 5;
res := DeviceIoControl(hVxd,
CTL_CODE(FILE_DEVICE_PROTOCOL, IOCTL_PROTOCOL_BIND, METHOD_BUFFERED, FILE_ANY_ACCESS),
inBuffer,
cbIn,
inBuffer,
cbIn,
Result,
nil);
if not res then
ErrorCode := GetLastError;
end;
constructor TVPacket.Create;
begin
hVxD := CreateFile('//./VPACKET.VXD',
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, // 异步要加 or FILE_FLAG_OVERLAPPED
0);
if hVxD = INVALID_HANDLE_VALUE then
ErrorCode := GetLastError;
end;
destructor TVPacket.Destroy;
begin
inherited;
if hVxD <> INVALID_HANDLE_VALUE then
CloseHandle(hVxd);
hVxD := INVALID_HANDLE_VALUE;
end;
function TVPacket.GetPacket(var inBuffer: Pointer; cbIn: DWORD): DWORD;
var
res: Boolean;
cbRet: DWORD;
begin
Result := 0;
res := DeviceIoControl(hVxd,
CTL_CODE(FILE_DEVICE_PROTOCOL, IOCTL_PROTOCOL_READ , METHOD_BUFFERED, FILE_ANY_ACCESS),
inBuffer,
cbIn,
inBuffer,
cbIn,
Result,
nil);
if not res then
ErrorCode := GetLastError;
end;
end.