Wish Library for interfacing with Windows C++ dynamic link library

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

I like using B4J for the UI parts of an app, but would like to call Windows DLL's generated by C++ for performing things like:

  • Neural networks and deep learning
  • Random number generation with various distributions
  • Complex filters and DSP

The problem I have is I don't know how to interface B4J with these DLLs. I have tried Googling, but it seems there is a huge learning curve and I am not sure where to start.

Having some sort of template or library with suitable example apps and instructions would be excellent.

I know that this is contrary to the cross-platform paradigm as it would only work with Windows, however having this facility would open up a lot of opportunities for application development using B4J

Best regards
Rob
 

rgarnett1955

Active Member
Licensed User
Longtime User
Interfacing with DLLs is technically possible but requires a lot of non-trivial work. It needs to be done with Java JNI feature.
It will be much easier to wrap similar Java libraries.

Hmm!
  • The problem I have is I can create code for these kind of functions in Matlab using the Matlab coder or compiled Simulink functions and then if necessary customize the intermediate C code myself, although this is not usually necessary. Being compiled from C/C++ they are very fast and efficient. They are also customized rather than general. To use the JNI feature I guess you need to know a lot about Java. That's my problem. I don't want to have to learn Java as it's a whole new the technology which is no a thing to start learning at 65.

  • As for Java ibraries, the same problem applies. You have to know Java and you have to know where to look.

I guess if I could see a working example that uses the JNI or Java libes I could have a go at it, but I haven't googled anything yet that seems to meet my needs.

I struggle to understand why it's so hard. Surely DLL's have rules of engagement as does Java.

Best regards
Rob
 

ranul

Member
Licensed User
Longtime User
The simplest solution will be to create a Windows C++ EXE file that uses that DLL and then you just call the EXE file from B4J
 

rgarnett1955

Active Member
Licensed User
Longtime User
The simplest solution will be to create a Windows C++ EXE file that uses that DLL and then you just call the EXE file from B4J

Hi Ranul,

That is a very good Idea. I will give it a try.

I can see a few issues though:

  • I have to write two applications instead of one. More work, more testing

  • I think this would only work for static data as passing data back and forth between B4J and the c++ app would have to be done using temporary files? So this idea would be good for batch processing not real-time? The point of using C/C++ sourced dll's for stuff like neural nets and filters is they are very fast. Using files to move data around takes most of the advantage away.

  • I guess I could use a RAM disk created by IMDisk or some other tool to get around this, but it's a bit clunky.

  • I guess you could create the c++ app as a headless process, but data exchange and control would be file based and tricky? Have you any thoughts on this?

  • Do you know of any way you could use shared ram memory for data exchange between the B4J app and the c++ app? This would avoid disk/ram disk usage.

Best regards
Rob
 

MathiasM

Active Member
Licensed User
My 2 cents on this problem:


  • I have to write two applications instead of one. More work, more testing

Partially true: Yes 2 applications, but way less work and testing than accessing via JNI.

  • I think this would only work for static data as passing data back and forth between B4J and the c++ app would have to be done using temporary files? So this idea would be good for batch processing not real-time? The point of using C/C++ sourced dll's for stuff like neural nets and filters is they are very fast. Using files to move data around takes most of the advantage away.

Never use temp files for this. This would be way to slow. See further.

  • I guess I could use a RAM disk created by IMDisk or some other tool to get around this, but it's a bit clunky.

Never use a RAM disk for this, it will be way too slow. See further

  • I guess you could create the c++ app as a headless process, but data exchange and control would be file based and tricky? Have you any thoughts on this?

Your C++ app should indeed be headless. But you should use a form of inter process communication to do this. For example via networking. Networking on a localhost is blazing fast. When you send a packet, it doesn't even leave the network stack, basically your just passing data on you L1 or L2 memory. You should be able to get tens of GB per second without any problem.

Best regards
Rob
 

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Mathias

Great Idea.

Silly me, I forgot about networks and I've already written stuff in b4J that uses local host for inter-process comm's and as you say BW is no problem. I've written so much code I forget what I've done half the time.

I will give this a try.

The main problem I have is that I don't know much about Microsoft Visual Studio as the base for writing the C++/C app. Are their any easier alternatives to this? I'm not even sure my PC has a working copy of Visual Studio. Every-time I try to use it I get problems relating to installation or licensing or some other Microsoft hurdle.

In fact if I use the headless app I need not always use a dll as I can bolt the filter or neural net into the base C++ app.

Best regards
Rob

PS:

I tried my VS 2019 installation, but I can't sign in, I get:

