B4J Library jDotNetBridge library

This awesome (when you think what is going on here) demonstration program shows a B4J application running VBScript code on a Windows ActiveX object, the MS Script Control, hosted by a .NET managed library written in C#.

The more technically competent amongst you can now access Windows resources from your B4J application following the template given here by writing a couple of libraries that talk to each other across a native code interface. The Java native code interface is provided by the Java JNA library and the .NET code native interface is provided by a Visual Studio NuGGet, UnmanagedExports, that post processes the managed library to expose managed method calls as native ones.

The archive contains a document, UnmanagedExports.rtf, that reveals the gory details in all (well most) of their splendour. Familiarity with a Java IDE or at least Java code and the Simple Library Compiler will be needed to create your own B4J library. Familiarity with Visual Studio and a .NET language will be needed to generate your .NET library that exposes whatever Windows functionality you require, be it via COM Interop or P/Invoke. Source code for both libraries used in this demo is included in the library archive.

I overlooked an early problem I had, see post #3. You need to open jna-4.0.0.jar with 7-Zip, double click on META-INF, select and right-click on MANIFEST.MF and select Edit. It will open in Notepad. Find the line "Implementation-version: 4.0.0 (b2)" and either delete the entire line or edit it to read "Implementation-version: 4.0.0". Save and exit Notepad and 7-Zip should ask if you want to update the archive - which you do.
 

Attachments

  • jDotNetBridgeDemo1.0.zip
    23.7 KB · Views: 1,088
Last edited:

agraham

Expert
Licensed User
Longtime User
Ah! Sorry, I forgot about that. It was one of the first (of many) problems I had to solve. Open jna-4.0.0.jar with 7-Zip, double click on META-INF, select and right-click on MANIFEST.MF and select Edit. It will open in Notepad. Find the line "Implementation-version: 4.0.0 (b2)" and either delete the entire line or edit it to read "Implementation-version: 4.0.0". Save and exit Notepad and 7-Zip should ask if you want to update the archive - which you do.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Can you upload the jna-4.0.0.jar example you have created?
The jDotNetBridgeDemo is effectively a demo of "pure" JNA where it is used to load a native library, in this case MSScript.dll, and access its methods.

The jJNA library that comes next, which I haven't yet finished, uses jna-platform-4.0.0.jar which uses jna-4.0.0.jar to load and expose some native APIs for Windows (a whole lot!), Unix( a few X11 calls) and Mac(very little).
 

jmon

Well-Known Member
Licensed User
Longtime User
Cool!

