Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Merging Java and Win32: A new way to develop Windows applications

Learn how to write Win32 applications in Java instead of C++ -- and save yourself some time and effort!

  • Print
  • Feedback
The news media has focused attention on a number of mergers in recent weeks. Banks, automotive corporations, and retail chains have announced that they are merging. Can you imagine the shock if Sun Microsystems and Microsoft ever decide to merge? Well, I don't think we should hold our breath. I do, however, think that Sun and Microsoft could learn a thing or two from each other. After all, both companies have developed good products -- namely Java and Win32. In my opinion, the Java learning curve is much shorter than the C++ learning curve. At the same time, Win32 is one important reason why Microsoft has Windows 95/NT running on some umpteen million PCs. It seems only natural to merge Java and Win32 to give give developers the edge they need to create better Windows applications in shorter amounts of time. That's the focus of this article.

In the beginning...

The first Windows applications were written in the C language. While C was okay for small applications, developers found it difficult to use this language to organize larger applications. The problem centered around the Windows messaging model and the fact that C is a structured rather than an object-oriented language. Traditional applications using C would create a main window and assign a callback function (known as a window procedure) to this window. Whenever anything of consequence happened to this window, Windows would fire a message to the window by calling the window procedure. The window procedure would respond by first identifying the message via a huge switch-case statement and then process the message. As is often the case, state would need to be saved via local static variables or global variables. A large application could result in many such variables. This paradigm worked well for smaller applications but proved to be detrimental to larger applications. Something had to be done.

The C language evolved from a structured language into an object-oriented language -- a language called C++. The nice thing about an object-oriented language is that it gives developers the ability to model real-world entities in a more natural way by using objects.

A few years ago, Microsoft released a tool for developers who wanted to create Windows applications using C++. This product became known as Visual C++. One of the features introduced with Visual C++ was an application framework known as Microsoft Foundation Classes (MFC). The MFC framework is a collection of C++ classes, written and tested by Microsoft's developers, that implements a lot of basic Windows functionality. Many software concepts -- from toolbars and status bars to a document-view model based on the Model-View-Controller architecture -- have been implemented in MFC. The idea behind MFC is to save time during development by using MFC code for most of the application and then extending MFC to provide that application's unique capabilities -- via the fundamental object-oriented concepts of encapsulation, inheritance, and polymorphism.

  • Print
  • Feedback

Resources
  • The zip archive contains all of the necessary C++/Java source and deployment files used by this article http://www.javaworld.com/jw-07-1998/java-win32/zip.zip
  • I've found the following resources beneficial when I'm writing applications that use the Java native Interface (JNI).
  • Essential JNI Java Native Interface, by Rob Gordon. PublisherPrentice Hall, Copyright1998. This book dives into JNI, and covers many techniques for calling C++ code with Java and calling Java with C++ code. One chapter focuses on the Invocation API and how to construct a C++ application which embeds the Java Virtual Machine -- in other words, how to create your own AppletViewer.exe program.
  • The JavaWorld article Use native methods to expand the Java environment provides an introductory look at the Java Native Interface http://www.javaworld.com/javaworld/jw-07-1997/jw-07-javadev.html