Why does the process return null if we use Process.Start of a file in C# ?

Mojtaba_Hakim 326 Reputation points
2022-10-17T08:29:24.097+00:00

I'm using Windows 10 Enterprise in Administrator user

I have a program that receives the file stored in binary form in SQL Server and puts it in a folder in the TEMP path. For example: there is a photo in the database, I select it and WriteAllBytes
in the temp, then using Process.Start(C:\Users\ADMINI~1\AppData\Local\Temp.myiamge.png)
I run that photo through the Windows Photo Viewer.

so for control the process has started to know when that would be ended I use this : MyProcess.WaitForExit(); but I get this error because MyProcess is null but it stated process successfully !

full code and info :

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. RLWA32 51,536 Reputation points
    2022-10-17T12:12:58.207+00:00

    You can start the Photos App using the ApplicationActivationManager. Refer to https://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c.

    Adapting the code in the above SO answer, the following C# .Net Framework console application will start the Photos App with a file and wait for it to be closed. Error checking has been omitted.

       static void Main(string[] args)  
       {  
           Guid clsidActivationManager = new Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C");  
           Type ta = Type.GetTypeFromCLSID(clsidActivationManager);  
           IApplicationActivationManager iAppMgr = (IApplicationActivationManager)Activator.CreateInstance(ta);  
           uint pid;  
           IShellItemArray array = GetShellItemArray(@"C:\Users\RLWA32\Pictures\propget.png");  
           iAppMgr.ActivateForFile("Microsoft.Windows.Photos_8wekyb3d8bbwe!App", array, "Open",  
               out pid);  
           Console.WriteLine("Photos App activated for file");  
           Process Photos = Process.GetProcessById((int)pid);  
           Photos.WaitForExit();  
           Console.WriteLine("Photos App has exited");  
       }  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. RLWA32 51,536 Reputation points
    2022-10-17T09:52:01.097+00:00

    The Windows API function that is ultimately called by Process.Start with UseShellExecute=true is ShellExecuteEx. The documentation for calling this function includes the following -

    "Note: ShellExecuteEx does not always return an hProcess, even if a process is launched as the result of the call. For example, an hProcess does not return when you use SEE_MASK_INVOKEIDLIST to invoke IContextMenu."

    Consequently, the call to Process.Start is not guaranteed to return a non-null Process reference.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.