Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[Snippet] Get Windows directory

Here's a snippet to get the Windows directory using the Windows API. Easy to use.

Code
[DllImport("kernel32.dll", BestFitMapping = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U4)]
        private static extern uint GetWindowsDirectory(StringBuilder builder, [MarshalAs(UnmanagedType.U4)] uint size);

        private static DirectoryInfo GetWindowsDirectory()
        {
            StringBuilder builder = new StringBuilder(260);
            uint realLength = GetWindowsDirectory( builder, (uint)builder.Capacity);
            string windowsDir = builder.ToString();
            return new DirectoryInfo(windowsDir);
        }
[Image: Syr3DlV.png]
Why use WinAPI for this at all?

Code
new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
(Dec 1, 2018 08:35 AM)Vuln Wrote:  Why use WinAPI for this at all?

Code
new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows));

^ Exactly

Additionally if you actually read the documentation it says size should be MAX_PATH, and that is not 255... It's 260.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Thanks for the input, changed accordingly
[Image: Syr3DlV.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by еlixirSep 9, 2022 06:31 AM 0 132 Sep 9, 2022 06:31 AM
Last Post: еlixir
[Shell] by Emre_Jan 12, 2021 11:16 PM 0 141 Jan 12, 2021 11:16 PM
Last Post: Emre_
[Python] by datguy.devNov 28, 2019 10:37 PM 4 299 Nov 28, 2019 10:37 PM
Last Post: Eternal Darkness