MicrosoftVS2019Error.png


I have tried to resolve this before, but couldn't get it to work:

Any ideas?

PS2

I found a solution for this problem:

Solution to Browser Authenticate Error -VS2019


Hii to whoever struggles on this issue,
In my case, I had an error message of,

"The browser based authentication dialog failed to complete."

So my solution is :
Step 1 : in the sign in options. Go to Accounts tab, click -> Add and reauthenticate accounts using: dropdown and select, System web browser
Step 2 : then close the tab and try again to login.
Step 3 : this now switches you to login page on your default browser.
Step 4 : try to login, Hurrrray if login is Succes else, perhaps you need to get your hands dirty yourself to solve this issue.

Thank you !

So I now have a working Version of VS!!
 
Last edited:

rgarnett1955

Active Member
Licensed User
Longtime User
My 2 cents on this problem:




Partially true: Yes 2 applications, but way less work and testing than accessing via JNI.



Never use temp files for this. This would be way to slow. See further.



Never use a RAM disk for this, it will be way too slow. See further



Your C++ app should indeed be headless. But you should use a form of inter process communication to do this. For example via networking. Networking on a localhost is blazing fast. When you send a packet, it doesn't even leave the network stack, basically your just passing data on you L1 or L2 memory. You should be able to get tens of GB per second without any problem.

Best regards
Rob
Hi

I have always wondered how they get networks so fast. Are there any good articles on the internet on how its done?

Best regards
Rob
 

MathiasM

Active Member
Licensed User
Hi

I have always wondered how they get networks so fast. Are there any good articles on the internet on how its done?

Best regards
Rob

I'm only talking loopback ofcourse. The main idea is to use UDP with as little overhead as possible, and optimize the kernel you are using. E.g: Using Fast Loopback on Windows could get roundtrips as fast as 0,003 ms (according to their own documentation): https://docs.microsoft.com/en-us/ar...th-windows-server-2012-tcp-loopback-fast-path

I think you could even tinker more in Linux, but that's just an assumption. I never needed so fast networking performance on the loopback adapter, and I have never tested it.
 

rgarnett1955

Active Member
Licensed User
Longtime User
I'm only talking loopback ofcourse. The main idea is to use UDP with as little overhead as possible, and optimize the kernel you are using. E.g: Using Fast Loopback on Windows could get roundtrips as fast as 0,003 ms (according to their own documentation): https://docs.microsoft.com/en-us/ar...th-windows-server-2012-tcp-loopback-fast-path

I think you could even tinker more in Linux, but that's just an assumption. I never needed so fast networking performance on the loopback adapter, and I have never tested it.
Hi MM

I have cooked up a server in C++ VS 2019 using IPPROTO_TCP. The server does a cubic spline of a single column of doubles. I'm only sending batches of max 4000 doubles at a time so the network cost is very small. Most of the time is used up by the linear solver calc that uses an n-1 x n matrix. Initially I single threaded the calc., but it was only about 2.5 times quicker than B4J for the same data. I broke the calc up into two or four threads depending on data size and it now flys. A 4000 point data-set with a an interpolation ratio of two i.e. an out put of 8000 points takes 70 seconds using native B4J and only with the speedySplineServer 0.6 seconds or about 120 times faster.

SplineTest.png


For most purposes where you wouldn't display more than a few hundred points B4J is plenty fast enough, but I'm using the spline to convert weather data from ten minutes to five to match a power monitoring data base that uses turns two second data into five minute aggregates. There are 16 columns to be converted, temperature max min, wind speed etc and there are tens of thousands of records so a fast interpolator is an asset. I could have got away with a linear interpolation, but I've always like splines, but have never developed an app to use them. Of course I can't use splines directly for polar measurement as you would have to decompose them into there orthoganals, spline those and then convert back to angles using atan. I'm not going to do that, I'll just use linear interpolation of the direction orthogonal and then atan them. There are limits!!

I will have a go at using UDP to see if there are any advantages, but I suspect that UDP won't make much difference as all the heavy lifting is done in the calcs. I have used UDP for real-time data over dedicated network links and if you wrap it in Modbus like protocol that will handle errors it works really well and is very efficient.

I will publish the project on this website over the next couple of days. I will post the source for the speedySplineServer, but I have included the executable in the B4J apps directory of the demo app. The demo app automatically starts the server from this location. So people can use the exe file without worrying about setting it up in VS or somem other IDE to compile it.

The next thing I will do is create a fast and random number generator that will produce "accurate" normal distributions.

Again thanks for the suggestion.

Best regards
Rob
 
Last edited:
Top