1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
////////////////////////////////////////////////////////////////////
// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
// All rights reserved
// This file was released under the GPLv2 on June 2015.
////////////////////////////////////////////////////////////////////

/*************************************************************************
*
* File: user_lib.cpp
*
* Module: User-mode library
*
* Description: common useful user-mode functions
*
* Author: Ivan
*
*************************************************************************/


#ifndef __USER_LIB_CPP__
#define __USER_LIB_CPP__

#include "user_lib.h"

TCHAR* MediaTypeStrings[] =
{
    "CD-ROM"        ,
    "CD-R"          ,
    "CD-RW"         ,
    "DVD-ROM"       ,
    "DVD-RAM"       ,
    "DVD-R"         ,
    "DVD-RW"        ,
    "DVD+R"         ,
    "DVD+RW"        ,
    "DD CD-ROM"     ,
    "DD CD-R"       ,
    "DD CD-RW"      ,
    "BD-ROM"        ,
    "BD-RE"         ,
    "[BUSY]"        ,
    "Unknown"
};

void * __cdecl mymemchr (
    const void * buf,
    int chr,
    size_t cnt
    )
{
    while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
        buf = (unsigned char *)buf + 1;
        cnt--;
    }

    return(cnt ? (void *)buf : NULL);
} // end mymemchr()

char * __cdecl  mystrrchr(
    const char * string,
    int ch
    )
{
    char *start = (char *)string;

    while (*string++) {;}

    while (--string != start && *string != (char)ch) {;}

    if (*string == (char)ch) {
        return( (char *)string );
    }

    return(NULL);
} // end mystrrchr()

char * __cdecl  mystrchr(
    const char * string,
    int ch
    )
{
    while (*string != (char)ch && *string != '\0' ) {
        string++;
    }

    if (*string == (char)ch) {
        return( (char *)string );
    }

    return(NULL);
} // end mystrchr()


int __cdecl Exist (
    PCHAR path
    )
{
    DWORD attr;

    attr = GetFileAttributes((LPTSTR)path);
    if (attr  == 0xffffffff) {
            return 0;
    }

    return 1;

} // end Exist()

ULONG
MyMessageBox(
    HINSTANCE hInst,
    HWND hWnd,
    LPCSTR pszFormat,
    LPCSTR pszTitle,
    UINT fuStyle,
    ...
    )
{
    CHAR szTitle[80];
    CHAR szFormat[1024];
    LPSTR pszMessage;
    BOOL fOk;
    int result;
    va_list ArgList;

    if (!HIWORD(pszTitle)) {
        LoadString(hInst, LOWORD(pszTitle), szTitle, sizeof(szTitle)/sizeof(szTitle[0]));
        pszTitle = szTitle;
    }

    if (!HIWORD(pszFormat)) {
        // Allow this to be a resource ID
        LoadString(hInst, LOWORD(pszFormat), szFormat, sizeof(szFormat)/sizeof(szFormat[0]));
        pszFormat = szFormat;
    }

    va_start(ArgList, fuStyle);
    fOk = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                        | FORMAT_MESSAGE_FROM_STRING,
                        pszFormat, 0, 0, (LPTSTR)&pszMessage, 0, &ArgList);

    va_end(ArgList);

    if (fOk && pszMessage) {
        result = MessageBox(hWnd, pszMessage, pszTitle, fuStyle | MB_SETFOREGROUND);
        LocalFree(pszMessage);
    } else {
        return -1;
    }

    return result;
} // end MyMessageBox()


/// Return service status by service name.
JS_SERVICE_STATE
ServiceInfo(
    LPCTSTR ServiceName
    )
{
    SC_HANDLE       schService;
    DWORD           RC;
    SERVICE_STATUS  ssStatus;
    JS_SERVICE_STATE return_value;
    SC_HANDLE       schSCManager;

    schSCManager = OpenSCManager(
                                NULL,                   // machine (NULL == local)
                                NULL,                   // database (NULL == default)
                                SC_MANAGER_ALL_ACCESS   // access required
                               );
    if (!schSCManager) {
        schSCManager = OpenSCManager(
                                    NULL,                   // machine (NULL == local)
                                    NULL,                   // database (NULL == default)
                                    SC_MANAGER_CONNECT   // access required
                                   );
    }
    if (!schSCManager)
        return JS_ERROR_STATUS;
    schService = OpenService(schSCManager, ServiceName, SERVICE_QUERY_STATUS);
    if (!schService) {
        RC = GetLastError();
        CloseServiceHandle(schSCManager);
        if (RC == ERROR_SERVICE_DOES_NOT_EXIST) return JS_SERVICE_NOT_PRESENT;
                                           else return JS_ERROR_STATUS;
    }
    QueryServiceStatus(schService, &ssStatus);
    if(ssStatus.dwCurrentState == SERVICE_RUNNING) {
        return_value = JS_SERVICE_RUNNING;
    } else {
        return_value = JS_SERVICE_NOT_RUNNING;
    }
    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);
    return return_value;
} // end ServiceInfo()

BOOL
CheckCdrwFilter(
    BOOL ReInstall
    )
{
    char CdromUpperFilters[1024];
    bool    found = false;

    if (LOBYTE(LOWORD(GetVersion())) < 5) {
        return true;
    }

    if (GetRegString(CDROM_CLASS_PATH,REG_UPPER_FILTER_NAME,&CdromUpperFilters[0],arraylen(CdromUpperFilters))) {
        char *token = &CdromUpperFilters[0];

        while (*token) {
            if (!strcmp(token,CDRW_SERVICE)) {
                found = true;
                break;
            }
            token += strlen(token)+1;
        }
        if (!found) {
            memcpy(token,CDRW_SERVICE,sizeof(CDRW_SERVICE));
            *(token+sizeof(CDRW_SERVICE)) = '\0';
            *(token+sizeof(CDRW_SERVICE)+1) = '\0';
            if(ReInstall) {
                RegisterString(CDROM_CLASS_PATH,REG_UPPER_FILTER_NAME,&CdromUpperFilters[0],TRUE,token-&CdromUpperFilters[0]+sizeof(CDRW_SERVICE)+1);
                found = true;
            }
        }
    } else {
        memcpy(CdromUpperFilters,CDRW_SERVICE,sizeof(CDRW_SERVICE));
        CdromUpperFilters[sizeof(CDRW_SERVICE)] = '\0';
        CdromUpperFilters[sizeof(CDRW_SERVICE)+1] = '\0';
        if(ReInstall) {
            RegisterString(CDROM_CLASS_PATH,REG_UPPER_FILTER_NAME,&CdromUpperFilters[0],TRUE,sizeof(CDRW_SERVICE)+1);
            found = true;
        }
    }
    return found;

} // end CheckCdrwFilter()

BOOL 
RegisterString(
    LPSTR pszKey, 
    LPSTR pszValue, 
    LPSTR pszData,
    BOOLEAN MultiSz,
    DWORD size
    )
{

    HKEY hKey;
    DWORD dwDisposition;

    // Create the key, if it exists it will be opened
    if (ERROR_SUCCESS != 
        RegCreateKeyEx(
          HKEY_LOCAL_MACHINE,      // handle of an open key 
          pszKey,                  // address of subkey name 
          0,                       // reserved 
          NULL,                    // address of class string 
          REG_OPTION_NON_VOLATILE, // special options flag 
          KEY_ALL_ACCESS,          // desired security access 
          NULL,                    // address of key security structure 
          &hKey,                   // address of buffer for opened handle  
          &dwDisposition))         // address of disposition value buffer 
    {
        return FALSE;
    }

    // Write the value and it's data to the key
    if (ERROR_SUCCESS != 
        RegSetValueEx(
            hKey,                                   // handle of key to set value for  
            pszValue,                               // address of value to set 
            0,                                      // reserved 
            MultiSz ? REG_MULTI_SZ : REG_SZ,        // flag for value type 
            (CONST BYTE *)pszData,                  // address of value data 
            MultiSz ? size : strlen(pszData) ))     // size of value data 
    {
        
        RegCloseKey(hKey);
        return FALSE;
    }

    // Close the key
    RegCloseKey(hKey);
    
    return TRUE;
} // end RegisterString()

