DosQueryHType

From EDM2
(Redirected from OS2 API:CPI:DosQueryHType)
Jump to: navigation, search

Description

Determines whether a handle refers to a file or a device.

Syntax

#define INCL_DOSFILEMGR
#include <os2.h>

HFILE     hFile;  /*  File handle. */
PULONG    pType;  /*  Address of the value indicating the handle type. */
PULONG    pAttr;  /*  Address of the device-driver attribute word if pType indicates a local character device. */
APIRET    ulrc;   /*  Return Code. */

ulrc = DosQueryHType(hFile, pType, pAttr);

Parameters

hFile (HFILE) - input 
File handle.
pType (PULONG) - output 
Address of the value indicating the handle type.
Possible values are shown in the following list:
15 - Network bit:
0 - The handle refers to a local file, device, or pipe.
1 - The handle refers to a remote file, device, or pipe.
14 - 1 Protected file handle.
13-8 - Reserved.
7-0 - HandleClass: Describes the handle class. It may take on the following values in the low byte of pType:
0 - Disk file
1 - Character device
2 - Pipe
Values greater than 2 are reserved.
pAttr (PULONG) - output 
Address of the device-driver attribute word if pType indicates a local character device.

Return Code

ulrc (APIRET) - returns

DosQueryHType returns one of the following values:

  • 0 NO_ERROR
  • 6 ERROR_INVALID_HANDLE

Remarks

DosQueryHType enables programs that are interactive or file-oriented to determine the source of their input. For example, CMD.EXE suppresses writing prompts if the input is from a disk file.

Example Code

This example handles the client side of a pipe. It opens an existing named pipe, queries the pipe handle type and pipe state, sends a message to the host, reads the host reply, and finally closes the pipe.

Before running this example, compile and run the example code shown in the DosConnectNPipe, DosCreateNPipe, DosDisConnectNPipe, or DosSetNPipeSem functions, which handles the host side of the pipe.

#define INCL_DOSFILEMGR       /* DOS File Manager values */
#define INCL_DOSNMPIPES       /* DOS Named Pipes values */
#define INCL_DOSSEMAPHORES    /* DOS Semaphore values */
#define INCL_DOSERRORS        /* DOS Error values */
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(VOID) {
   APIRET   rc                     = NO_ERROR;   /* Return code */
   CHAR     message[256]           = "";         /* Message buffer */
   HFILE    PipeHandle             = NULLHANDLE; /* Pipe handle */
   PIPEINFO PipeBuffer[4]          = {{0}};
   struct   _AVAILDATA  BytesAvail = {0};
   UCHAR    Buffer[200]            = {0};
   ULONG    bytes                  = 0;
   ULONG    Action                 = 0;
   ULONG    PipeState              = 0;
   ULONG    HandType               = 0;
   ULONG    FlagWord               = 0;
   ULONG    BytesRead              = 0;

   rc = DosOpen("\\PIPE\\EXAMPLE", &PipeHandle, &Action, 0, 0, FILE_OPEN,
                OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE |
                OPEN_FLAGS_FAIL_ON_ERROR, NULL);
   if (rc != NO_ERROR) {
      printf("DosOpen error: error code = %u\n", rc);
      return 1;
   }  else printf("Connected to pipe.\n");

   rc = DosQueryHType(PipeHandle, &HandType, &FlagWord);
   if (rc != NO_ERROR) {
      printf("DosQueryHType error: error code = %u\n", rc);
      return 1;
   }  else printf("Handle type value is %u\n", HandType);
   rc = DosPeekNPipe(PipeHandle, Buffer, sizeof(Buffer),
                     &BytesRead, &BytesAvail, &PipeState);
   if (rc != NO_ERROR) {
      printf("DosPeekNPipe error: error code = %u\n", rc);
      return 1;
   }  else printf("Pipe status value is %u\n\n", PipeState);

   printf("Enter message to send to PIPEHOST: ");

   fflush(NULL);   /* Flush above printf out to display */
   gets(message);

   rc = DosWrite(PipeHandle, message, strlen(message), &bytes);
   if (rc != NO_ERROR) {
      printf("DosWrite error: error code = %u\n", rc);
      return 1;
   }

   rc = DosRead(PipeHandle, message, sizeof(message), &bytes);
   if (rc != NO_ERROR) {
      printf("DosRead error: error code = %u\n", rc);
      return 1;
   }

   printf("\nMessage received from PIPEHOST: %s\n\n", message);

   rc = DosClose(PipeHandle);
   /* Should check if (rc != NO_ERROR) here... */

   printf("...Disconnected\n");
   return NO_ERROR;
}

Related Functions