If your structure has array fields they need to be initialized before you try to do anything that requires the size of the structure to be known (like allocating its backing memory).
On Oct 3, 2012, at 7:10 AM, Siva Rama Krishna (MT, BLR) wrote:
> This is what I tried
>
> case MyUser32.DBT_DEVTYP_DEVICEINTERFACE:
> {
> int size = (devHDR.dbch_size - 28) / 2;
> DEV_BROADCAST_DEVICEINTERFACE devInterf = new DEV_BROADCAST_DEVICEINTERFACE(
> new Pointer(lParam.longValue()));
> Arrays.copyOf(devInterf.dbcc_name, size);
> String str = new String(devInterf.dbcc_name, 0,size);
> System.out.println("Device name : " + str);
>
> }
>
> And it is giving the error as follows :
>
> JNA: Callback com.merittrac.apollo.testplayer.maldetect.ExternalDeviceDetector$1@d7b7d9 threw the following exception:
> java.lang.IllegalStateException: Array fields must be initialized
> at com.sun.jna.Structure.deriveLayout(Structure.java:919)
> at com.sun.jna.Structure.calculateSize(Structure.java:827)
> at com.sun.jna.Structure.ensureAllocated(Structure.java:307)
> at com.sun.jna.Structure.ensureAllocated(Structure.java:294)
> at com.sun.jna.Structure.read(Structure.java:467)
> at com.merittrac.apollo.testplayer.maldetect.MyUser32$DEV_BROADCAST_DEVICEINTERFACE1.<init>(MyUser32.java:122)
> at com.merittrac.apollo.testplayer.maldetect.ExternalDeviceDetector$1.callback(ExternalDeviceDetector.java:172)
> at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:444)
> at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:474)
> at sun.awt.windows.WToolkit.eventLoop(Native Method)
> at sun.awt.windows.WToolkit.run(WToolkit.java:291)
> at java.lang.Thread.run(Thread.java:619)
>
> can you explain me with some example...
>
> Thanks,
> Siva
>
>
> -----Original Message-----
> From: Timothy Wall [mailto:
twal...@java.net]
> Sent: Wednesday, October 03, 2012 4:30 PM
> To: Siva Rama Krishna (MT, BLR)
> Subject: Re: JNA call RegisterDeviceNotification fails with error 1066
>
> You simply need to look up the name (as a String), extract the char[] from the String, and assign it to the structure field dbcc_name and adjust the dbcc_size.
>
> JNA includes utilities for manipulating the registry.
>
> On Oct 3, 2012, at 2:26 AM, Siva Rama Krishna (MT, BLR) wrote:
>
>> My DEV_BROADCAST_DEVICEINTERFACE1 structure in c# is as follows :
>>
>> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
>> public struct DEV_BROADCAST_DEVICEINTERFACE1
>> {
>> public int dbcc_size;
>> public int dbcc_devicetype;
>> public int dbcc_reserved;
>> public Guid dbcc_classguid;
>> [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
>> public Char[] dbcc_name;
>> }
>> Thanks,
>> Siva.
>>
>> -----Original Message-----
>> From: Timothy Wall [mailto:
twal...@java.net]
>> Sent: Monday, October 01, 2012 5:14 PM
>> To:
iss...@jna.java.net
>> Cc: Siva Rama Krishna (MT, BLR)
>> Subject: Re: JNA call RegisterDeviceNotification fails with error 1066
>>
>> * Your return type is LONG, which is 32 bits, but should be of type HDEVNOTIFY, which is probably a pointer type.
>> * You might as well declare the second argument to be of type DEV_BROADCAST_DEVICEINTERFACE and pass the arg directly (instead of Structure.getPointer()).
>> * make sure you declare your structure field order (3.5.0 and later requires this; earlier depend on the JVM doing the expected thing if you don't specify it).
>> * dbcc_name might be char[] or byte[] depending on whether you're using ASCII/UNICODE mapping. You should probably initialize the name.
>>
>> On Oct 1, 2012, at 2:51 AM, Shankar Kommineni (MT, BLR) wrote:
>>
>>> Hi,
>>>
>>> We are attempting to register for notifications on USB device arrival
>>> and device remove complete events from a java application using JNA
>>> framework. But we seem to hit a problem with
>>> RegisterDeviceNotification
>>> () method which seems to throw error 1066 all the time. Can some shed some light on this issue?. Below is the sample code that we are using:
>>>
>>> JNA Version :: jna-3.4.1
>>>
>>> Interface File Declaration ::
>>>
>>> LONG RegisterDeviceNotification(HWND hWnd, Pointer pointer,int
>>> deviceNotifyWindowHandle); public static final int
>>> DBT_DEVTYP_DEVICEINTERFACE= 0x00000005; public static GUID[] arrGuid
>>> = new GUID[] {
>>> GUID_DEVINTERFACE_USB_DEVICE,Ole32Util.getGUIDFromString("{4d1e55b2-f
>>> 1
>>> 6f-11cf-88cb-001111000030}"),Ole32Util.getGUIDFromString("{53f56307-b
>>> 6
>>> bf-11d0-94f2-00a0c91efb8b}"),
>>> Ole32Util.getGUIDFromString("{784126bf-4190-11d4-b5c2-00c04f687a67}")
>>> ,
>>> Ole32Util.getGUIDFromString("{4D36E96B-E325-11CE-BFC1-08002BE10318}")
>>> };
>>>
>>> Notification Registration Code :
>>>
>>> HWND hWnd = new HWND();
>>> hWnd.setPointer(Native.getWindowPointer(window));
>>>
>>> DEV_BROADCAST_DEVICEINTERFACE filter = new
>>> DEV_BROADCAST_DEVICEINTERFACE(); filter.dbcc_devicetype = new
>>> DWORD(MyUser32.DBT_DEVTYP_DEVICEINTERFACE);
>>> filter.dbcc_name = new char[1];
>>> filter.dbcc_reserved = new DWORD(0);
>>> filter.dbcc_size = new DWORD(filter.size());
>>>
>>> for(int i=0;i<MyUser32.arrGuid.length;i++)
>>> {
>>> filter.dbcc_classguid = MyUser32.arrGuid[i];
>>> LONG retVal = MyUser32.MYINSTANCE.RegisterDeviceNotification(hWnd, filter.getPointer(), MyUser32.DEVICE_NOTIFY_WINDOW_HANDLE);
>>> if (retVal.longValue() == 0)
>>> {
>>> System.out.println("Error registering for usb: " + Native.getLastError());
>>> }
>>> else
>>> {
>>> System.out.println("Returned value 0 for registerDeviceNotifcation"+retVal.longValue());
>>> }
>>> }
>>>
>>> Let us know if you need any more information.
>>>
>>> Thanks
>>> Shankar
>>> Mailgate Notification
>>> ---------------------------------------------------------------------
>>> The information in this email is confidential and is intended solely for the addressee(s). Access to this email by anyone else is unauthorized. If you are not an intended recipient, you must not use or disseminate the information contained in this email.
>>> ---------------------------------------------------------------------
>>>
>>
>>
>> Mailgate Notification
>> ---------------------------------------------------------------------
>> The information in this email is confidential and is intended solely for the addressee(s). Access to this email by anyone else is unauthorized. If you are not an intended recipient, you must not use or disseminate the information contained in this email.
>> ---------------------------------------------------------------------
>>
>
>
> Mailgate Notification
> ---------------------------------------------------------------------
> The information in this email is confidential and is intended solely for the addressee(s). Access to this email by anyone else is unauthorized. If you are not an intended recipient, you must not use or disseminate the information contained in this email.
> ---------------------------------------------------------------------
>