BOOL 
RegDelString(
    LPSTR pszKey, 
    LPSTR pszValue
    )
{

    HKEY hKey;
    DWORD dwDisposition;

    // Create the key, if it exists it will be opened
    if (ERROR_SUCCESS != 
        RegCreateKeyEx(
          HKEY_LOCAL_MACHINE,      // handle of an open key 
          pszKey,                  // address of subkey name 
          0,                       // reserved 
          NULL,                    // address of class string 
          REG_OPTION_NON_VOLATILE, // special options flag 
          KEY_ALL_ACCESS,          // desired security access 
          NULL,                    // address of key security structure 
          &hKey,                   // address of buffer for opened handle  
          &dwDisposition))         // address of disposition value buffer 
    {
        return FALSE;
    }

    // Write the value and it's data to the key
    if (ERROR_SUCCESS != 
        RegDeleteValue(
            hKey,                 // handle of key to set value for  
            pszValue))     
    {
        
        RegCloseKey(hKey);
        return FALSE;
    }

    // Close the key
    RegCloseKey(hKey);
    
    return TRUE;
} // end RegDelString()

/// Get string from registry by Key path and Value name
BOOL
GetRegString(
    LPSTR pszKey,
    LPSTR pszValue,
    LPSTR pszData,
    DWORD dwBufSize
    )
{

    HKEY hKey;
    DWORD dwDataSize = dwBufSize;
    DWORD dwValueType = REG_SZ;

    if(!dwBufSize)
        return FALSE;

    RegOpenKeyEx(
       HKEY_LOCAL_MACHINE,    // handle of open key 
       pszKey,                // address of name of subkey to open 
       0,                     // reserved 
       KEY_QUERY_VALUE,       // security access mask 
       &hKey                  // address of handle of open key 
       );

    if (ERROR_SUCCESS != RegQueryValueEx(
        hKey,               // handle of key to query 
        pszValue,           // address of name of value to query 
        0,                  // reserved 
        &dwValueType,       // address of buffer for value type 
        (BYTE *)pszData,    // address of data buffer 
        &dwDataSize         // address of data buffer size 
        )) return FALSE;

    if (pszData[dwDataSize-1] != '\0')<--- Condition 'pszData[dwDataSize-1]!='\0'' is redundant
         pszData[dwDataSize-1] = '\0';<--- Assignment 'pszData[dwDataSize-1]='\0''

    return TRUE;
} // end GetRegString()

BOOL 
RegisterDword(
   LPSTR pszKey, 
   LPSTR pszValue, 
   DWORD dwData
   )
{

    HKEY hKey;
    DWORD dwDisposition;

    // Create the key, if it exists it will be opened
    if (ERROR_SUCCESS != 
        RegCreateKeyEx(
          HKEY_LOCAL_MACHINE,      // handle of an open key 
          pszKey,                  // address of subkey name 
          0,                       // reserved 
          NULL,                    // address of class string 
          REG_OPTION_NON_VOLATILE, // special options flag 
          KEY_ALL_ACCESS,          // desired security access 
          NULL,                    // address of key security structure 
          &hKey,                   // address of buffer for opened handle  
          &dwDisposition))         // address of disposition value buffer 
    {
        return FALSE;
    }

    // Write the value and it's data to the key
    if (ERROR_SUCCESS != 
        RegSetValueEx(
            hKey,                   // handle of key to set value for  
            pszValue,               // address of value to set 
            0,                      // reserved 
            REG_DWORD,              // flag for value type 
            (CONST BYTE *)&dwData,  // address of value data 
            4 ))                    // size of value data 
    {
        
        RegCloseKey(hKey);
        return FALSE;
    }

    // Close the key
    RegCloseKey(hKey);
    
    return TRUE;
} // end RegisterDword()

BOOL
GetRegUlong(
    LPSTR pszKey,
    LPSTR pszValue,
    PULONG pszData
    )
{

    HKEY hKey;
    DWORD dwDataSize = 4;
    DWORD dwValueType = REG_DWORD;
    ULONG origValue = *pszData;

    if(RegOpenKeyEx(
       HKEY_LOCAL_MACHINE,    // handle of open key
       pszKey,                // address of name of subkey to open
       0,                    // reserved
       KEY_QUERY_VALUE,        // security access mask
       &hKey                 // address of handle of open key
        ) != ERROR_SUCCESS) {
        (*pszData) = origValue;
        return FALSE;
    }

    if(RegQueryValueEx(
        hKey,         // handle of key to query
        pszValue,     // address of name of value to query
        0,             // reserved
        &dwValueType,// address of buffer for value type
        (BYTE *)pszData,     // address of data buffer
        &dwDataSize  // address of data buffer size
        ) != ERROR_SUCCESS) {
        (*pszData) = origValue;
    }

    RegCloseKey(hKey);

    return TRUE;
} // end GetRegUlong()

