Linking to a JNI Function Compiled with Visual Studio 6.0

Suppose you are writing a Java application on the windows platform and want to make a JNI call to a C function written with Microsoft Visual Studio 6.0. Well, here’s what you would do:

The ‘native’ method declaration in Java :

 public class x {	public native void   nativeC();	...}

resolves in the Java runtime to a dll call to a C function with thesignature

 JNIEXPORT void JNICALL Java_Cd_nativeC(JNIEnv *env,jobject obj)

and for Microsoft Visual C++ 6.0,

    JNIEXPORT resolves to __declspec(dllexport)   JNICALL   resolves to __stdcall

However, in Microsoft Visual C++ from Visual Studio 6 (compiler version12.x), functions declared as __stdcall have decorated names, where an @n is added to the function where n is a number (corresponding to number of argument bytes). So the above function name as output by the C compiler would be [email protected]

That poses a problem because you can’t say:

     public native void [email protected]();

in Java because its a syntax error (‘@’ not allowed in identifiers).

[email protected] will be the external name in the DLL and when you run Java and the Java application tries to call to the native dll, you will get a Java.lang.UnsatisfiedLinkError because the runtime won’t find the function inthe dll.

The fix in VS 6.0 is to add a .DEF file to the Visual Studio link command. The .DEF file can specify exports from the C code and also provide name translation. So in this case you would have a .DEF file that looks like:

 -- snip ---EXPORTS	[email protected] end --

The linker command would be:

 	link /map /DLL /DEF:cfile.def cfile.obj

you can also specify the translation directory in the linker command line as:

 	link /DLL /EXPORT [email protected] cfile.obj

but that form becomes clumsy if you have more than one exported nativemethod.

Another tip is that if you need to find out what the decorated names look like, you can write the C file, link it without the DEF file but with the /map option and then examine the link map to find the decorated names.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job