Solved

c# can't call wbemtest.exe

Posted on 2016-08-31
22
30 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
ID: 41777770
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
ID: 41777774
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
ID: 41777798
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:whorsfall
ID: 41777810
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
ID: 41777908
>>               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
ID: 41777964
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
ID: 41778061
Wierd.  I have the same problem here.
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41778137
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
ID: 41778149
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 33

Expert Comment

by:it_saige
ID: 41778436
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
ID: 41778521
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
 
LVL 33

Expert Comment

by:it_saige
ID: 41778592
@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
ID: 41778664
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 33

Expert Comment

by:it_saige
ID: 41778686
@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
ID: 41778694
Win 10, admin rights.   Ho Hum.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41778696
UAC status and level?  Firewall status?

-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41778700
My UAC is disabled and firewall is off.

-saige-
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41778730
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
ID: 41779315
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
ID: 41779350
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 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 41780127
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
ID: 41782622
Hi,

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

Ward.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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