BOOL
SetRegUlong(
  LPSTR pszKey,
  LPSTR pszValue,
  PULONG pszData
  )
{

    HKEY    hKey;
    DWORD   dwDataSize = 4;
    DWORD   dwValueType = REG_DWORD;
    LPVOID  lpMsgBuf;
    LONG    RC;

    if (!(ERROR_SUCCESS == (RC = RegOpenKeyEx(
       HKEY_LOCAL_MACHINE,    // handle of open key
       pszKey,                // address of name of subkey to open
       0,                    // reserved
       KEY_ALL_ACCESS ,        // security access mask
       &hKey                 // address of handle of open key
       )))) {
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
            NULL,
            RC,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL);

        // Display the string.
        MessageBox( NULL, (CCHAR*)lpMsgBuf, "Error", MB_OK|MB_ICONHAND );

        // Free the buffer.
        LocalFree( lpMsgBuf );
        return FALSE;
    }

    if (!(ERROR_SUCCESS == (RC = RegSetValueEx(
        hKey,         // handle of key to query
        pszValue,     // address of name of value to query
        0,             // reserved
        REG_DWORD,// address of buffer for value type
        (BYTE *)pszData,     // address of data buffer
        sizeof(ULONG)  // data buffer size
        )))) {
        FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
            NULL,
            RC,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL);

        // Display the string.
        MessageBox( NULL, (CCHAR*)lpMsgBuf, "Error", MB_OK|MB_ICONHAND );

        // Free the buffer.
        LocalFree( lpMsgBuf );

    }
    RegCloseKey(hKey);
    return TRUE;
} // end SetRegUlong()

BOOL
Privilege(
    LPTSTR pszPrivilege,
    BOOL bEnable
    )
{
   
    HANDLE           hToken;
    TOKEN_PRIVILEGES tp;

    //
    // obtain the token, first check the thread and then the process
    //
    if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, TRUE, &hToken)){
        if (GetLastError() == ERROR_NO_TOKEN) {
            if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
                return FALSE;
        }
        else return FALSE;
    }

    //
    // get the luid for the privilege
    //
    if (!LookupPrivilegeValue(NULL, pszPrivilege, &tp.Privileges[0].Luid))
         return FALSE;

    tp.PrivilegeCount = 1;

    if (bEnable)
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else
        tp.Privileges[0].Attributes = 0;

    //
    // enable or disable the privilege
    //
    if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
        return FALSE;

    if (!CloseHandle(hToken))
        return FALSE;

    return TRUE;
} // end Privilege()

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

 
BOOL IsWow64(VOID)
{
    BOOL bIsWow64 = FALSE;
    LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"),"IsWow64Process");
 
    if (NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
        {
            return FALSE;
        }
    }
    return bIsWow64;
} // end IsWow64()


HANDLE
CreatePublicEvent(
    PWCHAR EventName
    )
{
    SECURITY_DESCRIPTOR sdPublic;
    SECURITY_ATTRIBUTES saPublic;

    InitializeSecurityDescriptor(
        &sdPublic,
        SECURITY_DESCRIPTOR_REVISION
        );

    SetSecurityDescriptorDacl(
        &sdPublic,
        TRUE,
        NULL,
        FALSE
        );

    saPublic.nLength = sizeof(saPublic);
    saPublic.lpSecurityDescriptor = &sdPublic;

    return CreateEventW(
        &saPublic,
        TRUE,
        FALSE,
        EventName);

} // end CreatePublicEvent()

/// Send Device IO Controls to undelaying level via handle
ULONG
UDFPhSendIOCTL(
    IN ULONG IoControlCode,
    IN HANDLE DeviceObject,
    IN PVOID InputBuffer ,
    IN ULONG InputBufferLength,
    OUT PVOID OutputBuffer ,
    IN ULONG OutputBufferLength,
    IN BOOLEAN OverrideVerify,
    IN PVOID Dummy
    )
{
    ULONG real_read;
    ULONG ret;
    LONG offh=0;
    ULONG RC = DeviceIoControl(DeviceObject,IoControlCode,
                                InputBuffer,InputBufferLength,
                                OutputBuffer,OutputBufferLength,
                                &real_read,NULL);

    if (!RC) {
        ret = GetLastError();
    }
    return RC ? 1 : -1;
} // end UDFPhSendIOCTL()

