7 messages in net.java.dev.jna.usersRe: [jna-users] Trivial question.
FromSent OnAttachments
Jorge Nieto-MadridSep 9, 2008 3:47 am 
Timothy WallSep 9, 2008 6:24 am 
Jorge Nieto-MadridSep 11, 2008 4:08 am 
Timothy WallSep 11, 2008 6:02 am 
Jorge Nieto-MadridSep 11, 2008 8:43 am 
Timothy WallSep 11, 2008 9:07 am 
Jorge Nieto-MadridSep 15, 2008 5:10 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Trivial question.Actions...
From:Jorge Nieto-Madrid (jorg@gmail.com)
Date:Sep 11, 2008 4:08:32 am
List:net.java.dev.jna.users

I was interested in learning how JNA relates a memory Address (an integer) with the contains of the addressed memory. In C++ a pointer is just a variable that stores a memory address that you can assign to and also dereference, so if a function returns a memory address (pointer) you can dereference it to view its contents. I don't know how this would be possible in JNA (I read the doc. but it seems you can only see the contents of a pointer but no the address). Anyway this is the code I would like to map to JNA:

#include <windows.h> #include <stdio.h> #include <math.h> #include "VirtualDevice.h"

#define DOF1 8 #define DOF 6

//CreateFileMappingW, CloseHandle, MapViewofFile, RtlMoveMemory and FlushViewOfFile are defined in Kernel32.dll but C imports them implicitly typedef struct tag_VIRTUAL_DEVICE { //VIRTUAL DEVICE - STATE float fPositionIn[DOF1]; float fVelIn[DOF]; short fCamera[320*240]; int fIdentifier; }

static VIRTUAL_DEVICE *pVirtualDevice;

static HANDLE hShare;

BOOL OpenShareMemory(char *cpName) { if( pVirtualDevice != NULL){ MessageBox(NULL,"Error: THE SHARE MEMORY WAS ALREADY OPEN", "Error",MB_OK); return FALSE; }

hShare = CreateFileMappingW((HANDLE)0xFFFFFFFF,NULL, PAGE_READWRITE, 0, sizeof(VIRTUAL_DEVICE), cpName);

if(hShare == NULL){ MessageBox(NULL,"Error:Open Virtual Device::CreateFileMapping","Error",MB_OK); return FALSE; }else{ pVirtualDevice = (VIRTUAL_DEVICE *) MapViewOfFile(hShare,FILE_MAP_ALL_ACCESS, 0, 0, sizeof(VIRTUAL_DEVICE)); } if(pVirtualDevice == NULL){ MessageBox(NULL,"Error: Open Virtual Device::MapViewOfFile","Error",MB_OK); return FALSE; } return TRUE; }

BOOL CloseShareMemory() { if(pVirtualDevice != NULL) { if( FlushViewOfFile(pVirtualDevice, sizeof(VIRTUAL_DEVICE)) == 0){ MessageBox(NULL,"Error:Close Virtual Device::FlushViewOfFile","Error",MB_OK); return FALSE; } if( UnmapViewOfFile(pVirtualDevice) == 0){ MessageBox(NULL,"Error: Close Virtual Device::UnmapViewOfFile","Error",MB_OK); return FALSE; } if( CloseHandle(hShare) == 0){ MessageBox(NULL,"Error: Close Virtual Device::CloseHandle","Error",MB_OK); return FALSE; } pVirtualDevice = NULL; return TRUE; }else{ MessageBox(NULL,"Error: THE SHARE MEMORY WAS ALREADY CLOSE","Error",MB_OK); return FALSE; } }

BOOL ReadShareMemory(VIRTUAL_DEVICE *pfData) {

if(pVirtualDevice == NULL){ MessageBox(NULL,"Error:Read Virtual Device CANT FIND THE SHARE MEMORY","Error",MB_OK); return FALSE; } // RtlMoveMemory((VIRTUAL_DEVICE *)pfData, (VIRTUAL_DEVICE *)pVirtualDevice, sizeof(VIRTUAL_DEVICE));

return TRUE; }

BOOL WriteShareMemory(VIRTUAL_DEVICE *pfData) {

int i=0;

if(pVirtualDevice == NULL){ MessageBox(NULL,"Error:Read Virtual Device CANT FIND THE SHARE MEMORY","Error",MB_OK); return FALSE; }

for(i=0;i<DOF1;i++) { pVirtualDevice->fPositionIn[i] = pfData->fPositionIn[i]; }

for(i=0;i<DOF;i++) { pVirtualDevice->fVelIn[i] = pfData->fVelIn[i]; }

for(i=0;i<DOF2*0.5;i++) { pVirtualDevice->compensacion[i] = pfData->compensacion[i]; }

for(i=0;i<320*240;i++) { pVirtualDevice->fCamera[i] = pfData->fCamera[i]; }

pVirtualDevice->fIdentifier[i] = pfData->fIdentifier[i];

if( FlushViewOfFile(pVirtualDevice, sizeof(VIRTUAL_DEVICE)) == 0){ MessageBox(NULL,"Error: Close Virtual Device::FlushViewOfFile","Error",MB_OK); return FALSE; } return TRUE; }

//END

If I map HANDLE to int, should I map: pVirtualDevice = (VIRTUAL_DEVICE *) MapViewOfFile(hShare,FILE_MAP_ALL_ACCESS, 0, 0, sizeof(VIRTUAL_DEVICE)); to this: - Pointer pVirtualDevice = MapViewOfFile(hShare, FILE_MAP_ALL_ACCESS, 0, 0, SIZE_V_DEVICE); //I define manually FILE_MAP_ALL_ACCESS and SIZE_V_DEVICE

I another file I define the Kernel32 Interface public interface Kernel32 extends StdCallLibrary { // Method declarations, constant and structure definitions go here Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("C:\\windows\\system32\\kernel32.dll", Kernel32.class);

Kernel32 SYNC_INSTANCE = (Kernel32) Native.synchronizedLibrary(INSTANCE);

public static final int PAGE_READONLY = 0x02; public static final int PAGE_READWRITE = 0x04; public static final int PAGE_WRITECOPY = 0x08;

public static final int FILE_MAP_COPY = 0x0001; public static final int FILE_MAP_WRITE = 0x0002; public static final int FILE_MAP_READ = 0x0004; public static final int FILE_MAP_ALL_ACCESS = 0xF001F; public static final int INVALID_HANDLE_VALUE = -1;

Pointer CreateFileMappingW(int hFile, int lpFileMappingAttributes, int flProtect, int dwMaximumSizeLow, int dwMaximumSizeHigh, String lpName); ....

I tried this way but I did not work. Any suggestions?? thanks.

2008/9/9 Timothy Wall <twal@dev.java.net>

You'll need to be more explicit about what you want to do. Fundamentally, you can't do pointer arithmetic in Java. You can get the same results in JNA, but if you don't understand the underlying concepts of pointers and memory, you'd best put on some asbestos gloves.

On Sep 9, 2008, at 6:48 AM, Jorge Nieto-Madrid wrote:

Hi!

In C++ you are able to write:

void *p; char b[6] = {1,2,3,4,5,6};

p = &b[0];

printf("The address of b is: %d", p ); for(int i=0;i<6;i++){ printf("%d\n", *p); p++; }

How can I map this to JNA USING Pointer (and/or PointerByReference). I am trying to understand how Jna's Pointers work. Please don't just ask: use byte[] or something like that. If I understood the documentation, one can map void * into Pointer, but I don't know how to make a Pointer to point at a variable and then dereference it like in my trivial example.

Thanks in advance.