0

I have irritating problem and can't solve it 30+ hours. Please help me because there is a Friday evening and I'd like to go home.

What's a problem?

My service tries to invoke action for file via ShellExecuteA function but action doesn't invoked. When I invoke action from right click menu - this action is done correctly.

Details I create action for txt and tif extensions:

  • txt: Action Name: A4, comand line: "C:\Program Files\Windows\NT\Accessories\WORDPAD.EXE" /pt "%1" "Printer" “%3”
  • tif: Action Name: A4, comand line: "c:\windows\System32\rundll32.exe" "c:\windows\System32\shimgvw.dll",ImageView_PrintTo /pt "%1" "Printer" "%3" "%4"

Where Printer - name of printer. So, as you can see these commands are configured for printing file. In my service that I wrote on c# I use the next code:

string size = "A4";//name of action for file
int returnCode = Shell32.ShellExecuteAny(0, ref size, ref sourceFilePath, 0, 0, 6);

Where Shell32.ShellExecuteAny is the next:

[System.Security.SuppressUnmanagedCodeSecurity]
    public static class Shell32
    {
        [DllImport("shell32.dll", EntryPoint = "ShellExecuteA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        extern private static int ShellExecuteA(int hWnd, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpOperation, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpFile, IntPtr lpParameters, IntPtr lpDirectory, int nShowCmd);

        public static int ShellExecuteAny(int hWnd, ref string lpOperation, ref string lpFile, int lpParameters, int lpDirectory, int nShowCmd)
        {
            int result;
            GCHandle handle = GCHandle.Alloc(lpParameters, GCHandleType.Pinned);
            GCHandle handle2 = GCHandle.Alloc(lpDirectory, GCHandleType.Pinned);
            try
            {
                IntPtr tmpPtr = handle.AddrOfPinnedObject();
                IntPtr tmpPtr2 = handle2.AddrOfPinnedObject();

                result = ShellExecuteA(hWnd, ref lpOperation, ref lpFile, tmpPtr, tmpPtr2, nShowCmd);
            }
            finally
            {
                handle.Free();
                handle2.Free();
            }
            return result;
        }
    }

So, when sometimes my sourceFilePath is txt file, sometimes it's tif file. And this commands work correctly on windows7 and print my files.

But when I try to do so on windows2012r2 I noticed following things. Firstly, my txt files are printed correctly. Secondly, my tif files aren't printed but ShellExecuteAny returns 42(that is means that all have worked correctly). BUT when I right click on tif file and press A4 the file is printed!!!

That's confused me. I don't know what can I do to catch the error or understand what I do wrong. Thanks.

UPDATE

As Hans says now I use this code:

var startInfo =new ProcessStartInfo(sourceFilePath) {Verb = "A4" };
        if(startInfo.Verbs.FirstOrDefault(x=>x == "A4") == null)
        {
            Logger.Warn("Some warning");
            return false;
        }
        var newProcess = new Process {StartInfo = startInfo};

        try
        {
            newProcess.Start();
        }
        catch (Exception ex)
        {
            Logger.Warn("Some message");
        }

But the problem is the same. On windows7 all works fine. On windows 2012 no exceptions but for tif files verb doesn't work and pcl files isn't created. What is a so interesting problem?

  • The pinvoke declaration is not correct. There's no obvious reason for using it, this is already supported in .NET by the Process class. Use the ProcessStartInfo.Verb property. – Hans Passant Jan 31 '14 at 17:46
  • Hans, thanks for your advice, but problem is staying the same - all works on windows 7 but on windows 2012 verb works on right click but don't work `var startInfo =new ProcessStartInfo(sourceFilePath) {Verb = "A4" }; var newProcess = new Process {StartInfo = startInfo}; newProcess.Start();` No errors were occured but pcl file was not created. – Alexander Korolchuk Feb 03 '14 at 12:27
  • Well, you've now got two independent ways to tell you that there is something fundamentally borken about the verb implementation. Like not having the registry keys recorded properly, trying to use 32-bit registry keys on a 64-bit operating system (or the other way around), not having the target program properly installed, it crashing when invoked or not being able to access the file due to file system redirection. SysInternals' Process Monitor tends to be helpful, your IT staff often deals with problems like this. Ask more questions about it at superuser.com – Hans Passant Feb 03 '14 at 12:50

0 Answers0