CHAR    RealDeviceName[MAX_PATH+1];

PCHAR
UDFGetDeviceName(
    PCHAR szDeviceName
    )
{
    HANDLE      hDevice;
    WCHAR       DeviceName[MAX_PATH+1];
    ULONG       RC;

    ODS("  UDFGetDeviceName\r\n");
    hDevice = CreateFile(szDeviceName, GENERIC_READ ,
                         FILE_SHARE_READ,
                         NULL,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,  NULL);
    
    if (hDevice == ((HANDLE)-1)) {
        strcpy(RealDeviceName,"");
        return (PCHAR)&RealDeviceName;
    }

    RC = UDFPhSendIOCTL(IOCTL_CDRW_GET_DEVICE_NAME,hDevice,
                        &DeviceName,(MAX_PATH+1)*sizeof(WCHAR),
                        &DeviceName,(MAX_PATH+1)*sizeof(WCHAR), FALSE,NULL);

    if(RC == 1) {
        wcstombs((PCHAR)&RealDeviceName,&DeviceName[1],(USHORT)DeviceName[0]);
        RealDeviceName[(USHORT)DeviceName[0]/sizeof(USHORT)] = '\0';
    } else {
        strcpy(RealDeviceName, szDeviceName+4);
    }

    CloseHandle(hDevice);

    return (PCHAR)(strrchr(RealDeviceName, '\\')+1);
} // end UDFGetDeviceName()

BOOL
GetOptUlong(
    PCHAR Path,
    PCHAR OptName,
    PULONG OptVal
    )
{
    if(!Path) {
        return FALSE;
    }
    if(Path[0] && Path[1] == ':') { 
        CHAR SettingFile[MAX_PATH];
        CHAR Setting[16];

        sprintf(SettingFile, "%s\\%s", Path, UDF_CONFIG_STREAM_NAME);
        GetPrivateProfileString("DiskSettings", OptName, "d", &Setting[0], 10, SettingFile);
        Setting[15]=0;
        if (Setting[0] != 'd') {
            if(Setting[0] == '0' && Setting[1] == 'x') {
                sscanf(Setting+2, "%x", OptVal);
            } else {
                sscanf(Setting, "%d", OptVal);
            }
            return TRUE;
        }
        return FALSE;
    }
    return GetRegUlong(Path, OptName, OptVal);
} // end GetOptUlong()

BOOL
SetOptUlong(
    PCHAR Path,
    PCHAR OptName,
    PULONG OptVal
    )
{
    if(!Path) {
        return FALSE;
    }
    if(Path[0] && Path[1] == ':') { 
        CHAR SettingFile[MAX_PATH];
        CHAR Setting[16];
        if(Path[2] != '\\') {
            sprintf(SettingFile, "%s\\%s", Path, UDF_CONFIG_STREAM_NAME);
        } else {
            sprintf(SettingFile, "%s%s", Path, UDF_CONFIG_STREAM_NAME);
        }
        sprintf(Setting, "%d\n", (*OptVal));
        return WritePrivateProfileString("DiskSettings", OptName, Setting, SettingFile);
    }
    return SetRegUlong(Path, OptName, OptVal);
} // end SetOptUlong()

ULONG
UDFGetOptUlongInherited(
    PCHAR Drive,
    PCHAR OptName,
    PULONG OptVal,
    ULONG CheckDepth
    )
{
    CHAR    LocalPath[1024];
    ULONG   retval = 0;

    ODS("  UDFGetOptUlongInherited\r\n");

    if(GetOptUlong(UDF_SERVICE_PARAM_PATH, OptName, OptVal)) {
        retval = UDF_OPTION_GLOBAL;
    }
    if(CheckDepth <= UDF_OPTION_GLOBAL) {
        return retval;
    }
    strcpy(LocalPath,UDF_SERVICE_PARAM_PATH);
    strcat(LocalPath,"\\");
    strcat(LocalPath,UDFGetDeviceName(Drive));
    if(GetOptUlong(LocalPath, OptName, OptVal)) {
        retval = UDF_OPTION_DEVSPEC;
    }
    if(CheckDepth <= UDF_OPTION_DEVSPEC) {
        return retval;
    }
    if(GetOptUlong(Drive, OptName, OptVal))
        retval = UDF_OPTION_DISKSPEC;
    return retval;
} // end UDFGetOptUlongInherited()

