We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › [SOLVED] Processing and (third-party) native .DLLs
Pages: 1 2 
[SOLVED] Processing and (third-party) native .DLLs (Read 5693 times)
[SOLVED] Processing and (third-party) native .DLLs
Aug 12th, 2008, 1:18am
 
Hi,
I'm trying to interface Processing to a USB radio device (ANT - comparable to a Bluetooth dongle) using a Windows interface DLL supplied by the manufacturer, using the JNI hack page as a starting point. The catch is, the tutorial is based on the idea that you are developing your own native DLL in tandem with a Java/Processing app, and have access to the DLL source code (i.e. if I'm reading it correctly, can recompile the DLL substituting JNIEXPORTs and the associated data types in place of the native calling convention).

They do provide a header file listing the DLL's exports. They are of the form:
__declspec(dllexport) BOOL ANT_Init(UCHAR ucUSBDeviceNum, USHORT usBaudrate);

(According to this page __declspec(ellexport) is a microsoft-specific language extension, but I've been successful dynamic-linking functions from it under mingw/gcc.)

Is there any hope of using this hack or similar methods without (re-)compiling the native DLL

Failing that, has anyone had experience/luck with JNA or any of the other tools that claim to interface native DLL code without going through JNI (links at the bottom of http://en.wikipedia.org/wiki/Java_Native_Interface)

Big catch #2: the DLL relies extensively on callbacks, which some of these (NativeCall) explicitly do not support and some don't specify.
Narrowing the playing field...
Reply #1 - Sep 5th, 2008, 5:07am
 
OK, a bit of progress.

The actual application I'm writing is a demo that will be used once at a conference, then thrown away. So I will feel less like I wasted my time if I can contribute working code/examples back to the community in the process. So I restricted my search to fully free, open source (both the native wrapper itself, and any reliances on access to the DLL source code, which we do not have) "Pure Processing" solutions. (Yeah, "just load up Eclipse/javac and..." could simplify the solution maybe, but Processing users aren't Processing users because they wanted to become Java developers...)

So here are the native interface options I found:

JNIWrapper 30-day trialware.

JNI we have no public DLL source code, and so can't recompile it with JNIEXPORT*s. JNI is also reputed to be very complicated and error-prone (possibly by people selling their own native wrapper products...). * It may be possible (if messy) to create another DLL from scratch that wraps the closed-source DLL functions and re-exports them JNI-aware, but my whole purpose of writing a heavily graphical demo app in Processing is the avoidance of work, and this really is starting to sound like work. This may be a last-resort try.

JNative (Java to Native Interface) It takes a lot of typing to actually call the DLL function, but this is the only one I found so far that a) does not require DLL source, writing C code, setting up a native compiler, etc., b) supports Callbacks, and c) works! I now have a working (ugly, but working!) proof of concept using JNative as a Processing library.

Java Native Access (JNA) ...maybe? Looks like nLink, but messier.

nLink Weirdness with non-Pascal calling conventions and on further inspection, won't support callbacks anytime soon. I'll post my progress so far with nLink, maybe it'll help someone else. If you know your native function uses the _stdcall convention (all the DLLs that come on your Windows CD use this) and you don't need callbacks, nLink is probably the easiest way to go.

NativeCall No callbacks; supports the _stdcall convention only

J/Invoke Trialware

JNIEasy 5-day trialware

Gluegen - auto-generate JNI wrapper?

SWIG - auto-generate wrapper DLL source?
Re: [SOLVED] Processing and (third-party) native .
Reply #2 - Sep 8th, 2008, 9:10am
 
OK, JNative tutorial is written up on the Hacks page:

http://processing.org/hacks/doku.php?id=hacks:jnative
Re: [SOLVED] Processing and (third-party) native .
Reply #3 - Dec 2nd, 2008, 4:39pm
 
Hey,

I'm a newbie when it comes to Processing but impressed so far. I've been using JNative hack as documented but have one problem:

Has anyone had any problems exporting to a windows app? My exported apps simply won't launch?

Thanx.

J.
Re: [SOLVED] Processing and (third-party) native .
Reply #4 - Dec 2nd, 2008, 8:09pm
 
Update:

I've identified the app crashed on execution of the any lines of code invoke the dll function...

i.e.      
try {int blah=org.xvolks.jnative.util.K8062D_DLL.SetData(i+1, mouseY);}
catch (NativeException e){e.printStackTrace();}
catch (IllegalAccessException e){e.printStackTrace();}

Weird thing is, everything works fine within Processing but not when exported.

J.

Re: [SOLVED] Processing and (third-party) native .
Reply #5 - Dec 3rd, 2008, 12:29pm
 
Where is the DLL? Is it in the Windows path?
Re: [SOLVED] Processing and (third-party) native .
Reply #6 - Dec 3rd, 2008, 12:32pm
 
Thanks for the response.

I think so... if by "Windows path" you mean the System32 folder, then yes, that's where it is. Should it be elsewhere?

The DLL is for a USB DMX lighting controller made by velleman. Has only four simple functions and doesn't rely on any callbacks.

Re: [SOLVED] Processing and (third-party) native .
Reply #7 - Dec 3rd, 2008, 1:42pm
 
Looks like a good place, indeed... Smiley

Disclaimer: I never use JNative and even less you DLL! Just trying to help.

Some people had issues with export function recently, you should first try it with a plain sketch.

I just tried, plain sketch and one using an external jar, it worked fine with Processing 1.0.1 on Windows XP Pro SP3.
I noticed the .exe file must have the lib folder with it to work, otherwise I get an error message:
---------------------------
Java Virtual Machine Launcher
---------------------------
Could not find the main class. Program will exit.
---------------------------
OK  
---------------------------

