Solved

c# can't call wbemtest.exe

Posted on 2016-08-31
22
27 Views
Last Modified: 2016-09-03
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
Comment
Question by:whorsfall
  • 9
  • 6
  • 6
  • +1
22 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
 

Author Comment

by:whorsfall
Comment Utility
Hi,

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

Thanks,

Ward
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
 

Author Comment

by:whorsfall
Comment Utility
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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>               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
 

Author Comment

by:whorsfall
Comment Utility
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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Wierd.  I have the same problem here.
0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
 

Author Comment

by:whorsfall
Comment Utility
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
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 32

Expert Comment

by:it_saige
Comment Utility
@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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
@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
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Win 10, admin rights.   Ho Hum.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
UAC status and level?  Firewall status?

-saige-
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
My UAC is disabled and firewall is off.

-saige-
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
UAC and firewall are on.  However I can just double click the wbemtest.exe in explorer and it runs without problem.
0
 

Author Comment

by:whorsfall
Comment Utility
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
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
Comment Utility
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
 
LVL 32

Accepted Solution

by:
it_saige earned 250 total points
Comment Utility
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
 

Author Comment

by:whorsfall
Comment Utility
Hi,

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

Ward.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now