Is this the whole code? The pointer starts out pointing to random memory, and the first thing you do with it is dereference it, assigning the value JAWT_VERSION_1_4 to some random location in memory -- that's a crash waiting to happen right there.
Instead of awt being a pointer to JAWT, make is a JAWT, then use &awt when you need a pointer.
You don't need it to be a valid structure before calling the method, but it does have to be a structure. The pointer variable you're declaring is literally pointing to a random location in memory, and when you set awt->version to JAWT_VERSION_1_4, you're literally setting a random location in memory to that value. It could be corrupting the stack, or the Java heap, or the malloc arena, or any other random data structure. You have to actually have some memory set aside to hold the JAWT struct and the variable has to refer to it.
So the solution is simply to actually allocate memory for a JAWT structure. There are two ways to do this. If you just say
then awt is a global variable containing the memory itself; the memory is located in a safe place in global memory (at the base of the stack, perhaps.) It doesn't need to be freed, so it's convenient. "&awt" is the address of this structure; use it wherever you need the address of the structure. This is what I recommended in my previous reply.
The other choice is to use "malloc" allocate one, something like
JAWT *awt = (JAWT*) malloc(sizeof(JAWT));
Dynamic memory is allocated on the heap to hold a JAWT object; then just use awt as the pointer to this memory. Strictly, you need to use "free" at some point to get rid of the JAWT struct.
Joined: Feb 06, 2008
Ok thanks worked both ways. However my next problem is that
Is returning NULL which means it was unsuccessful in creating the window given the jhandle. I know that jhandle isn't the correct way so I tried
then passing win32dsinfo->hwnd instead of jhandle. Both ways lead to problems however if I don't use swing components and just use the awt Window class then it works fine.
Question 1 Can you only use this native interface with awt objects or can you also use it with swing objects? If you can use swing then how can you get a handle to it's components. Seems to me that all the handles are 0x00000 when I use swing components. (I would love to beable to use swing components with this stuff )
Question 2 (JVM crashes if I use anything other then the following) The only successful way is when I extend Windows or Frames class's. But I have a problem with using these. Because I cann't add a window to a Frame ,... JFrame , ... or whatever. You get an java.lang.IllegalArgumentException: adding a window to a container. I have done my research on the awt and supposedly windows and frames cann't be add to each other. So how can I set the window on a Frame or JFrame. I don't really want to have to create functions that redraw the window relative to where the Frame moves but maybe that is the only way?
Right now It look's like the window is a popup window and not within my Frame. (I.E when I move the frame it doesn't move the window with it!)
I could basically get rid of the middle man and just have the native code in the top level Frame. However their are 2 problems with this. One I wanted to make a JPanel/or Panel class so I could add this to a top level JFrame/or Frame. Partly so I could use this in different projects just by adding the JPanel/Panel to a JFrame/Frame as needed.
Two I do alot of swing code and I don't like to mix light/heavyweight components to much (this is where question 1 comes in )
Originally posted by Sam Doder: Question 1 Can you only use this native interface with awt objects or can you also use it with swing objects? If you can use swing then how can you get a handle to it's components. Seems to me that all the handles are 0x00000 when I use swing components. (I would love to beable to use swing components with this stuff )
You can only use it with heavy weight objects. You can test objects by calling Component's isLightweight() method. Please note that calling this method will only make sense when the component has become paintable.
For instance, if you create a control like a JComboBox, then check the return of isLightweight(), it will return false. But as soon as you add it to a frame / dialog, show that and check, it will return true.
JFrame and JDialog are among the very, very few heavy weight components in Swing.
So does this mean that I can only use this native interface with awt. And a couple types of swing classes like JFrame and JDialog.
Or is their anyway I could use this stuff on a JInternal Frame's.
I know you can use getPeer() to get a handle on the java side. However these classes are not supported and can change with different versions of jsdk. Java developer's should provided this functionality for getting a handle to their GUI component's.
Anyway I have been looking around in the java folders and I came across all the dll's like awt.dll ,...etc etc...
What I am curious about is their a way to get the source code to the dll's like awt.dll , jawt.dll, jvm.dll . Because then maybe I could improve the JNI for GUI thing's. (Or at least know how to go from C GUI to java GUI and vise versa. )
I have read all the java source code but it always boils down to calling a native function. I have been successful in loading awt.dll and using some native functions that I found in their source code. Even with the JNI they only give you the header file's not the .cpp files which is where everthing is.
Anyway back to the initial question so I am new to using jawt.h and in your opinon what is the best way to complete this so that my native window is on top of a JFrame , Frame , JPanel , Panel ,...
Another problem is that the native window created by capCreateCaptureWindow has a HWND as I parent. I could be wrong here but this mean's that the parent must be a handle to a window. Which is why I get a JVM crash if I don't extend from either a Window or Frame. I would love to beable to extend a JPanel or Panel and add it to a Frame or a JFrames ContentPane. And I still have big problems with the jawt.h and heavy/lightweight components.
author and iconoclast
The thing you need to understand is pretty simple: "heavyweight" components are those that actually have a peer (a native component). "Lightweight" components simply don't have one -- they're drawn using Java graphics calls onto other components that do have peers. So it doesn't make any sense to try to get a HWND to a JPanel, because JPanels aren't associated with an OS control of any kind; whereas a JFrame is tied to a "real" OS window.
Anyway, I think the basic idea of what you need to to is, given a JPanel, find the top-level window that it's packed into, get the native window associated with that, and then draw your video into that top-level window using the bounds of the original JPanel. You can easily find the top-level window of a component using the SwingUtilities.getRoot() method.
Joined: Feb 06, 2008
Ok I get what you are saying about light and heavyweight components. But JPanel's and Panels don't inheret from a Window. So I am assuming SwingUtilities.getRoot() will return me a Container class if I use it on these objects.
What my trouble is. Is that the VFW lib in my native code I can only get to work if I pass a Window Object Handle as a parent
So win32dsinfo->hwnd must be a handle to a window object in java. But This limit's me to using Window , JWindow , Frame , JFrame or ( I am assuming any class that inheret's Window directly or indirectly )
So I really wanted to beable to put the video screen on my JFrame. Kind of like adding a JPanel to a JFrame.
Cann't go Panel/JPanel to Frame /JFrame (error in the JNI code) Cann't go Panel/JPanel to Window/JWindow (error in the JNI code) Cann't go Window/JWindow add Frame /JFrame (error in java code)
This leaves me with the only choice of creating a Window who has it's parent as a JFrame/Frame. And then repositioning the Window over the JFrame. And using mouse moved events to repostion the Window over the JFrame if the JFrame moves.
Their must be a better way? (maybe their isn't really not to hard to do) How would you put a capture window in a JFrame???
I am also troubled by the fact that I have to use this function
since why cann't I just do
instead of first creating a capCreateCaptureWindow I should beable just to use the window's handle I get from the java object. (i.e win32dsinfo->hwnd )
However when I do it this way capDriverConnect returns false So no go. :roll:
This is very hard to debug since it is not the code that is wrong it's at runtime. Maybe you have to use a handle created by capCreateCaptureWindow for capDriverConnect to work.
I am going to write a c++ program to test if I can use a handle created without using capCreateCaptureWindow. If it works then I know it is something with java win32 handle's.
C++ Question 1 Does anybody know if you can use capDriverConnect without using capCreateCaptureWindow?
Java Question 1 Since their is no way to add a Window to a Frame. Is the only way to get that effect is thur repostioning when the Frame moves...
Java Question 2 Say I have a class A that has a Thread in it call it ThreadA. If ThreadA call's a native method in it's run method over and over again. Like in an infinte while loop! Is this Ok? The reason I ask is that I have my WebCamScreen class which is going to implement runnable and call the native capGrabFrame function in an infinite while loop.
However I have had a deadlocking problem on the JNI side with capGrabFrame From the VFW. I have read that library's that use messaging services like sendMessage are Deadlock prone. How can I stop them from deadlock.
I try to use postMessage instead of capGrabFrame or sendMethod but I still giving me trouble.
This is all very frustrating.
So if anybody is an Expert at JNI Threading Problems and the VFW lib Please help.
I don't thing the deadlocking problems can be fixed with a synchronized statment or (Monitor on the JNI side)
I think it is that the VFW is deadlock prone. Plus the fact I am not an expert at using it.
Question 3 Can you spawn a thread from the JNI / C++ code ? I mean not by creating a new java/lang/thread object but using like pThread's or the C++ threads (OS thread's )
Would you have to Attach the Thread or could it run on the native side until the native side killed it. Or until you make a native call to a function that kills it?
Because maybe capGrabFrame would work in a seperate native thread instead of a java thread.
Thanks for your replies very helpful and you are very knowledgeable.