2 messages in net.java.dev.jna.usersRe: [jna-users] twain & callback
FromSent OnAttachments
Krull, Hans-GünterOct 24, 2008 6:23 am 
Timothy WallOct 24, 2008 11:51 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] twain & callbackActions...
From:Timothy Wall (twal@dev.java.net)
Date:Oct 24, 2008 11:51:57 am
List:net.java.dev.jna.users

PASCAL is the same as WINAPI, which maps to the __stdcall calling convention, so use StdCallLibrary and StdCallCallback. If you're not using windows, then that's not relevant.

Generally a callback is something you pass to native code that the native code later invokes. You make no calls to native code in your example, but rather call your callback directly from Java, which makes little sense.

I'd recommend going back to your C example code and make sure you're following the same steps.

On Oct 24, 2008, at 9:23 AM, Krull, Hans-Günter wrote:

Hi , I have some problem to access the twain interface using a callback function.

Here is the c-code for the function (Plattform is MS-Windows):

#ifdef _MSWIN_

TW_UINT16 FAR PASCAL DSM_Entry( pTW_IDENTITY pOrigin,

pTW_IDENTITY pDest,

TW_UINT32 DG,

TW_UINT16 DAT,

TW_UINT16 MSG,

TW_MEMREF pData);

and my JNA interface:

public interface Twain extends Library {

public static interface DSM_Entry extends Callback {

public int callback(TW_IDENTITY origin, //source of message

TW_IDENTITY dest, //destination of message

int dg, //data group ID: DG_xxxx

short dat, //data argument type: DAT_xxxx

short msg, //message ID: MSG_xxxx

Structure data); //pointer to data (TW_MEMREF = LPVOID)

}//end class DSM_Entry

}//end interface Twain

The C-structure of the pTW_IDENTITY from twain.h:

/* DAT_IDENTITY. Identifies the program/library/code resource. */

typedef struct {

TW_UINT32 Id; /* Unique number. In Windows, application hWnd */

TW_VERSION Version; /* Identifies the piece of code */

TW_UINT16 ProtocolMajor; /* Application and DS must set to TWON_PROTOCOLMAJOR */

TW_UINT16 ProtocolMinor; /* Application and DS must set to TWON_PROTOCOLMINOR */

TW_UINT32 SupportedGroups; /* Bit field OR combination of DG_ constants */

TW_STR32 Manufacturer; /* Manufacturer name, e.g. "Hewlett- Packard" */

TW_STR32 ProductFamily; /* Product family name, e.g. "ScanJet" */

TW_STR32 ProductName; /* Product name, e.g. "ScanJet Plus" */

} TW_IDENTITY, FAR * pTW_IDENTITY;

The java code I tried:

public class TW_IDENTITY extends Structure {

public int Id; // hWnd

public TW_VERSION Version; // TW_VERSION

public short ProtocolMajor; // TW_UINT16

public short ProtocolMinor; // TW_UINT16

public int SupportedGroups; // TW_UINT32

public String Manufacturer; // TW_STR32

public String Productfamily;

public String ProductName;

}//end class

The pData should be a pointer to the user interface:

C:

/* DAT_USERINTERFACE. Coordinates UI between application and data source. */

typedef struct {

TW_BOOL ShowUI; /* TRUE if DS should bring up its UI */

TW_BOOL ModalUI; /* For Mac only - true if the DS's UI is modal */

TW_HANDLE hParent; /* For windows only - Application window handle */

} TW_USERINTERFACE, FAR * pTW_USERINTERFACE;

Java:

public class TW_USERINTERFACE extends Structure {

public boolean ShowUI;

public boolean ModalUI;

public Pointer hParent;

} //end class TW_Userinterface

I tried this interface but got an Exception in thread "main" java.lang.NullPointerException

For the sake of completeness the source of the main program:

import com.sun.jna.*;

import image.twain.*;

public class JCam {

public interface Twain extends Library {

public static interface DSM_Entry extends Callback {

public int callback(TW_IDENTITY origin, //source of message

TW_IDENTITY dest, //destination of message

int dg, //data group ID: DG_xxxx

short dat, //data argument type: DAT_xxxx

short msg, //message ID: MSG_xxxx

Structure data); //pointer to data (TW_MEMREF = LPVOID)

}//end class DSM_Entry

}//end interface Twain

public static Twain.DSM_Entry dsm_entry;

public static TW_IDENTITY origin = new TW_IDENTITY();

public static TW_USERINTERFACE ui = new TW_USERINTERFACE();

public static void main(String[] args) {

//

Twain twain = (Twain) Native.loadLibrary("twain_32", Twain.class);

//Definition of Origin

origin.Id = 0; //null;

origin.Version.MajorNum = 0;

origin.Version.MinorNum = 1;

origin.Version.Language = TWAIN.TWLG_GER;

origin.Version.Country = TWAIN.TWCY_GERMANY;

origin.Version.Info = "JCam 0.1";

origin.ProtocolMajor = TWAIN.TWON_PROTOCOLMAJOR;

origin.ProtocolMinor = TWAIN.TWON_PROTOCOLMINOR;

origin.SupportedGroups = TWAIN.DG_CONTROL | TWAIN.DG_IMAGE;

origin.Manufacturer = "Hans-Guenter Krull";

origin.Productfamily = "Java based image capture";

origin.ProductName = "JCam";

int test = dsm_entry.callback(origin,

(TW_IDENTITY) null,

TWAIN.DG_CONTROL,

TWAIN.DAT_USERINTERFACE,

TWAIN.MSG_ENABLEDS,

ui);

}

} //end of class

Sorry for the stupid question