Does anyone know how to call a VC++ DLL from Java?
I want to control a standard media player through a programming language.
For example from a Java program, call a VC++ or VB DLL that controls Windows Sound Recorder (to record) and then Win Media Player (or
Real Audio or Quicktime) to playback sections of audio.
Do I need to buy VC++ (dont laugh... the Java SDK is free).
Any help greatly appreciated.
thanks,
Anil
[409 byte] By [
anilp1a] at [2007-10-2 0:15:22]

Calling/Debugging a DLL is done in a couple of steps.
Any C++ compiler that can created a Windows DLL is fine. The difficulty will be in the MFC macros and perhaps the com macros. Overtime they are changed and improved. I have to admit for the most part Microsoft does a good job keeping most interfaces the same over time. But you need to cautious. A little research before using a call you be sufficient. But say for example you are using COM and doing all the data/method binding and calls youself. No problem.
1-Load the DLL in the java main code. Be sure the DLL can be found by specifying the path in the load command or putting the path in the system PATH environment variable.
2-When the DLL loads, windows calls it at its DLLEntry point. The purpose is for the DLL to perform any process and thread specify initialization. There are 4 possible calls to DLLEntry. Each of these calls is delineated by a reason code that is passed as an argument in the call. The reasons are:
Process attach
Thread attach
process detach
thread detach
At each entry you would perform the appropriate initialization. As an example is you had a multi-threaded COM device you would call CoInitialize some where in the ProcessAttach call. It is the only time these calls are made. Java code is multi-threaded so the thread attach/detach calls will be made throughout the life of the process.
3-In the Java code create a set of JNI calls that are of the form.
<return value> public native <method_name> (<arguments>); within a class definition. It becomes the stub entry in the DLL. Then a corresponding DLL entry point is created in the DLL. To insure you get the correct spelling/syntax, compile the java code and a C++ header file is created in the "Generated Source" folder. Use the header file as the template for the code body.
There is an online tutorial that discussing the JNI interface. It is quite handing explaining all the JNI calls and their arguments as well as handling and throwing exceptions. You can also buy a soft cover book on the JNI intderface that you can buy.
Be sure to handle catch ALL exceptions. Not necessarily handle it, but it least catch it. Otherwise you could spend a lot of time tracking down an error that was caused by a uncaught exception.
Lastly debugging the DLL. That is the easy part.
Just below the DLL Entry put something like this.
#ifdef __DEBUG_MY_DLL__
boolean postMessage = true;
if (postMessage){
->Display a system dialog box and include the process ID<--
}
#endif
When the java code loads the dll the message will be displayed and give the process ID. Now you start your friendly symbolic debugger
1- attach to the process,
2- load the DLL modules
3- load the DLL symbol table
4- open the DLL source
5- set your breakpoints in the DLL
6- set the variable "postMessage" to false.
go!
The next time the DLL entry is called the message will not be displayed. But that is OK because you have your breakpoints set!
enjoy!
carl