HANDLE
OpenOurVolume(
    PCHAR szDeviceName
    )
{
    HANDLE hDevice;

    // Open device volume
    hDevice = CreateFile(szDeviceName, GENERIC_READ ,
                             FILE_SHARE_READ,
                             NULL,
                             OPEN_EXISTING, 
                             FILE_ATTRIBUTE_NORMAL,  NULL);

    if (hDevice == ((HANDLE)-1)) {   
        hDevice = CreateFile(szDeviceName, GENERIC_READ ,
                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                                 NULL,
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL,  NULL);

    }
    return hDevice;
} // end OpenOurVolume()

ULONG
drv_letter_to_index(
    WCHAR a
    )
{
    if(a >= 'a' && a <= 'z') {
        return a - 'a';
    }
    if(a >= 'A' && a <= 'Z') {
        return a - 'A';
    }
    return -1;
} // end drv_letter_to_index()

/// Start app with desired parameters
DWORD
WINAPI
LauncherRoutine2(
    LPVOID lpParameter
    )
{
    PCHAR ParamStr = (PCHAR)lpParameter;
    STARTUPINFO proc_startup_info;
    PROCESS_INFORMATION proc_info;
    CHAR szLaunchPath[MAX_PATH],ErrMes[50];
    INT  index;
    ULONG MkUdfRetCode;
    CHAR szTemp[256];

    GetRegString(UDF_KEY,"ToolsPath",szLaunchPath, sizeof(szLaunchPath));
    SetCurrentDirectory(szLaunchPath);

    strcat(szLaunchPath,"\\");
    //strcat(szLaunchPath,UDFFMT);
    strcat(szLaunchPath,ParamStr);

    //strcpy(MkUdfStatus,"");

#ifndef TESTMODE
    proc_startup_info.cb = sizeof(proc_startup_info);
    proc_startup_info.lpReserved = 0;
    proc_startup_info.lpReserved2 = 0;
    proc_startup_info.cbReserved2 = 0;
    proc_startup_info.lpDesktop = 0;
    proc_startup_info.lpTitle = 0;
    proc_startup_info.dwFlags = 0;

    proc_startup_info.hStdInput = NULL;
    proc_startup_info.hStdOutput = NULL;
    proc_startup_info.hStdError = NULL;

    if(CreateProcess(NULL, szLaunchPath, 0,0, TRUE, CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
                  0,0, &proc_startup_info, &proc_info)) {

        //hFmtProc[i] = proc_info.hProcess;
        WaitForSingleObject(proc_info.hProcess, -1);
        GetExitCodeProcess(proc_info.hProcess, &MkUdfRetCode);
        index=0;
/*
        while (mkudf_err_msg[index].err_code != 0xffffffff){
          if (mkudf_err_msg[index].err_code == MkUdfRetCode) break;
          index++;
        } 
*/
        //strcpy(MkUdfStatus,mkudf_err_msg[index].err_msg);
        
        CloseHandle(proc_info.hThread);
        CloseHandle(proc_info.hProcess);
    } else {
        strcpy(ErrMes,"Stop: Cannot launch ");
        strcat(ErrMes,szLaunchPath);
        sprintf(szTemp," error %d",GetLastError());
        strcat(ErrMes,szTemp);
        MessageBox(NULL,ErrMes,"UDFFormat",MB_OK | MB_ICONHAND);

    }
#else
        MessageBox(NULL,szLaunchPath,"Message",MB_OK);
        Sleep(500);
        MkUdfRetCode = MKUDF_OK;
//        MkUdfRetCode = MKUDF_FORMAT_REQUIRED;
//        MkUdfRetCode = MKUDF_CANT_BLANK;

        index = 0;
        while (mkudf_err_msg[index].err_code != 0xffffffff){
          if (mkudf_err_msg[index].err_code == MkUdfRetCode) break;
          index++;
        } 
        //strcpy(MkUdfStatus,mkudf_err_msg[index].err_msg);

#endif
    return MkUdfRetCode;
} // end LauncherRoutine2()


#endif // __USER_LIB_CPP__