It works very well, thanks Agraham. Here is a quick test that opens your CD Drive with AutoIt COM (you need autoit Installed http://www.autoitscript.com/site/autoit/downloads/):

B4X:
Sub btnGo_Action
    'Change your drive here first:
    Dim MyCDDrive As String = "E:"
    Dim params(1) As String
   
    Dim s As StringBuilder : s.Initialize
    s.Append("Dim oAutoIt").Append(CRLF)   
    s.Append("Set oAutoIt = CreateObject(""AutoItX3.Control"")").Append(CRLF)
    s.Append("oAutoIt.CDTray """ & MyCDDrive & """, ""open""").Append(CRLF)
    s.Append("Set oAutoIt = nothing").Append(CRLF)
   
    params(0) = ""
    DNB.SetAllowUI(True)
    DNB.SetScriptTimeout(1)
   
    DNB.AddCode(s.ToString)
    Log(DNB.Run("Test", params))
    Log("Timed out = " & DNB.TimedOut)
    Log("Exception flag = " & DNB.ExceptionFlag & " : " & DNB.ExceptionMsg)
End Sub
 

jmon

Well-Known Member
Licensed User
Longtime User
Agraham,
I was wondering if you would be interested in sharing the source code for creating a library like the speech library you did. I would like to port AutoItX to B4J, but I don't know how to code the library in C or Java. I was thinking that starting from your work could be a good start, and I could learn from it.

Here is a list of functions that could be accessible this way and they would be very handy in windows:
AutoItSetOption: Changes the operation of various AutoIt functions/parameters.
BlockInput: Disable/enable the mouse and keyboard.
CDTray: Opens or closes the CD tray.
ClipGet: Retrieves text from the clipboard.
ClipPut: Writes text to the clipboard.
ControlClick: Sends a mouse click command to a given control.
ControlCommand: Sends a command to a control.
ControlDisable: Disables or "grays-out" a control.
ControlEnable: Enables a "grayed-out" control.
ControlFocus: Sets input focus to a given control on a window.
ControlGetFocus: Returns the ControlRef# of the control that has keyboard focus within a specified window.
ControlGetHandle: Retrieves the internal handle of a control.
ControlGetPosHeight: Retrieves the position and size of a control relative to it's window.
ControlGetPosWidth: Retrieves the position and size of a control relative to it's window.
ControlGetPosX: Retrieves the position and size of a control relative to it's window.
ControlGetPosY: Retrieves the position and size of a control relative to it's window.
ControlGetText: Retrieves text from a control.
ControlHide: Hides a control.
ControlListView: Sends a command to a ListView32 control.
ControlMove: Moves a control within a window.
ControlSend: Sends a string of characters to a control.
ControlSetText: Sets text of a control.
ControlShow: Shows a control that was hidden.
ControlTreeView: Sends a command to a TreeView32 control.
DriveMapAdd: Maps a network drive.
DriveMapDel: Disconnects a network drive.
DriveMapGet: Retreives the details of a mapped drive.
IniDelete: Deletes a value from a standard format .ini file.
IniRead: Reads a value from a standard format .ini file.
IniWrite: Writes a value to a standard format .ini file.
IsAdmin: Checks if the current user has administrator privileges.
MouseClick: Perform a mouse click operation.
MouseClickDrag: Perform a mouse click and drag operation.
MouseDown: Perform a mouse down event at the current mouse position.
MouseGetCursor: Returns a cursor ID Number of the current Mouse Cursor.
MouseGetPosX: Retrieves the current X position of the mouse cursor.
MouseGetPosY: Retrieves the current Y position of the mouse cursor.
MouseMove: Moves the mouse pointer.
MouseUp: Perform a mouse up event at the current mouse position.
MouseWheel: Moves the mouse wheel up or down. NT/2000/XP ONLY.
PixelChecksum: Generates a checksum for a region of pixels.
PixelGetColor: Returns a pixel color according to x,y pixel coordinates.
PixelSearch: Searches a rectangle of pixels for the pixel color provided.
ProcessClose: Terminates a named process.
ProcessExists: Checks to see if a specified process exists.
ProcessSetPriority: Changes the priority of a process
ProcessWait: Pauses script execution until a given process exists.
ProcessWaitClose: Pauses script execution until a given process does not exist.
RegDeleteKey: Deletes a key from the registry.
RegDeleteVal: Deletes a value from the registry.
RegEnumKey: Reads the name of a subkey according to it's instance.
RegEnumVal: Reads the name of a value according to it's instance.
RegRead: Reads a value from the registry.
RegWrite: Creates a key or value in the registry.
Run: Runs an external program.
RunAsSet: Initialise a set of user credentials to use during Run and RunWait operations. 2000/XP or later ONLY.
RunWait: Runs an external program and pauses script execution until the program finishes.
Send: Sends simulated keystrokes to the active window.
Shutdown: Shuts down the system.
Sleep: Pause script execution.
StatusbarGetText: Retrieves the text from a standard status bar control.
ToolTip: Creates a tooltip anywhere on the screen.
WinActivate: Activates (gives focus to) a window.
WinActive: Checks to see if a specified window exists and is currently active.
WinClose: Closes a window.
WinExists: Checks to see if a specified window exists.
WinGetCaretPosX: Returns the coordinates of the caret in the foreground window
WinGetCaretPosY: Returns the coordinates of the caret in the foreground window
WinGetClassList: Retrieves the classes from a window.
WinGetClientSizeHeight: Retrieves the size of a given window's client area.
WinGetClientSizeWidth: Retrieves the size of a given window's client area.
WinGetHandle: Retrieves the internal handle of a window.
WinGetPosHeight: Retrieves the position and size of a given window.
WinGetPosWidth: Retrieves the position and size of a given window.
WinGetPosX: Retrieves the position and size of a given window.
WinGetPosY: Retrieves the position and size of a given window.
WinGetProcess: Retrieves the Process ID (PID) associated with a window.
WinGetState: Retrieves the state of a given window.
WinGetText: Retrieves the text from a window.
WinGetTitle: Retrieves the full title from a window.
WinKill: Forces a window to close.
WinList: Retrieves a list of windows.
WinMenuSelectItem: Invokes a menu item of a window.
WinMinimizeAll: Minimizes all windows.
WinMinimizeAllUndo: Undoes a previous WinMinimizeAll function.
WinMove: Moves and/or resizes a window.
WinSetOnTop: Change a window's "Always On Top" attribute.
WinSetState: Shows, hides, minimizes, maximizes, or restores a window.
WinSetTitle: Changes the title of a window.
WinSetTrans: Sets the transparency of a window. (Windows 2000/XP or later)
WinWait: Pauses execution of the script until the requested window exists.
WinWaitActive: Pauses execution of the script until the requested window is active.
WinWaitClose: Pauses execution of the script until the requested window does not exist.
WinWaitNotActive: Pauses execution of the script until the requested window is not active.
 

jmon

Well-Known Member
Licensed User
Longtime User
Top