_get_osfhandle

From RAD Studio
Jump to: navigation, search

Go Up to io.h Index


Header File

io.h

Category

Input/output Routines

Prototype

long _get_osfhandle(int filehandle);

Description

Associates file handles.

The _get_osfhandle function associates an operating system file handle with an existing runtime file handle. The variable filehandle is the file handle of your program.

Return value

On success, _get_osfhandle returns an operating system file handle corresponding to the variable filehandle.

On error, the function returns -1 and sets the global variable errno to

EBADF

an invalid file handle



Example

#include <windows.h>
#include <fcntl.h>
#include <stdio.h>
#include <io.h>
//Example for _get_osfhandle() and _open_osfhandle()
BOOL InitApplication(HINSTANCE hInstance);
HWND InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,
            WPARAM wParam, LPARAM lParam);
Example_get_osfhandle(HWND hWnd);
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;      // message
  if (!InitApplication(hInstance))  // Initialize shared things
   return (FALSE); // Exits if unable to initialize
  /* Perform initializations that apply to a specific instance */
  if (!(InitInstance(hInstance, nCmdShow)))
   return (FALSE);
  /* Acquire and dispatch messages until a WM_QUIT message is received. */
  while (GetMessage(&msg, // message structure
   NULL, // handle of window receiving the message
   NULL, // lowest message to examine
   NULL))  // highest message to examine
  {
  TranslateMessage(&msg); // Translates virtual key codes
  DispatchMessage(&msg);  // Dispatches message to window
  }
  return (msg.wParam);  // Returns the value from PostQuitMessage
}
BOOL InitApplication(HINSTANCE hInstance)
{
  WNDCLASS  wc;
  // Fill in window class structure with parameters that describe the
  // main window.
  wc.style = CS_HREDRAW | CS_VREDRAW; // Class style(s).
  wc.lpfnWndProc = (long (FAR PASCAL*)(void *,unsigned int,unsigned int, long ))MainWndProc; // Function to retrieve messages for
                // windows of this class.
  wc.cbClsExtra = 0;  // No per-class extra data.
  wc.cbWndExtra = 0;  // No per-window extra data.
  wc.hInstance = hInstance; // Application that owns the class.
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName = NULL;  // Name of menu resource in .RC file.
  wc.lpszClassName = "Example"; // Name used in call to CreateWindow.
  /* Register the window class and return success/failure code. */
  return (RegisterClass(&wc));
}
HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND  hWnd; // Main window handle.
  /* Create a main window for this application instance.  */
  hWnd = CreateWindow(
   "Example",                 // See RegisterClass() call.
   "Example _get_osfhandle _open_osfhandle (32 bit)",  // Text for window title bar.
   WS_OVERLAPPEDWINDOW,        // Window style.
   CW_USEDEFAULT,         // Default horizontal position.
   CW_USEDEFAULT,          // Default vertical position.
   CW_USEDEFAULT,            // Default width.
   CW_USEDEFAULT,        // Default height.
   NULL,                  // Overlapped windows have no parent.
   NULL,                  // Use the window class menu.
   hInstance,              // This instance owns this window.
   NULL                   // Pointer not needed.
  );
  /* If window could not be created, return "failure" */
  if (!hWnd)
   return (FALSE);
  /* Make the window visible; update its client area; and return "success" */
  ShowWindow(hWnd, nCmdShow); // Show the window
  UpdateWindow(hWnd);     // Sends WM_PAINT message
  return (hWnd);        // Returns the value from PostQuitMessage
}
#pragma argsused
LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,
            WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
    case WM_CREATE:
    {
      Example_get_osfhandle(hWnd);
      return NULL;
    }
    case WM_QUIT:
    case WM_DESTROY:  // message: window being destroyed
      PostQuitMessage(0);
    break;
    default:      // Passes it on if unprocessed
      return (DefWindowProc(hWnd, message, wParam, lParam));
  }
}
Example_get_osfhandle(HWND hWnd)
{
    long osfHandle;
    char str[128];
    int fHandle = open("file1.c", O_CREAT|O_TEXT);
    if(fHandle != -1)
    {
      osfHandle = _get_osfhandle(fHandle);
      sprintf(str, "file handle = %lx OS file handle = %lx", fHandle, osfHandle);
      MessageBox(hWnd,str,"_get_osfhandle",MB_OK|MB_ICONINFORMATION);
      close(fHandle);
      fHandle = _open_osfhandle(osfHandle, O_TEXT );
      sprintf(str, "file handle = %lx OS file handle = %lx", fHandle, osfHandle);
      MessageBox(hWnd,str,"_open_osfhandle",MB_OK|MB_ICONINFORMATION);
         close(fHandle);
    }
    else
      MessageBox(hWnd,"File Open Error","WARNING",MB_OK|MB_ICONSTOP);
return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

+