8 Mart 2020 Pazar

NtQuerySystemTime metodu

Giriş
NtQuerySystemTime tam olarak dokümante edilmiş bir fonksiyon değil ve msdn'de de ileride değişebileceğine dair uyarı var.

GetSystemTimeAsFileTime ile aynı işi yapar ancak FILETIME türünden bir structure dönmek yerine onun 64 bite dönüştürülmüş halini verir.

6 Mart 2020 Cuma

Windows Batch Kodlama - ERRORLEVEL gömülü değişkeni

Giriş
Açıklaması şöyle.
A pseudo environment variable named errorlevel stores the exit code:
Örnek
Şöyle yaparız. EQU 0 'dan sonra gelen açılış parantezi için bir boşluk olmalı
@echo off
my_nify_exe.exe
if %ERRORLEVEL% EQU 0 (
   echo Success
) else (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

4 Mart 2020 Çarşamba

Windows runas komutu

Örnek
Kendimiz bir servis geliştirmiş olalım ve servis içinden bir bat dosyasını çalıştırmak isteyelim. Bunu denemek için şöyle yaparız.
C:\> runas /user:DOMAIN\service_acct "cmd.exe /C C:\foo.bat"

3 Mart 2020 Salı

CreateProcess metodu

Giriş
Açıklaması şöyle
CreateProcess creates a process that runs as the same user as the parent application and it runs in the same session. You must use CreateProcessAsUser/CreateProcessWithLogonW/ CreateProcessWithTokenW to create a process as another user.
Örnek
Şöyle yaparız
CreateProcess(NULL,   // No module name (use command line)
    L"C:\\Windows\\System32\\cmd.exe /C myfile.bat", // Call cmd.exe with /C flag
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    CREATE_NO_WINDOW,              // Use CREATE_NO_WINDOW!!!
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
Hata kodu varsa yakalamak için şöyle yaparız
if( !CreateProcessA(...) )
{
  char msg[100];
  sprintf( msg, "CreateProcess() failed: %d", GetLastError() );
  OutputDebugStringA( msg );
}
Yeni uygulamanın bitmesini beklemek için şöyle yaparız
::WaitForSingleObject(pi.hProcess, INFINITE);

2 Mart 2020 Pazartesi

Windows Batch For Döngüsü

Giriş
Döngü için tanımlanan %%i değişkeni eğer batch dosyası içindeyse çift yüzde karakteri ile kullanılır. Komut satırında ise tek % karakteri ile kullanılır.

Açıklaması şöyle
Be careful however as setting and using variables inside of a loop may require delayed expansion
1. Tilde Magic
For döngülerinde bazen tilde karakteri görülüyor. Açıklaması şöyle
The tilde (~) sign is used in different ways in batch files:

- Argument quote removal. A tilde sign before an command-line argument (such as "%~1") indicates to remove the surrounding quotes from the parameter. Such if the value for %1 is "Hi" then %~1 will expand to only Hi.

- Substring processing. There is a way to extract substring from a string variable in batch file. The syntax is: %variable:~num_chars_skip,num_chars_keep%. Num chars skip means the point where to start in the string, or to exclude how much characters preceding the string variable. And num chars to keep indicates the number of characters after the start point. Num chars to keep is optional, but first is mandatory. If num chars to keep is not specified, only that num chars to keep'th character will be parsed.
Yani birinci amacı parametredeki quote karakterini kaldırmak

Örnek  - Argument Quote Removal
Elimizde şöyle bir kod olsun
@Echo off
For %%A IN (
    "this is a sentence"
    two words
    "some words"
) Do (
    Echo with tilde: "%%~A"
    Echo w/o  tilde: "%%A"
    Echo:
)
Çıktı olarak şunu alırız. Çıktıda "some words" parametresinin tilde ile gösterilirken quote karakterinin çıkarıldığı görülebilir.
> SU1397704.cmd
with tilde: "this is a sentence"
w/o  tilde: ""this is a sentence""

with tilde: "two"
w/o  tilde: "two"

with tilde: "words"
w/o  tilde: "words"

with tilde: "some words"
w/o  tilde: ""some words""

2. Magic Variables
For döngülerinde magic değişkenler de görülebilir. Açıklaması şöyle
- The magic variables %n contains the arguments used to invoke the file: 
- %0 is the path to the bat-file itself
- %1 is the first argument after, %2 is the second and so on.

Since the arguments are often file paths, there is some additional syntax to extract parts of the path. 
- ~d is drive
- ~p is the path (without drive), 
- ~n is the file name. They can be combined so ~dp is drive+path.

- %~dp0 is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.

You can also get other kinds of meta info about the file: 
- ~t is the timestamp, 
- ~z is the size.
Örnek
Şöyle yaparız. Burada komut satırında çalışıldığı için tek % karakteri var. Batch dosyası olsaydı çift %% kullanmak gerekirdi
for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"
Dosyaları şöyle rename eder. %~na ile a değişkenindeki dosya ismi alınır. %~xa ile a değişkenindeki uzantı alınır
File 1.txt  ->  File 1 version 1.txt
File 2.txt  ->  File 2 version 1.txt
File 3.txt  ->  File 3 version 1.txt
File 4.txt  ->  File 4 version 1.txt
...
3. @ Karakteri
Açıklaması şöyle. Eğer @echo off yapılmamışsa, normalde çalıştırılan komutu görürüz. Bunu da görmemek için komutun başına @ karakteri konulur
The @ symbol tells the command processor to be less verbose; to only show the output of the command without showing it being executed or any prompts associated with the execution. When used it is prepended to the beginning of the command, it is not necessary to leave a space between the "@" and the command.

When "echo" is set to "off" it is not necessary to use "@" because setting "echo" to "off" causes this behavior to become automatic. "Echo" is usually set to "on" by default when the execution of a script begins. This is the reason "@echo off" is commonly used, to turn echo off without displaying the act of turning it off.
4. Seçenekler
/D seçeneği - Directory
Örnek
C:\test altındaki ismi target olan dizinleri tamamıyla (alt dizinleri dahil) silmek için şöyle yaparız
for /d /r c:\test %%d in (target) do @IF EXIST "%%d" rd /s /q %%d
/F seçeneği - File Contents
Hem bir dosyayı hem de bir başka uygulamanın çıktısını okumak için de kullanılır

Örnek
Şöyle yaparız.
for /F %%x in (Test.txt) do @echo %%x
Örnek
Şöyle yaparız.
for /F %%I in (Test.txt) do (
   echo %%i
   echo "%%i"
   echo etc
)
/L seçeneği - Range of Numbers
Örnek
Şöyle yaparız. 1'den başla. 1 artır ve 94'e kadar (dahil) git anlamına gelir.
@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,94) do (
  set "NUM=00%%N"
  set "DIRNAME=ch.!NUM:~-3!
  md !DIRNAME!
)
Örnek
Komut satırında tek % ile kullanarak şöyle yaparız
for /L %i in (0,1,60) do type source.txt>>dest.txt
Örnek
Batch dosyasında kullanmak için şöyle yaparız
for /L %%i in (0,1,%3) do type %1>>%2

:: %1 source file
:: %2 destination file
:: %3 number of inserts

Windows Batch CALL komutu - Başka Bir Bat Dosyasını Çalıştırır

Giriş
Açıklaması şöyle. Yani aslında sadece bit bat dosyasından, başka bir bat dosyasını çalıştırmak için kullanılmalı.
Calls one batch program from another, calls a subprogram within a single batch program, or, as an undocumented behavior, starts a program. In particular, suspends the execution of the caller, starts executing the callee, and resumes the execution of the caller if and when the callee finishes execution.
...

Beware that calling a batch program from a batch without using the call keyword results in the execution never returning to the caller once the callee finishes.

The callee inherits environment variables of the caller, and unless the callee prevents that via SETLOCAL, changes made by the callee to environment variables become visible to the caller once it resumes execution.
Örnek
Şöyle yaparız.
@echo off

cd C:\Program Files (x86)\Project
CALL init_env.bat
Örnek
Şöyle yaparız. command1, command2.cmd ve command3 sırayla çalışır
command1 & call command2.cmd & command3

1 Mart 2020 Pazar

NTFS

Şifreleme
NTFS kendiliğinden şifreli çalışmaz. Açıklaması şöyle.
By default, no files are encrypted, but encryption can be enabled by users on a per-file, per-directory, or per-drive basis.