Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: I need help on a native project

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help on a native project

    Dear users,
    I have a program that starts up a notepad, and I want it to be contained inside a java application.
    Like: Code to Host a Third Party Application in our Process Window (e.g. Google Chrome, Internet Explorer 8) - CodeProject
    Everything runs fine, but finally the SetParent does not work at all, but it return a pointer (showing me that it is success)

    The SetParent() does not set the parent, but everything seems to be fine.

    The windows supports the following functions to handle windows:
    Content not found...

    The following is done:
    CreateProcessA() called to start notepad (and I used it because java does not allow me to retrieve process id).
    WaitForInputIdle() used to wait for the window to become visible, and to retrieve its handle.
    GetTopWindow(null), GetWindowThreadProcessId(w, pid), GetWindow(w, 2); is used to find the window of the given process id. (If I close the notepad before these are executed the system does not find a window for that process id, so it works as well)
    Finally the SetParent() is used to set the parent
    From the microsoft msdn:
    "If the function succeeds, the return value is a handle to the previous parent window."
    And it does return a pointer!
    Result:
    The notepad window does not hosted

    Here are the files I use:

    --- APPLICATION -------------------
    package com.smith;
     
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Component;
    import java.awt.Dimension;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
     
    import com.smith.Kernel32.ProcessInformation;
    import com.smith.Kernel32.StartupInfoA;
    import com.sun.jna.Native;
    import com.sun.jna.Platform;
    import com.sun.jna.Pointer;
    import com.sun.jna.ptr.IntByReference;
     
    public class Main implements Runnable {
        public static void main(String[] args) {
        Main main = new Main();
        SwingUtilities.invokeLater(main);
        }
     
        public Pointer getWindowOf(int pid) {
        Pointer w = User32.INSTANCE.GetTopWindow(null);
        IntByReference iptr = new IntByReference();
        while (w != null) {
            User32.INSTANCE.GetWindowThreadProcessId(w, iptr);
            if (iptr.getValue() == pid) {
            return w;
            }
            w = User32.INSTANCE.GetWindow(w, 2);
        }
        return null;
        }
     
        private String host(String prog, Component heavy) {
        ProcessInformation pi = this.runAndWait(prog);
        heavy.setPreferredSize(new Dimension(600, 500));
        Pointer handle = Native.getComponentPointer(heavy);
        Pointer procWnd = this.getWindowOf(pi.dwProcessId);
        Pointer last = User32.INSTANCE.SetParent(procWnd, handle);
        if (last == null) {
            throw new IllegalArgumentException("Can not set parent: " + Kernel32.INSTANCE.GetLastError());
        }
        return "Started: " + pi.dwProcessId + " (window: " + procWnd + ") -> Java parent: " + handle;
        }
     
        @Override
        public void run() {
        JFrame frame = new JFrame("Hosting Window");
        frame.setPreferredSize(new Dimension(500, 600));
        JLabel label = new JLabel("Alma");
        frame.setLayout(new BorderLayout());
        frame.add(label, BorderLayout.NORTH);
        Component heavy = new Canvas();
        frame.add(heavy, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        try {
            String ret = this.host("c:\\Windows\\notepad.exe", heavy);
            label.setText(ret);
        } catch (IllegalArgumentException ia) {
            label.setText(ia.getMessage());
        }
        }
     
        private ProcessInformation runAndWait(String program) {
        ProcessInformation processInformation = new ProcessInformation();
        StartupInfoA startupInfo = new StartupInfoA();
        startupInfo.cb = Platform.is64Bit() ? 104 : 68;
        boolean ok = Kernel32.INSTANCE.CreateProcessA(program, null, null, null, true, 0x00000020, null, null, startupInfo, processInformation);
        if (ok) {
            User32.INSTANCE.WaitForInputIdle(processInformation.hProcess, -1);
        } else {
            throw new IllegalArgumentException("Executing '" + program + "' failed: " + Kernel32.INSTANCE.GetLastError());
        }
        return processInformation;
        }
    }
    ---------------------------------------------------
    package com.smith;
     
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.Structure;
     
    public interface Kernel32 extends Library {
        public class ProcessInformation extends Structure {
        public Pointer hProcess;
        public Pointer hThread;
        public int dwProcessId;
        public int dwThreadId;
        }
     
        public class StartupInfoA extends Structure {
        public int cb;
        public String lpReserved;
        public String lpDesktop;
        public String lpTitle;
        public int dwX;
        public int dwY;
        public int dwXSize;
        public int dwYSize;
        public int dwXCountChars;
        public int dwYCountChars;
        public int dwFillAttribute;
        public int dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public Pointer lpReserved2;
        public Pointer hStdInput;
        public Pointer hStdOutput;
        public Pointer hStdError;
        }
     
        Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
     
        boolean CreateProcessA(String lpApplicationName, String lpCommandLine, Pointer lpProcessAttributes, Pointer lpThreadAttributes, boolean bInheritHandles,
            long dwCreationFlags, Pointer lpEnvironment, String lpCurrentDirectory, StartupInfoA lpStartupInfo, ProcessInformation lpProcessInformation);
     
        int GetLastError();
    }
    ----------------------------------------------------
    package com.smith;
     
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.ptr.IntByReference;
     
    public interface User32 extends Library {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
     
        Pointer GetTopWindow(Pointer wnd);
     
        Pointer GetWindow(Pointer wnd, int cmd);
     
        int GetWindowThreadProcessId(Pointer wnd, IntByReference ptr);
     
        Pointer SetParent(Pointer hWndChild, Pointer hWndNewParent);
     
        int WaitForInputIdle(Pointer process, int timeout);
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I need help on a native project

    This is a duplicate post
    See: http://www.javaprogrammingforums.com...ts-needed.html

    Also cross posted at: JNA problem

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I need help on a native project

    Duplicate post removed. quake_jatekos, recommended reading: http://www.javaprogrammingforums.com...s-posting.html

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help on a native project

    Quote Originally Posted by copeg View Post
    Duplicate post removed. quake_jatekos, recommended reading: http://www.javaprogrammingforums.com...s-posting.html
    Java Forums - The Front Page is not www.javaprogrammingforums.com, I do not understand.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help on a native project

    Quote Originally Posted by quake_jatekos View Post
    quake, please read - http://www.javaprogrammingforums.com...s-posting.html

    All will become clear.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help on a native project

    Because you are using non standard classes, it's going to be hard to get someone to answer this unless they are willing to setup the project in the same way.

    This does look interesting though so I will look into it further.

    I notice that 'the code project' have a forum so it may worth posting there for help considering that is where you found the code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. calling native c code from java using JNI
    By ashok_jeev in forum Java Native Interface
    Replies: 3
    Last Post: November 20th, 2012, 05:28 AM
  2. Using JNA to access export native classes
    By sattu in forum Java Theory & Questions
    Replies: 1
    Last Post: May 9th, 2011, 04:24 AM
  3. Openning a file in Native Application not Working
    By wagb278 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 8th, 2011, 02:31 PM
  4. Resize a window without native decorators
    By supertreta in forum AWT / Java Swing
    Replies: 0
    Last Post: January 11th, 2011, 02:06 PM
  5. Can anyone help me on my project?
    By alesana514 in forum Paid Java Projects
    Replies: 1
    Last Post: December 16th, 2009, 09:24 AM