1 ////////////////////////////////////////////////////////////////////
2 // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
4 ////////////////////////////////////////////////////////////////////
6 /*************************************************************************
10 * Module: User-mode library
12 * Description: common useful user-mode functions
16 *************************************************************************/
19 #ifndef __USER_LIB_CPP__
20 #define __USER_LIB_CPP__
24 TCHAR
* MediaTypeStrings
[] =
44 void * __cdecl
mymemchr (
50 while ( cnt
&& (*(unsigned char *)buf
!= (unsigned char)chr
) ) {
51 buf
= (unsigned char *)buf
+ 1;
55 return(cnt
? (void *)buf
: NULL
);
58 char * __cdecl
mystrrchr(
63 char *start
= (char *)string
;
67 while (--string
!= start
&& *string
!= (char)ch
) {;}
69 if (*string
== (char)ch
) {
70 return( (char *)string
);
76 char * __cdecl
mystrchr(
81 while (*string
!= (char)ch
&& *string
!= '\0' ) {
85 if (*string
== (char)ch
) {
86 return( (char *)string
);
99 attr
= GetFileAttributes((LPTSTR
)path
);
100 if (attr
== 0xffffffff) {
125 if (!HIWORD(pszTitle
)) {
126 LoadString(hInst
, LOWORD(pszTitle
), szTitle
, sizeof(szTitle
)/sizeof(szTitle
[0]));
130 if (!HIWORD(pszFormat
)) {
131 // Allow this to be a resource ID
132 LoadString(hInst
, LOWORD(pszFormat
), szFormat
, sizeof(szFormat
)/sizeof(szFormat
[0]));
133 pszFormat
= szFormat
;
136 va_start(ArgList
, fuStyle
);
137 fOk
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
138 | FORMAT_MESSAGE_FROM_STRING
,
139 pszFormat
, 0, 0, (LPTSTR
)&pszMessage
, 0, &ArgList
);
143 if (fOk
&& pszMessage
) {
144 result
= MessageBox(hWnd
, pszMessage
, pszTitle
, fuStyle
| MB_SETFOREGROUND
);
145 LocalFree(pszMessage
);
151 } // end MyMessageBox()
154 /// Return service status by service name.
160 SC_HANDLE schService
;
162 SERVICE_STATUS ssStatus
;
163 JS_SERVICE_STATE return_value
;
164 SC_HANDLE schSCManager
;
166 schSCManager
= OpenSCManager(
167 NULL
, // machine (NULL == local)
168 NULL
, // database (NULL == default)
169 SC_MANAGER_ALL_ACCESS
// access required
172 schSCManager
= OpenSCManager(
173 NULL
, // machine (NULL == local)
174 NULL
, // database (NULL == default)
175 SC_MANAGER_CONNECT
// access required
179 return JS_ERROR_STATUS
;
180 schService
= OpenService(schSCManager
, ServiceName
, SERVICE_QUERY_STATUS
);
183 CloseServiceHandle(schSCManager
);
184 if (RC
== ERROR_SERVICE_DOES_NOT_EXIST
) return JS_SERVICE_NOT_PRESENT
;
185 else return JS_ERROR_STATUS
;
187 QueryServiceStatus(schService
, &ssStatus
);
188 if(ssStatus
.dwCurrentState
== SERVICE_RUNNING
) {
189 return_value
= JS_SERVICE_RUNNING
;
191 return_value
= JS_SERVICE_NOT_RUNNING
;
193 CloseServiceHandle(schService
);
194 CloseServiceHandle(schSCManager
);
196 } // end ServiceInfo()
203 char CdromUpperFilters
[1024];
206 if (LOBYTE(LOWORD(GetVersion())) < 5) {
210 if (GetRegString(CDROM_CLASS_PATH
,REG_UPPER_FILTER_NAME
,&CdromUpperFilters
[0],arraylen(CdromUpperFilters
))) {
211 char *token
= &CdromUpperFilters
[0];
214 if (!strcmp(token
,CDRW_SERVICE
)) {
218 token
+= strlen(token
)+1;
221 memcpy(token
,CDRW_SERVICE
,sizeof(CDRW_SERVICE
));
222 *(token
+sizeof(CDRW_SERVICE
)) = '\0';
223 *(token
+sizeof(CDRW_SERVICE
)+1) = '\0';
225 RegisterString(CDROM_CLASS_PATH
,REG_UPPER_FILTER_NAME
,&CdromUpperFilters
[0],TRUE
,token
-&CdromUpperFilters
[0]+sizeof(CDRW_SERVICE
)+1);
230 memcpy(CdromUpperFilters
,CDRW_SERVICE
,sizeof(CDRW_SERVICE
));
231 CdromUpperFilters
[sizeof(CDRW_SERVICE
)] = '\0';
232 CdromUpperFilters
[sizeof(CDRW_SERVICE
)+1] = '\0';
234 RegisterString(CDROM_CLASS_PATH
,REG_UPPER_FILTER_NAME
,&CdromUpperFilters
[0],TRUE
,sizeof(CDRW_SERVICE
)+1);
240 } // end CheckCdrwFilter()
255 // Create the key, if it exists it will be opened
258 HKEY_LOCAL_MACHINE
, // handle of an open key
259 pszKey
, // address of subkey name
261 NULL
, // address of class string
262 REG_OPTION_NON_VOLATILE
, // special options flag
263 KEY_ALL_ACCESS
, // desired security access
264 NULL
, // address of key security structure
265 &hKey
, // address of buffer for opened handle
266 &dwDisposition
)) // address of disposition value buffer
271 // Write the value and it's data to the key
274 hKey
, // handle of key to set value for
275 pszValue
, // address of value to set
277 MultiSz
? REG_MULTI_SZ
: REG_SZ
, // flag for value type
278 (CONST BYTE
*)pszData
, // address of value data
279 MultiSz
? size
: strlen(pszData
) )) // size of value data
290 } // end RegisterString()
302 // Create the key, if it exists it will be opened
305 HKEY_LOCAL_MACHINE
, // handle of an open key
306 pszKey
, // address of subkey name
308 NULL
, // address of class string
309 REG_OPTION_NON_VOLATILE
, // special options flag
310 KEY_ALL_ACCESS
, // desired security access
311 NULL
, // address of key security structure
312 &hKey
, // address of buffer for opened handle
313 &dwDisposition
)) // address of disposition value buffer
318 // Write the value and it's data to the key
321 hKey
, // handle of key to set value for
333 } // end RegDelString()
335 /// Get string from registry by Key path and Value name
346 DWORD dwDataSize
= dwBufSize
;
347 DWORD dwValueType
= REG_SZ
;
353 HKEY_LOCAL_MACHINE
, // handle of open key
354 pszKey
, // address of name of subkey to open
356 KEY_QUERY_VALUE
, // security access mask
357 &hKey
// address of handle of open key
360 if (ERROR_SUCCESS
!= RegQueryValueEx(
361 hKey
, // handle of key to query
362 pszValue
, // address of name of value to query
364 &dwValueType
, // address of buffer for value type
365 (BYTE
*)pszData
, // address of data buffer
366 &dwDataSize
// address of data buffer size
369 if (pszData
[dwDataSize
-1] != '\0')
370 pszData
[dwDataSize
-1] = '\0';
373 } // end GetRegString()
386 // Create the key, if it exists it will be opened
389 HKEY_LOCAL_MACHINE
, // handle of an open key
390 pszKey
, // address of subkey name
392 NULL
, // address of class string
393 REG_OPTION_NON_VOLATILE
, // special options flag
394 KEY_ALL_ACCESS
, // desired security access
395 NULL
, // address of key security structure
396 &hKey
, // address of buffer for opened handle
397 &dwDisposition
)) // address of disposition value buffer
402 // Write the value and it's data to the key
405 hKey
, // handle of key to set value for
406 pszValue
, // address of value to set
408 REG_DWORD
, // flag for value type
409 (CONST BYTE
*)&dwData
, // address of value data
410 4 )) // size of value data
421 } // end RegisterDword()
432 DWORD dwDataSize
= 4;
433 DWORD dwValueType
= REG_DWORD
;
434 ULONG origValue
= *pszData
;
437 HKEY_LOCAL_MACHINE
, // handle of open key
438 pszKey
, // address of name of subkey to open
440 KEY_QUERY_VALUE
, // security access mask
441 &hKey
// address of handle of open key
442 ) != ERROR_SUCCESS
) {
443 (*pszData
) = origValue
;
448 hKey
, // handle of key to query
449 pszValue
, // address of name of value to query
451 &dwValueType
,// address of buffer for value type
452 (BYTE
*)pszData
, // address of data buffer
453 &dwDataSize
// address of data buffer size
454 ) != ERROR_SUCCESS
) {
455 (*pszData
) = origValue
;
461 } // end GetRegUlong()
472 DWORD dwDataSize
= 4;
473 DWORD dwValueType
= REG_DWORD
;
477 if (!(ERROR_SUCCESS
== (RC
= RegOpenKeyEx(
478 HKEY_LOCAL_MACHINE
, // handle of open key
479 pszKey
, // address of name of subkey to open
481 KEY_ALL_ACCESS
, // security access mask
482 &hKey
// address of handle of open key
485 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
488 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
493 // Display the string.
494 MessageBox( NULL
, (CCHAR
*)lpMsgBuf
, "Error", MB_OK
|MB_ICONHAND
);
497 LocalFree( lpMsgBuf
);
501 if (!(ERROR_SUCCESS
== (RC
= RegSetValueEx(
502 hKey
, // handle of key to query
503 pszValue
, // address of name of value to query
505 REG_DWORD
,// address of buffer for value type
506 (BYTE
*)pszData
, // address of data buffer
507 sizeof(ULONG
) // data buffer size
510 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
513 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
518 // Display the string.
519 MessageBox( NULL
, (CCHAR
*)lpMsgBuf
, "Error", MB_OK
|MB_ICONHAND
);
522 LocalFree( lpMsgBuf
);
527 } // end SetRegUlong()
540 // obtain the token, first check the thread and then the process
542 if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, TRUE
, &hToken
)){
543 if (GetLastError() == ERROR_NO_TOKEN
) {
544 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
))
551 // get the luid for the privilege
553 if (!LookupPrivilegeValue(NULL
, pszPrivilege
, &tp
.Privileges
[0].Luid
))
556 tp
.PrivilegeCount
= 1;
559 tp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
561 tp
.Privileges
[0].Attributes
= 0;
564 // enable or disable the privilege
566 if (!AdjustTokenPrivileges(hToken
, FALSE
, &tp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0))
569 if (!CloseHandle(hToken
))
575 typedef BOOL (WINAPI
*LPFN_ISWOW64PROCESS
) (HANDLE
, PBOOL
);
580 BOOL bIsWow64
= FALSE
;
581 LPFN_ISWOW64PROCESS fnIsWow64Process
= (LPFN_ISWOW64PROCESS
)GetProcAddress(GetModuleHandle("kernel32"),"IsWow64Process");
583 if (NULL
!= fnIsWow64Process
)
585 if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64
))
599 SECURITY_DESCRIPTOR sdPublic
;
600 SECURITY_ATTRIBUTES saPublic
;
602 InitializeSecurityDescriptor(
604 SECURITY_DESCRIPTOR_REVISION
607 SetSecurityDescriptorDacl(
614 saPublic
.nLength
= sizeof(saPublic
);
615 saPublic
.lpSecurityDescriptor
= &sdPublic
;
623 } // end CreatePublicEvent()
625 /// Send Device IO Controls to undelaying level via handle
628 IN ULONG IoControlCode
,
629 IN HANDLE DeviceObject
,
630 IN PVOID InputBuffer
,
631 IN ULONG InputBufferLength
,
632 OUT PVOID OutputBuffer
,
633 IN ULONG OutputBufferLength
,
634 IN BOOLEAN OverrideVerify
,
641 ULONG RC
= DeviceIoControl(DeviceObject
,IoControlCode
,
642 InputBuffer
,InputBufferLength
,
643 OutputBuffer
,OutputBufferLength
,
647 ret
= GetLastError();
650 } // end UDFPhSendIOCTL()
652 CHAR RealDeviceName
[MAX_PATH
+1];
660 WCHAR DeviceName
[MAX_PATH
+1];
663 ODS(" UDFGetDeviceName\r\n");
664 hDevice
= CreateFile(szDeviceName
, GENERIC_READ
,
668 FILE_ATTRIBUTE_NORMAL
, NULL
);
670 if (hDevice
== ((HANDLE
)-1)) {
671 strcpy(RealDeviceName
,"");
672 return (PCHAR
)&RealDeviceName
;
675 RC
= UDFPhSendIOCTL(IOCTL_CDRW_GET_DEVICE_NAME
,hDevice
,
676 &DeviceName
,(MAX_PATH
+1)*sizeof(WCHAR
),
677 &DeviceName
,(MAX_PATH
+1)*sizeof(WCHAR
), FALSE
,NULL
);
680 wcstombs((PCHAR
)&RealDeviceName
,&DeviceName
[1],(USHORT
)DeviceName
[0]);
681 RealDeviceName
[(USHORT
)DeviceName
[0]/sizeof(USHORT
)] = '\0';
683 strcpy(RealDeviceName
, szDeviceName
+4);
686 CloseHandle(hDevice
);
688 return (PCHAR
)(strrchr(RealDeviceName
, '\\')+1);
689 } // end UDFGetDeviceName()
701 if(Path
[0] && Path
[1] == ':') {
702 CHAR SettingFile
[MAX_PATH
];
705 sprintf(SettingFile
, "%s\\%s", Path
, UDF_CONFIG_STREAM_NAME
);
706 GetPrivateProfileString("DiskSettings", OptName
, "d", &Setting
[0], 10, SettingFile
);
708 if (Setting
[0] != 'd') {
709 if(Setting
[0] == '0' && Setting
[1] == 'x') {
710 sscanf(Setting
+2, "%x", OptVal
);
712 sscanf(Setting
, "%d", OptVal
);
718 return GetRegUlong(Path
, OptName
, OptVal
);
719 } // end GetOptUlong()
731 if(Path
[0] && Path
[1] == ':') {
732 CHAR SettingFile
[MAX_PATH
];
734 if(Path
[2] != '\\') {
735 sprintf(SettingFile
, "%s\\%s", Path
, UDF_CONFIG_STREAM_NAME
);
737 sprintf(SettingFile
, "%s%s", Path
, UDF_CONFIG_STREAM_NAME
);
739 sprintf(Setting
, "%d\n", (*OptVal
));
740 return WritePrivateProfileString("DiskSettings", OptName
, Setting
, SettingFile
);
742 return SetRegUlong(Path
, OptName
, OptVal
);
743 } // end SetOptUlong()
746 UDFGetOptUlongInherited(
753 CHAR LocalPath
[1024];
756 ODS(" UDFGetOptUlongInherited\r\n");
758 if(GetOptUlong(UDF_SERVICE_PARAM_PATH
, OptName
, OptVal
)) {
759 retval
= UDF_OPTION_GLOBAL
;
761 if(CheckDepth
<= UDF_OPTION_GLOBAL
) {
764 strcpy(LocalPath
,UDF_SERVICE_PARAM_PATH
);
765 strcat(LocalPath
,"\\");
766 strcat(LocalPath
,UDFGetDeviceName(Drive
));
767 if(GetOptUlong(LocalPath
, OptName
, OptVal
)) {
768 retval
= UDF_OPTION_DEVSPEC
;
770 if(CheckDepth
<= UDF_OPTION_DEVSPEC
) {
773 if(GetOptUlong(Drive
, OptName
, OptVal
))
774 retval
= UDF_OPTION_DISKSPEC
;
776 } // end UDFGetOptUlongInherited()
785 // Open device volume
786 hDevice
= CreateFile(szDeviceName
, GENERIC_READ
,
790 FILE_ATTRIBUTE_NORMAL
, NULL
);
792 if (hDevice
== ((HANDLE
)-1)) {
793 hDevice
= CreateFile(szDeviceName
, GENERIC_READ
,
794 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
797 FILE_ATTRIBUTE_NORMAL
, NULL
);
801 } // end OpenOurVolume()
808 if(a
>= 'a' && a
<= 'z') {
811 if(a
>= 'A' && a
<= 'Z') {
815 } // end drv_letter_to_index()
817 /// Start app with desired parameters
824 PCHAR ParamStr
= (PCHAR
)lpParameter
;
825 STARTUPINFO proc_startup_info
;
826 PROCESS_INFORMATION proc_info
;
827 CHAR szLaunchPath
[MAX_PATH
],ErrMes
[50];
832 GetRegString(UDF_KEY
,"ToolsPath",szLaunchPath
, sizeof(szLaunchPath
));
833 SetCurrentDirectory(szLaunchPath
);
835 strcat(szLaunchPath
,"\\");
836 //strcat(szLaunchPath,UDFFMT);
837 strcat(szLaunchPath
,ParamStr
);
839 //strcpy(MkUdfStatus,"");
842 proc_startup_info
.cb
= sizeof(proc_startup_info
);
843 proc_startup_info
.lpReserved
= 0;
844 proc_startup_info
.lpReserved2
= 0;
845 proc_startup_info
.cbReserved2
= 0;
846 proc_startup_info
.lpDesktop
= 0;
847 proc_startup_info
.lpTitle
= 0;
848 proc_startup_info
.dwFlags
= 0;
850 proc_startup_info
.hStdInput
= NULL
;
851 proc_startup_info
.hStdOutput
= NULL
;
852 proc_startup_info
.hStdError
= NULL
;
854 if(CreateProcess(NULL
, szLaunchPath
, 0,0, TRUE
, CREATE_NO_WINDOW
| NORMAL_PRIORITY_CLASS
,
855 0,0, &proc_startup_info
, &proc_info
)) {
857 //hFmtProc[i] = proc_info.hProcess;
858 WaitForSingleObject(proc_info
.hProcess
, -1);
859 GetExitCodeProcess(proc_info
.hProcess
, &MkUdfRetCode
);
862 while (mkudf_err_msg[index].err_code != 0xffffffff){
863 if (mkudf_err_msg[index].err_code == MkUdfRetCode) break;
867 //strcpy(MkUdfStatus,mkudf_err_msg[index].err_msg);
869 CloseHandle(proc_info
.hThread
);
870 CloseHandle(proc_info
.hProcess
);
872 strcpy(ErrMes
,"Stop: Cannot launch ");
873 strcat(ErrMes
,szLaunchPath
);
874 sprintf(szTemp
," error %d",GetLastError());
875 strcat(ErrMes
,szTemp
);
876 MessageBox(NULL
,ErrMes
,"UDFFormat",MB_OK
| MB_ICONHAND
);
880 MessageBox(NULL
,szLaunchPath
,"Message",MB_OK
);
882 MkUdfRetCode
= MKUDF_OK
;
883 // MkUdfRetCode = MKUDF_FORMAT_REQUIRED;
884 // MkUdfRetCode = MKUDF_CANT_BLANK;
887 while (mkudf_err_msg
[index
].err_code
!= 0xffffffff){
888 if (mkudf_err_msg
[index
].err_code
== MkUdfRetCode
) break;
891 //strcpy(MkUdfStatus,mkudf_err_msg[index].err_msg);
895 } // end LauncherRoutine2()
898 #endif // __USER_LIB_CPP__