<div dir="ltr">Hi all<div><br></div><div>Many native SDKs allow the caller to pass a void* or long as an additional parameter. This parameter is then later provided as a reference to the original call, either in the context of a callback function call or as an attribute of an operating system object. The native SDK will not interpret the parameter in any way, neither as a number nor as a pointer. It's up to the caller to know what it represents.</div><div><br></div><div>Examples are:</div><div><br></div><div>lParam in Windows' SendMessage() - <a href="https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage">https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage</a></div><div><br></div><div>dwNewLong in Windows' SetClassLongPtr() - <a href="https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setclasslongptrw">https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setclasslongptrw</a></div><div><br></div><div>arg in POSIX's pthread_create() - <a href="https://pubs.opengroup.org/onlinepubs/000095399/functions/pthread_create.html">https://pubs.opengroup.org/onlinepubs/000095399/functions/pthread_create.html</a></div><div><br></div><div>refcon in macOS' WritePipeAsync() - <a href="https://developer.apple.com/documentation/iokit/iousbinterfaceinterface800/1639539-writepipeasync">https://developer.apple.com/documentation/iokit/iousbinterfaceinterface800/1639539-writepipeasync</a></div><div><br></div><div>Is there a way to use this from Java for Java objects, i.e. to pass a Java object as a void* and later convert the void* back to the Java object?</div><div><br></div><div>Since the FFM API exposes void* as MemorySegments, it would probably mean that a MemorySegment could be created encapsulating a reference to a Java object, and that the Java object can later be retrieved from this MemorySegment.</div><div><br></div><div>I am aware of two workarounds:</div><div><br></div><div>- In the context of a callback function, the Java callback function can be extended with an additional parameter and then the Java object can be bound to it. This function is then used to create the up call stub. Since the callback function already contains the reference, the void* parameter is no longer needed and can be passed MemorySegment.NULL. In my context (I/O processing with several thousand functions calls per second), it likely is rather inefficient as several thousand upcall stubs will need to be generated by second.</div><div><br></div><div>- Instead of the Java object, a generated integer key could be passed as the void*. The link between the key and the Java object would need to be managed in a hash map. Possible, but more of a workaround.</div><div><br></div><div>Regards</div><div>Manuel</div><div><br></div><div><br></div><div><br></div><div><br></div></div>