MS Visual J++ Tips


Using names for Java methods that differ from the name the DLL uses

You may want to use a name for a Java method that is different from the name the DLL uses when it exports the function.  For example. you may  need to conform to naming conventions prescribed by your own site. In that case, just use the @dll.import directive along with the entrypoint modifier.  For example. if you wanted to use the GetSysColor function, but wanted to change the first letter to lowercase, you would include the following:

/**@dll.import ("USER32",entrypoint="GetSysColor") */
  static native int getSysColor(int nIndex);

The entrypoint modifier has now set up the aliasing for you.

Use J++ 6.0's Dynamic Syntax Checking to find your errors as you type

As you enter code in the new and improved J++ 6.0 Text Editor, compiler errors you may be making are underlined with a red squiggle.  Microsoft calls it Dynamic Syntax Checking, a feature of their IntelliSense technology, and it can help save you time by pointing out your errors before you compile. If you rest your cursor over the red squiggle, you'll get an Error Tip to help you correct your mistake. And, if you click the right mouse button on the red squiggle, you can select Error Help from the
short-cut menu to get online help for the error that's been identified.

If you haven't seen any red squiggles while entering code, Dynamic Syntax Checking may be disabled. To enable it, (or to disable it you're a long-time Java user that rarely makes compiler errors) do the following:

1. On the Tools menu, click Options.

2. In the Options dialog box, expand the Text Editor node (click on the + sign next to it).

3. Select Java Tasks.

4. In the Tasks group of the property page, check (or uncheck) "Check syntax as you type"

5. In the Error Display group of the property page, check (or uncheck) "Underline syntax errors as you type"

Although occasionally the Error Tip is the less-than-helpful "Syntax error", Dynamic Syntax Checking can show you where your errors are before you compile your code, which can be a real time saver, particularly for beginners.

Use Conditional Compilation for debugging J++ 6.0 applications

The newest version of J++ includes the ability to do conditional compilation, a handy mechanism for debugging your code, particularly if you are working on larger projects. Similar to the #ifdef/#else/#endif structure available to C programmers, J++'s conditional compilation allows you to include or exclude entire blocks of code at run time using the conditional directives #if, #else, #endif, #define, and #undef. Here's an example of how conditional directives work:

#define DEBUG

#if DEBUG
System.out.println("We have a problem");
#else
System.out.println("All's well");
#endif

The #define statement sets the identifier DEBUG (you can substitute any name, of course) to true. That would cause the statement within the #if block to execute. Simply removing the #define statement, or changing it to #undef DEBUG, will cause the #else block to execute.

How might you put the power of conditional compilation to work? Here are a few examples:

* Include diagnostic code during development, and then exclude it all at run time by simply removing the one #define statement.
* Exclude code that you think you may want to include again at a later date.
* Switch among several code sections to experiment with different implementations by #define-ing different identifiers.

Write to the NT Event Log from a Java class

Ever wondered how you can log something to the NT Event log from a Java class?  It's easy, if you use the com.ms.util.EventLog class that ships with the Microsoft SDK for Java.  Try this out:

Step 1:  Write and compile this class with Visual J++:

import com.ms.util.EventLog;

public class EventLogTest


     public static void main (String[] args)  {

         EventLog myEventlog = new EventLog("EventLogTest");
         myEventlog.reportEvent(EventLog.INFORMATION, (short)0, 0,
         "This is a test event log message");
      }
}


Step 2:  Run this class on your NT server using JView.

Step 3:  Open up the NT Event Viewer application, and look at your Application Log.  Your event should be at (or near) the top.


Legal Disclaime
THE INFORMATION IN THIS PUBLICATION IS PROVIDED "AS IS". WE EXPRESSLY DISCLAIMS ALL REPRESENTATIONS AND WARRANTIES OF ANY KIND REGARDING THE CONTENTS OR USE OF THE INFORMATION INCLUDING, BUT NOT LIMITED TO, EXPRESS AND IMPLIED WARRANTIES OF ACCURACY, COMPLETENESS, MERCHANTABILITY, FITNESS FOR A PARTICULAR USE, OR NON-INFRINGEMENT. IN NO EVENT SHALL WE BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, LOST BUSINESS OR LOST DATA, RESULTING FROM THE USE OR RELIANCE UPON THE INFORMATION, EVEN IF WE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT APPLY TO YOU.