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,106
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:
 

jmon

Well-Known Member
Licensed User
Longtime User
Cookies are required to use this site. You must accept them to continue using the site. Learn more…