Samples‎ > ‎WinPack Samples‎ > ‎

ShellExecuteExExample

import com.jniwrapper.*;
import com.jniwrapper.win32.Handle;
import com.jniwrapper.win32.LastErrorException;
import com.jniwrapper.win32.automation.OleStr;
import com.jniwrapper.win32.shell.Shell32;
import com.jniwrapper.win32.ui.Wnd;

/**
 * This example demonstrates how to invoke <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx">ShellExecuteE</a> API function,
 * and how to create the Java wrapper for <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx">SHELLEXECUTEINFO</a> structure.
 */
public class ShellExecuteExExample {
    /**
     * Wrapper for <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx">SHELLEXECUTEINFO</a> structure.
     */
    public static class SHELLEXECUTEINFO extends Structure {
        /*
        DWORD     cbSize;
        ULONG     fMask;
        HWND      hwnd;
        LPCTSTR   lpVerb;
        LPCTSTR   lpFile;
        LPCTSTR   lpParameters;
        LPCTSTR   lpDirectory;
        int       nShow;
        HINSTANCE hInstApp;
        LPVOID    lpIDList;
        LPCTSTR   lpClass;
        HKEY      hkeyClass;
        DWORD     dwHotKey;
        union {
            HANDLE hIcon;
            HANDLE hMonitor;
        } DUMMYUNIONNAME;
        HANDLE    hProcess;
        */
        private UInt32 cbSize = new UInt32();
        private ULongInt fMask = new ULongInt();
        private Wnd hwnd = new Wnd();
        private OleStr lpVerb = new OleStr();
        private OleStr lpFile = new OleStr();
        private OleStr lpParameters = new OleStr();
        private OleStr lpDirectory = new OleStr();
        private Int nShow = new Int();
        private Handle hInstApp = new Handle();
        private Pointer.Void lpIDList = new Pointer.Void();
        private OleStr lpClass = new OleStr();
        private Handle hkeyClass = new Handle();
        private UInt32 dwHotKey = new UInt32();
        private Handle hIcon = new Handle();
        private Handle hProcess = new Handle();

        public SHELLEXECUTEINFO() {
            init(new Parameter[] {
                    cbSize,
                    fMask,
                    hwnd,
                    lpVerb,
                    lpFile,
                    lpParameters,
                    lpDirectory,
                    nShow,
                    hInstApp,
                    lpIDList,
                    lpClass,
                    hkeyClass,
                    dwHotKey,
                    hIcon,
                    hProcess
            }, (short) 8);
        }
    }

    static final int SW_SHOW = 5;

    public static void main(String[] args) {
        Function shellExecuteEx = Shell32.getInstance().getFunction("ShellExecuteExW"); // using unicode version of ShellExecuteExW

        Bool result = new Bool();
        SHELLEXECUTEINFO shellexecuteinfo = new SHELLEXECUTEINFO();

        shellexecuteinfo.cbSize.setValue(shellexecuteinfo.getLength());
        shellexecuteinfo.lpFile.setValue("notepad.exe");
        shellexecuteinfo.lpParameters.setValue("");
        shellexecuteinfo.nShow.setValue(SW_SHOW);

        long lastError = shellExecuteEx.invoke(result, new Pointer(shellexecuteinfo));
        System.out.println("result = " + result);
        if (!result.getValue()) {
            throw new LastErrorException(lastError);
        }
    }
}