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?