On May 19, 2008, at 10:39 AM, Dale...@coats.com wrote:
If you set the system property jna.dump_memory, Structure.toString
will dump the native memory contents as well as individual field
values.
The dump_memory showed the contents of the char array was the same,
no matter if I made it with 10 individual chars or if I made it
with the array of 10 chars.
The problem seems to be that the W32HANDLEs are getting wiped-out
when I run the .size() method, but only if I use the char array
and not if I use individual char fields!
This might be considered a bug; the Java fields should probably not
get wiped if Structure.size() is called after the Java fields have
been assigned. If you could supply a standalone JUnit test which
fails for this (or append one to StructureTest), that'd be helpful.
The cbSize field is the number of bytes in the structure,
so, perhaps not so brightly, I set the cbSize field _after_
all the other fields are populated, using the .size()
method. I found that if I just moved the population of
cbSize to be first, then I was fine:
Ideally you'd use a field initializer, but unfortunately then it'd
then get called *before* the array field initializer, so the soonest
you can do it is within an explicit constructor.
Size calculations must happen *after* any array field initializations.
NOTIFYICONDATA pnid = new NOTIFYICONDATA();
pnid.cbSize = pnid.size(); // <<<<< GOOD SPOT FOR THIS
pnid.hWnd = hInst;
pnid.uID = 0;
pnid.uFlags = Shell32.NIF_ICON | Shell32.NIF_TIP |
Shell32.NIF_MESSAGE;
pnid.uCallbackMessage = Shell32.WM_MOUSEMOVE;
pnid.hIcon = hIcon;
pnid.setSzTip("test t");
// pnid.cbSize = pnid.size(); // Wipes out hWnd & hIcon if szTip is
array
Are the Java fields wiped, or is it only the native memory that is
blank? I would expect the latter; the former would be a bug.
Structures are auto-written before function invocation and auto-read
after for convenience (otherwise this list would be full of questions
about why invocations with structures fail).
Windows works so much better when you give it something other
than null for it's handles!
Glad to hear it's working now!