You write: "the app crashed on execution". Do you get an error message? A stack trace?

Mmm, I tried to run a sketch doing only println stuff then exiting, I get no output on stdout.
I made it write to a file, it isn't created when running the exported Windows application, while I got it when running in PDE.
I will investigate a bit more.
Re: [SOLVED] Processing and (third-party) native .
Reply #8 - Dec 3rd, 2008, 1:47pm
 
Hey PhiLho,

Thanks for your reply. Initially the app was just crashing, no error message, on launch but I had code calling a DLL function in the setup() function.

try {int blah=org.xvolks.jnative.util.K8062D_DLL.SetData(i+1, mouseY);}
catch (NativeException e){e.printStackTrace();}
catch (IllegalAccessException e){e.printStackTrace();}

When I moved that and triggered it using a mouse click the app launched and ran fine. Normal sketches work perfectly as applications. Soon as I clicked the mouse to run the offensive script it stalls, and I need to end process in task manager.

I'd understand if this was happening in Processing also, but it works fine in the dev environment. So frustrating to see it work perfectly, only to fall on the final hurdle!

Edited:
Not at my dev machine at the moment but will check out your lib folder issue and see if that's the cause


J.
Re: [SOLVED] Processing and (third-party) native .
Reply #9 - Dec 3rd, 2008, 2:01pm
 
If you look at Export page, you will see a number of libraries seems not to work when exported as applications (video at least).
Maybe this one suffers the same issue...

The fact my simple sketch using PrintWriter doesn't create a file after being exported, while it does when running in PDE, is another hint that this export is, sometime, problematic.
I should fill in a bug report for this last issue.

Hint: I used Sysinternals' Process Monitor to see the file activity when running the sketch. I can see the text file created when running in the PDE, and no attempt at all when running the exe file... In both cases, an input file is read OK.
Maybe you can use the same utility to check if the DLL is correctly found.
Re: [SOLVED] Processing and (third-party) native .
Reply #10 - Dec 3rd, 2008, 2:04pm
 
Thanks... don't fully understand everything there but you've given me some important keywords and in Google we trust!

Will report back.

Re: [SOLVED] Processing and (third-party) native .
Reply #11 - Dec 3rd, 2008, 2:35pm
 
OK, I was half wrong for the "text file not created" part.
The problem is that I didn't create a sketch frame: just read info, write file and exit. Apparently, the exit is too fast and doesn't let Java the time to write the file. If I display some image, I have the file created. Same if I add delay(1000); before exit() in my former sketch.

There is still the problem of not getting stdout (println output). I think that's a side effect of using ShellExecuteEx (similar to doing "start something" on the command line: it uses the "open" verb, and javaw to boot). It would need to pipe stdout (and stderr) to standard channels, or something. Perhaps more trouble than worth, since we rarely use Processing to do something else than graphics output!

Side note: the xxx.exe file is always the same: it just goes to the lib folder, and finds which sketch to run, with which jar files, in the args.txt file.
You might want to try and add your DLL to this list, who know? In this case, you should copy the DLL to the lib folder.
Re: [SOLVED] Processing and (third-party) native .
Reply #12 - Dec 4th, 2008, 1:18am
 
Ok, update...

I checked out the Export notes and implemented point number three under application Export ("you'll need to write your own main() method in order for Export to Application to work"). Yes, that's how little I know about Java. Here's what it looks like...

public class K8062D_DLL extends PApplet
{
 public static void main(String args[]){    
   PApplet.main(new String[] {
     "K8062D_DLL"     }
   );
 } etc... etc...

I think that's right, but it didn't solve the problem.

Tried moving the dll (K8062D.dll) into the lib folder and adding it to 'args.txt'. Didn't help.

Tried SysInternals process monitor and everything looked great (to the untrained eye) until I found these lines, just before the application stalled...

javaw.exe2072RegOpenKeyHKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\OLEAUT32.DLLNAME NOT FOUNDDesired Access: ReadRegistry
javaw.exe2072RegOpenKeyHKLM\SOFTWARE\Microsoft\OLEAUTNAME NOT FOUNDDesired Access: Query ValueRegistry
javaw.exe2072RegOpenKeyHKLM\SOFTWARE\Microsoft\OLEAUT\UserEraNAME NOT FOUNDDesired Access: Query Value, Enumerate Sub KeysRegistry
javaw.exe2072RegOpenKeyHKLM\SOFTWARE\Microsoft\OLEAUTNAME NOT FOUNDDesired Access: Query ValueRegistry
javaw.exe2072RegOpenKeyHKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\JNativeCpp.dllNAME NOT FOUNDDesired Access: ReadRegistry

I don't know what these mean but the NAME NOT FOUND references are the alarm bell ringers. I'm gonna rerun tomorrow in both PDE and as an App and compare the two. Any light that can be shed in the meantime would be great.

At some stage I'm gonna try JNative out on a more standard DLL, one everyone has, or can get their hands on. Unfortunately I have to work on this for the moment.

Will update if there's any change. Thanks for all the help.
Re: [SOLVED] Processing and (third-party) native .
Reply #13 - Dec 4th, 2008, 4:01pm
 
Update: Just tried this out on a different DLL file (User32.dll) which comes with windows. Same result, works fine in PDE but not when exported. Think this one might have me beaten!
Re: [SOLVED] Processing and (third-party) native .DLLs
Reply #14 - Apr 22nd, 2010, 1:50pm
 
Hi,

I followed Drmn4ea's method to build a sketch, but it shows that "org.xvolks.jnative.exceptions.NativeException: DLL AD9920APlugin.dll not found"

Is it because I didn't put the DLL file in the right path?

Thanks,

KW
Pages: 1 2