3

I'm trying to get the name of the Windows 8 App that is running with a given ProcessID. I can get to wwahost, that is the real name of the process that is running, but I want to get the name of the App that WWHOST is actually running.

I saw this thread http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/c9665bf4-00e4-476c-badb-37126efd3f4b/ with that discussion, but there is no specific answer.

any ideas ?

2 Answers2

2

You want to call GetApplicationUserModelId

The sample application provided allows you to pass in a PID and get back the information about the app. For instance:

C:\src\GetAppInfo\Debug>GetAppInfo.exe 7400
Process 7400 (handle=00000044)
Microsoft.BingWeather_8wekyb3d8bbwe!App

To port to C#,

   const int QueryLimitedInformation = 0x1000;
   const int ERROR_INSUFFICIENT_BUFFER = 0x7a;
   const int ERROR_SUCCESS = 0x0;

   [DllImport("kernel32.dll")]
   internal static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

   [DllImport("kernel32.dll")]
   static extern bool CloseHandle(IntPtr hHandle);

    [DllImport("kernel32.dll")]
    internal static extern Int32 GetApplicationUserModelId(
        IntPtr hProcess, 
        ref UInt32 AppModelIDLength, 
        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder sbAppUserModelID);

Then, your code should look something like this:

            if (sProcessName.ToLower().Contains("wwahost") 
            && ((Environment.OSVersion.Version.Major == 6) && (Environment.OSVersion.Version.Minor > 1)))
            {
                IntPtr ptrProcess = OpenProcess(QueryLimitedInformation, false, iPID);
                if (IntPtr.Zero != ptrProcess)
                {
                    uint cchLen = 130; // Currently APPLICATION_USER_MODEL_ID_MAX_LENGTH = 130
                    StringBuilder sbName = new StringBuilder((int)cchLen);
                    Int32 lResult = GetApplicationUserModelId(ptrProcess, ref cchLen, sbName);
                    if (ERROR_SUCCESS == lResult)
                    {
                        sResult = sbName.ToString();
                    }
                    else if (ERROR_INSUFFICIENT_BUFFER == lResult)
                    {
                        sbName = new StringBuilder((int)cchLen);
                        if (ERROR_SUCCESS == GetApplicationUserModelId(ptrProcess, ref cchLen, sbName))
                        {
                            sResult = sbName.ToString();
                        }
                    }
                    CloseHandle(ptrProcess);
                }
            }
EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • I don't understand why, if you receive `ERROR_INSUFFICIENT_BUFFER`, you make the same call again a second time? In theory, that would mean that 130 is not enough, so it won't work the second time... unless I misunderstood something? – Christian Rondeau Aug 14 '15 at 02:13
  • 1
    This is a common calling pattern in Windows; you make an API call with a guess at the necessary buffer size, and if you're wrong, you use the buffer size that the failing call suggests. (Note that `ref cchLen` parameter?) – EricLaw Aug 14 '15 at 02:56
-1

Have a look at Get executing assembly name from referenced DLL in C#

You could look around Assembly.GetEntryAssembly() or Assembly.GetExecutingAssembly() e.g.

string exeAssemblyName = Assembly.GetEntryAssembly().GetName().Name;
Community
  • 1
  • 1
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • Your suggestion will get the name of the assembly that is running. That was not the question. I want to get the name of the Windows Store app that is running under the process wwwhost with a specific process ID. – Tiago Andrade e Silva Oct 29 '13 at 15:00