Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32WindowDemo.java #1286

Closed
MPC-BlackBox opened this issue Dec 18, 2020 · 4 comments · Fixed by #1288
Closed

Win32WindowDemo.java #1286

MPC-BlackBox opened this issue Dec 18, 2020 · 4 comments · Fixed by #1288

Comments

@MPC-BlackBox
Copy link

Provide complete information about the problem

  1. Version of JNA and related jars - 5.6.0
  2. Version and vendor of the java virtual machine - OpenJDK 64-Bit Server VM Corretto-8.242.07.1 (build 25.242-b07, mixed mode)
  3. Operating system- WIN10
  4. System architecture (CPU type, bitness of the JVM) - Intel
  5. Complete description of the problem
  6. Steps to reproduce

The Win32WindowDemo.java example uses the following to gain access to the DEV_BROADCAST_PORT structure.

DEV_BROADCAST_PORT bpt = new DEV_BROADCAST_PORT(bhdr.getPointer());

when trying the demo I get the ERROR 'Array fields must be initialized'

Code works if you comment out the line but you are not able to access the DEV_BROADCAST_PORT structure.

MPC.

@matthiasblaesing
Copy link
Member

Please provide a full stack trace - for me the demo works. A real fix would be preferred, feel free to open a PR.

@dbwiddis
Copy link
Contributor

Looking at the DEV_BROADCAST_PORT mapping, it looks like size() is declared for the first element, before the fourth element (a char[]) is initialized. However, this should not cause a problem unless force is used in either getField() or calculateSize().

A potential fix for stability would be to move the size() initialization from the field into the constructor for the classes in DBT.java. Being able to reliably reproduce the cause and write a testcase that this change would fix would be very useful, and that starts with a stack trace and your associated code.

@MPC-BlackBox
Copy link
Author

Stack trace below:

Dec 21, 2020 11:31:22 AM com.sun.jna.Native$1 uncaughtException
WARNING: JNA: Callback com.company.MyForm$1@2a33fae0 threw the following exception
java.lang.IllegalStateException: Array fields must be initialized
at com.sun.jna.Structure.deriveLayout(Structure.java:1288)
at com.sun.jna.Structure.calculateSize(Structure.java:1159)
at com.sun.jna.Structure.ensureAllocated(Structure.java:380)
at com.sun.jna.Structure.ensureAllocated(Structure.java:367)
at com.sun.jna.Structure.size(Structure.java:434)
at com.sun.jna.platform.win32.DBT$DEV_BROADCAST_PORT.(DBT.java:283)
at com.company.MyForm$1.callback(MyForm.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:520)
at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:551)
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(WToolkit.java:316)
at java.lang.Thread.run(Thread.java:748)

@MPC-BlackBox
Copy link
Author

Code which causes the issue below. I have managed to get round the issue using the lines of code.

                	   ComName=bhdr.getPointer().getWideString(bhdr.size()); 
                	   System.out.println("Device name : " + ComName);
                       System.out.println("BROADCAST_PORT: " + uMsg);

Code which causes the issue:
public static MyListener listener = new MyListener()
{
public WinDef.LRESULT callback(WinDef.HWND hWnd, int uMsg, WinDef.WPARAM uParam, WinDef.LPARAM lParam)
{
if (uMsg == MyWinUser.WM_DEVICECHANGE)
{
if(uParam.intValue() == MyWinUser.DBT_DEVICEARRIVAL)
{
DEV_BROADCAST_HDR bhdr = new DEV_BROADCAST_HDR(lParam.longValue());
System.out.println("Device was added");
switch (bhdr.dbch_devicetype) {
case DBT.DBT_DEVTYP_PORT: {
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363248.aspx
DEV_BROADCAST_PORT bpt = new DEV_BROADCAST_PORT(bhdr.getPointer());
ComName=bhdr.getPointer().getWideString(bhdr.size());
System.out.println("Device name : " + ComName);
System.out.println("BROADCAST_PORT: " + uMsg);
break;
}
default:
}
// return TRUE means processed message for this wParam.
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363205.aspx
// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363208.aspx
}

               if(uParam.intValue() == MyWinUser.DBT_DEVICEREMOVECOMPLETE)
               {
				  DEV_BROADCAST_HDR bhdr = new DEV_BROADCAST_HDR(lParam.longValue());
				  System.out.println("Device was removed");
				  switch (bhdr.dbch_devicetype) {
				  case DBT.DBT_DEVTYP_PORT: {
				      // see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363248.aspx
				   System.out.println("Device name : " + ComName);
					if (MyForm.ComName!=null && ComName.length()!=0)
					{
						if (ComName.equals(bhdr.getPointer().getWideString(bhdr.size())))
						{
						   ComName="";
						}
					}
                      break;
                  }
                  default:
                  }
               }

               if(uParam.intValue() == MyWinUser.DBT_DEVNODES_CHANGED)
               {                    
                   System.out.println("Device node changed" + lParam.longValue());
               }
               return new WinDef.LRESULT(1);
            }
            return new WinDef.LRESULT(0);
        }
    };

matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Dec 21, 2020
- Ensure all structures defined in com.sun.jna.platform.win32.DBT can be
  instantiated
- Use correct mapping for DEV_BROADCAST_PORT#dbcp_name. And add accessor
  for the string value of that property. This is an API incompatible
  change, but the class could not be instantiated, so it is assumed,
  that it is a safe change
- Prevent potential out-of-bounds read in DEV_BROADCAST_DEVICEINTERFACE
- Make demo-nativewindowmsg.jar runnable

Closes: java-native-access#1286
matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Dec 22, 2020
- Ensure all structures defined in com.sun.jna.platform.win32.DBT can be
  instantiated
- Use correct mapping for DEV_BROADCAST_PORT#dbcp_name. And add accessor
  for the string value of that property. This is an API incompatible
  change, but the class could not be instantiated, so it is assumed,
  that it is a safe change
- Prevent potential out-of-bounds read in DEV_BROADCAST_DEVICEINTERFACE
- Make demo-nativewindowmsg.jar runnable

Closes: java-native-access#1286
matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Dec 22, 2020
- Ensure all structures defined in com.sun.jna.platform.win32.DBT can be
  instantiated
- Use correct mapping for DEV_BROADCAST_PORT#dbcp_name. And add accessor
  for the string value of that property. This is an API incompatible
  change, but the class could not be instantiated, so it is assumed,
  that it is a safe change
- Prevent potential out-of-bounds read in DEV_BROADCAST_DEVICEINTERFACE
- Make demo-nativewindowmsg.jar runnable

Closes: java-native-access#1286
dbwiddis pushed a commit to dbwiddis/jna that referenced this issue Jan 29, 2021
- Ensure all structures defined in com.sun.jna.platform.win32.DBT can be
  instantiated
- Use correct mapping for DEV_BROADCAST_PORT#dbcp_name. And add accessor
  for the string value of that property. This is an API incompatible
  change, but the class could not be instantiated, so it is assumed,
  that it is a safe change
- Prevent potential out-of-bounds read in DEV_BROADCAST_DEVICEINTERFACE
- Make demo-nativewindowmsg.jar runnable

Closes: java-native-access#1286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants