Windows CE:Accessing a Stream Driver from an Application

#include "stdafx.h"
#include <Windows.h>
#include <WinBase.h>
int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
    HANDLE ret_device = ActivateDeviceEx(L"\\Drivers\\StreamTestDrv" , NULL,0, NULL);
    if (ret_device == INVALID_HANDLE_VALUE)
    {
        printf("Invalid Handle\n");
     }
     else 
    {
        printf("Valid Handle\n");
     }
    HANDLE fileptr = CreateFile(L"DRV1:",
                                                     GENERIC_READ|GENERIC_WRITE,
                                                     FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                     NULL,
                                                     OPEN_EXISTING,
                                                     FILE_ATTRIBUTE_NORMAL,
                                                     NULL);
    printf("File ptr:%d and Invalid Handle value :%d\n", fileptr,INVALID_HANDLE_VALUE);
    if (fileptr == INVALID_HANDLE_VALUE)
    {
       printf("Unable to open file\n");
       return -1;
    }
    printf("File Opened Successfully\n");
    //WriteFile writes the data to the file pointer
    CHAR buff[] = "Hello World";
    DWORD bytes_written;
    int ret_value = WriteFile(fileptr, buff, sizeof(buff), &bytes_written,NULL);
    if (ret_value == 0)
   {
        printf("Failed to write the data\n");
        return -1;
   }
   else
  {
      printf("Successfully written:%d bytes\tret_value[%d]\n",bytes_written, ret_value); 
  }
  LONG high_pos = 0;
  DWORD j = SetFilePointer(fileptr, 0, &high_pos, FILE_END);
  if(j == 0xFFFFFFFF)
     printf("SetFilePointer fail[%d]....[%d] and Invalid_file_pointer:%d\n",j,                                      
               GetLastError(),INVALID_SET_FILE_POINTER );
  ret_value = ReadFile(fileptr, "ABCDEFGHIJ", 10, &bytes_written, NULL);
  //close the file
  BOOL retStatus = CloseHandle(fileptr);
  if (retStatus == FALSE) 
  {
        printf("Unable to close the file\n");
        return 1;
   }
   printf("File closed successfully\n");
   Sleep(20000);  
   DeactivateDevice(ret_device);
   return 0;
}

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig