Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 51
  • Last Modified:

c# can't call wbemtest.exe

Hi,

Why does my code not work. I know Wbemtest.exe is in the path specified.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace WTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Process my_process = new Process();

                my_process.StartInfo.FileName = @"C:\Windows\System32\wbem\wbemtest.exe";

                my_process.Start();
            }

            // Generates error: The system cannot find the file specified

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Open in new window

wbem_error.png
0
whorsfall
Asked:
whorsfall
  • 9
  • 6
  • 6
  • +1
2 Solutions
 
AndyAinscowCommented:
Is this a 64 bit windows OP system?

Windows does some things in the background that a path you see doesn't actually exist, it is an alias of something else.  Unfortunately this translation isn't applied when you try to do some things from code.
0
 
whorsfallAuthor Commented:
Hi,

Thanks for your response. Yes I am doing on Windows 10 x64 + Windows server 2012 R2 x64.

Thanks,

Ward
0
 
AndyAinscowCommented:
Use this built in functionality to find the paths to various OP system folders.
https://msdn.microsoft.com/en-us/library/14tx8hby(v=vs.110).aspx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
whorsfallAuthor Commented:
Hi AndyAinscow,

Thanks for that.

I tried below and got the same result - is there something I am missing?

Thanks,

Ward

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace WTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string system_folder_path = Environment.GetFolderPath(Environment.SpecialFolder.System);

                string wbem_path = system_folder_path + "\\wbemtest.exe";

                Process my_process = new Process();

                my_process.StartInfo.FileName = wbem_path;

                my_process.Start();
            }

            // Generates error: The system cannot find the file specified

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Open in new window

0
 
AndyAinscowCommented:
>>               my_process.StartInfo.FileName = @"C:\Windows\System32\wbem\wbemtest.exe";
Above is from your question, are you missing the wbem directory in your latest code
0
 
whorsfallAuthor Commented:
Hi AndyAinscow,

Thanks for your help being great so far. Still no go.

I have double checked my path. So in the file system it via dos it finds it. But the C# code does not.
So there is some aliasing going on. if I fire up the program in the OS then you can see what it is saying.

Thanks,

Ward

Here is my current code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace WTest1
{
    class Program
    {
        static void Main(string[] args)
        {

            string path;

            path = @"C:\Windows\System32\wbem\wbemtest.exe";

            string special_path = Environment.GetFolderPath(Environment.SpecialFolder.System);

            Console.WriteLine("Special Path: {0}", special_path);
            Console.WriteLine("File Exists : {0}", File.Exists(path));

            /* Output:

            Special Path: C:\WINDOWS\system32
            File Exists : False

            */

            try
            {
                Process my_process = new Process();

                my_process.StartInfo.FileName = path;

                my_process.StartInfo.WorkingDirectory = @"C:\Windows\System32\wbem";

                my_process.Start();
            }

            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Exception: {0}", ex.Message));
            }


        }

    }
}

Open in new window

wbem_info.png
0
 
AndyAinscowCommented:
Wierd.  I have the same problem here.
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
I think it's security. I am working on mine now. I'm getting "failed to enumerate objects in the container. access is denied" when attempting to modify the security properties on the wbem folder.

I'm looking at how to fix it now. If you can't enumerate the files, you won't be able to "find" it, I think.
0
 
whorsfallAuthor Commented:
Hi,

Thanks it is really strange issue :)

BTW: Powershell can start it fine - I wonder why C# is different.

start-process "C:\Windows\system32\wbem\wbemtest.exe"

Thanks,

Ward
0
 
it_saigeDeveloperCommented:
I believe that Daniel is on the right track.  You probably need to elevate your rights, try starting it with the 'runas' verb specified; e.g. -
using System;
using System.Diagnostics;

namespace EE_Q28966771
{
	class Program
	{
		static void Main(string[] args)
		{
			Process wbem = default(Process);
			try
			{
				wbem = new Process();
				wbem.StartInfo.FileName = @"C:\Windows\System32\wbem\wbemtest.exe";
				wbem.StartInfo.Verb = "runas";
				wbem.Start();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Exception - {0} [{1}]", ex, ex.Message);
			}
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following output for me -Capture.JPG-saige-
0
 
AndyAinscowCommented:
I've tried running the exe on my system from windows explorer.  I have the same error (can't find file) both for running normally and running as administrator.
Adding the Verb option (see previous comment) makes no difference.
0
 
it_saigeDeveloperCommented:
@Andy,

Hmmmm...  If you can't even start it from explorer then you may potentially have some DCOM issues.  Do you have any entries in your systems event viewer related to the issue?

-saige-
0
 
AndyAinscowCommented:
Having reread my comment I had better clarify.
By 'the exe' I am referring to the compiled app created by the code above.  In the console window appears the can't find file message.

The target app, wbemtest.exe, I can start without problems via double click in windows explorer.
0
 
it_saigeDeveloperCommented:
@Andy,

Thanks for the clarification.  I was starting to get worried about your computer.  ;)

What OS version are we running here where it is not working.  On mine, I can remove the verb and still get the process (wbemtest.exe) to start without an exception being issued.  I am using Windows 10, I also am an Administrator on the computer.

-saige-
0
 
AndyAinscowCommented:
Win 10, admin rights.   Ho Hum.
0
 
it_saigeDeveloperCommented:
UAC status and level?  Firewall status?

-saige-
0
 
it_saigeDeveloperCommented:
My UAC is disabled and firewall is off.

-saige-
0
 
AndyAinscowCommented:
UAC and firewall are on.  However I can just double click the wbemtest.exe in explorer and it runs without problem.
0
 
whorsfallAuthor Commented:
Hi,

I have done some more testing. I have got it to work. But I don't know why. If you target the Visual Studio to build an x64 app instead of AnyCPU it works.

So be interested if anybody got any theories on this one - why does not work on Anycpu?

Thanks,

Ward
0
 
AndyAinscowCommented:
Same behaviour here.  Changing to x64 results in it working.  Also if you leave it as AnyCPU and uncheck the 'prefer 32 bit' option (just below the CPU selection combo) then it also works without problem.

I really suspect this is something to do with aliasing of paths behind the scenes.
0
 
it_saigeDeveloperCommented:
I used sysinternals process monitor and verified that the cause is as Andy has stated, the Windows subsystem is trying to locate the 32-bit version of wbemtest.exe by aliasing System32 as SysWOW64:Capture.JPG
I then copied the wbemtest.exe into the SysWOW64\wbem folder and got nothing; no gui; no exception; no errors in the event log.  The process does start (verified by looking in Task Manager).  I might be missing a dependency but I would imagine that an error would be thrown.

A supporting link where the author states that wbemtest.exe on an 64-bit system has no 32-bit counterpart:

http://www.enhansoft.com/blog/why-wmic-is-useful

Just do as Andy states, deselect the 'Prefer 32-bit' option on your projects 'Build property page:Capture.JPG
-saige-
0
 
whorsfallAuthor Commented:
Hi,

Fantastic thanks for all the help I got - great responses.

Ward.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now