Advertisement
EnderAlice

Win32 device name test

Jul 2nd, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. /* cl /GF /MD file.c /link /SUBSYSTEM:CONSOLE */
  2. #define _UNICODE
  3. #define UNICODE
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #include <windows.h>
  8.  
  9. void CreateFileNormal(const TCHAR *Name)
  10. {
  11.     HANDLE TestFileHandle;
  12.     TCHAR *CurrentDir;
  13.     TCHAR *Path;
  14.  
  15.     _tprintf_s(_T("Trying CreateFile to create %s normally...\n"), Name);
  16.     CurrentDir = new TCHAR[256];
  17.     Path = new TCHAR[512];
  18.     GetCurrentDirectory(256, CurrentDir);
  19.     _stprintf_s(Path, 512, _T("%s\\%s"), CurrentDir, Name);
  20.     TestFileHandle = CreateFile(Path,  GENERIC_WRITE, FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
  21.     if(TestFileHandle != INVALID_HANDLE_VALUE)
  22.     {
  23.         CloseHandle(TestFileHandle);
  24.         _tprintf_s(_T(" OK!\n"));
  25.     }
  26.     else
  27.     {
  28.         _tprintf_s(_T(" Failed. W32err=%u\n"), GetLastError());
  29.     }
  30.     delete [] CurrentDir;
  31.     delete [] Path;
  32. }
  33.  
  34. void CreateFileWithDeviceNamespace(const TCHAR *Name)
  35. {
  36.     HANDLE TestFileHandle;
  37.     TCHAR *CurrentDir;
  38.     TCHAR *Path;
  39.  
  40.     _tprintf_s(_T("Trying CreateFile to create %s using device namespace...\n"), Name);
  41.     CurrentDir = new TCHAR[256];
  42.     Path = new TCHAR[512];
  43.     GetCurrentDirectory(256, CurrentDir);
  44.     _stprintf_s(Path, 512, _T("\\\\.\\%s\\%s"), CurrentDir, Name);
  45.     TestFileHandle = CreateFile(Path,  GENERIC_WRITE, FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr);
  46.     if(TestFileHandle != INVALID_HANDLE_VALUE)
  47.     {
  48.         CloseHandle(TestFileHandle);
  49.         _tprintf_s(_T(" OK!\n"));
  50.     }
  51.     else
  52.     {
  53.         _tprintf_s(_T(" Failed. W32err=%u\n"), GetLastError());
  54.     }
  55.     delete [] CurrentDir;
  56.     delete [] Path;
  57. }
  58.  
  59. int _tmain(int argc, _TCHAR *argv[])
  60. {
  61.     BOOL Bool;
  62.  
  63.     Bool = DefineDosDevice(DDD_REMOVE_DEFINITION, TEXT("AUX"), nullptr);
  64.     if(!Bool)
  65.     {
  66.         _tprintf_s(_T("DefineDosDevice failed. %u\n"), GetLastError());
  67.     }
  68.  
  69.     CreateFileNormal(TEXT("Hoge"));
  70.     CreateFileNormal(TEXT("AUX"));
  71.  
  72.     CreateFileWithDeviceNamespace(TEXT("Hoge"));
  73.     CreateFileWithDeviceNamespace(TEXT("AUX"));
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement