Get win32 error while trying to start external program (32 bit DOS)

Rinaldo 396 Reputation points
2022-06-20T13:14:45.117+00:00

I try to start a 32 bit dos program via SHellExecute because Process.Start fails to start with the parameters giving. Now I try via ShellExecuteEX but get an win32 error.

The code:

        private void btn2_Click(object sender, EventArgs e)  
        {  
            ShellExecuteInfo sei = new ShellExecuteInfo();  
            sei.Size = Marshal.SizeOf(sei);  
            sei.Verb = "";  
            sei.File = "binkd.exe";  
            sei.Directory = Environment.CurrentDirectory;  
            sei.Show = 1;  
            if (!ShellExecuteEx(ref sei))  
                throw new System.ComponentModel.Win32Exception();  
  
        }  
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
        public class ShellExecuteInfo  
        {  
            public ShellExecuteInfo()  
            {  
                Size = Marshal.SizeOf(this);  
            }  
            public int Size;  
            public uint Mask;  
            public IntPtr hwnd;  
            public string Verb;  
            public string File;  
            public string Parameters;  
            public string Directory;  
            public int Show;  
            public IntPtr InstApp;  
            public IntPtr IDList;  
            public string Class;  
            public IntPtr hkeyClass;  
            public uint HotKey;  
            public IntPtr IconOrMonitor;  
            public IntPtr Process;  
        }  
          
        [DllImport("shell32.dll")]  
          
        public static extern IntPtr ShellExecute(  
             IntPtr hwnd,  
             string lpszOp,  
             string lpszFile,  
             string lpszParams,  
             string lpszDir,  
             ShowWindowCommands FsShowCmd  
         );  
  
        [DllImport("shell32.dll", SetLastError = true)]  
        extern public static bool  
           ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);  
C#
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.
10,203 questions
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2022-06-20T14:08:23.28+00:00

    SHELLEXECUTEINFO is usually defined as struct, not class (if you use ref)

    This works for me :

            public struct SHELLEXECUTEINFO  
            {  
                public int cbSize;  
                public uint fMask;  
                public IntPtr hwnd;  
                [MarshalAs(UnmanagedType.LPStr)]  
                public string lpVerb;  
                [MarshalAs(UnmanagedType.LPStr)]  
                public string lpFile;  
                [MarshalAs(UnmanagedType.LPStr)]  
                public string lpParameters;  
                [MarshalAs(UnmanagedType.LPStr)]  
                public string lpDirectory;  
                public int nShow;  
                public IntPtr hInstApp;  
                public IntPtr lpIDList;  
                [MarshalAs(UnmanagedType.LPStr)]  
                public string lpClass;  
                public IntPtr hkeyClass;  
                public uint dwHotKey;  
                public IntPtr hIcon;  
                public IntPtr hProcess;  
            }  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rinaldo 396 Reputation points
    2022-06-20T17:21:59.297+00:00

    @Castorix31
    It works thanks

    0 comments No comments