locked
DIR command in C# looking elsewhere? RRS feed

  • Question

  • Hi. I have installed RSAT tools to utilize "dnscmd.exe" in a C# process.

    I am finding that the C# code cannot find the file.

    C# code:

    Process cmd = new Process();
    cmd.StartInfo.FileName = @"cmd.exe";
    cmd.StartInfo.WorkingDirectory = @"c:\windows\system32\";
    cmd.StartInfo.Arguments = @"/K DIR dnsc*.*";  // <-- This will execute the command and wait to close
    cmd.Start();
    cmd.WaitForExit();

    returns

    07/16/2016  06:43 AM           127,488 dnscmmc.dll
                   1 File(s)        127,488 bytes
                   0 Dir(s)  118,767,841,280 bytes free

    ...whereas the following DOS-batch

    C:\Windows\System32>dir dnsc*.*

    returns

    07/16/2016  06:42 AM            32,768 dnscacheugc.exe
    09/23/2017  11:10 PM           428,544 dnscmd.exe
    07/16/2016  06:42 AM           134,656 dnscmmc.dll
                   3 File(s)        595,968 bytes
                   0 Dir(s)  118,767,841,280 bytes free

    Why can't C# see the "dnscmd.exe" file, and why are the "dnscmmc.dll" files different?

    Thursday, January 4, 2018 1:01 PM

Answers

  • The purpose is just to disable redirection if the process is in Wow64

    A better code would be :

    bool bWow64 = false;
    IsWow64Process(Process.GetCurrentProcess().Handle, out bWow64);
    if (bWow64)
    {
        IntPtr OldValue = IntPtr.Zero;
        bool bRet = Wow64DisableWow64FsRedirection(out OldValue);
    }

    with

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);

    • Proposed as answer by valat Friday, January 5, 2018 10:52 AM
    • Marked as answer by JFoushee Thursday, February 8, 2018 3:11 PM
    Thursday, January 4, 2018 6:40 PM
  • Hello JFoushee,

    The System32 is a special folder that contains 32 bit program and 64 bit program. which is design for 32-bit and 64-bit compatibility considerations. In fact the 32 bit program can be ran at 64 bit windows OS because  some dlls of SysWOW64folder.

    As for your circumstance, you just set your program run at x64 platform.

    click solution platforms >> choose Configuration Manager >> select your project and set platform as X64 >> Run your program.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 5, 2018 10:44 AM

All replies

  • Add just before :

    IntPtr OldValue = IntPtr.Zero;
    bool bRet = Wow64DisableWow64FsRedirection(out OldValue);

    Declaration :

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool Wow64DisableWow64FsRedirection(out IntPtr OldValue);

    • Edited by Castorix31 Thursday, January 4, 2018 6:40 PM
    Thursday, January 4, 2018 1:53 PM
  • So it turned out that the class was AnyCPU, running in a harness specified at 32-bit.

    Your reply interests me.. what would you have said if I informed you the value of bRet?

    Thursday, January 4, 2018 6:11 PM
  • The purpose is just to disable redirection if the process is in Wow64

    A better code would be :

    bool bWow64 = false;
    IsWow64Process(Process.GetCurrentProcess().Handle, out bWow64);
    if (bWow64)
    {
        IntPtr OldValue = IntPtr.Zero;
        bool bRet = Wow64DisableWow64FsRedirection(out OldValue);
    }

    with

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);

    • Proposed as answer by valat Friday, January 5, 2018 10:52 AM
    • Marked as answer by JFoushee Thursday, February 8, 2018 3:11 PM
    Thursday, January 4, 2018 6:40 PM
  • Hello JFoushee,

    The System32 is a special folder that contains 32 bit program and 64 bit program. which is design for 32-bit and 64-bit compatibility considerations. In fact the 32 bit program can be ran at 64 bit windows OS because  some dlls of SysWOW64folder.

    As for your circumstance, you just set your program run at x64 platform.

    click solution platforms >> choose Configuration Manager >> select your project and set platform as X64 >> Run your program.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 5, 2018 10:44 AM
  • Hello JFoushee,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:34 PM