image
 
image
NativeMethods.c


/*::.
==================================================================================================================================
=================================================� Copyright � 2003 Allen Baker �=================================================
                                                 +------------------------------+
File:       NativeMethods.c
Originator: Allen Baker (2003.12.20)
LayoutRev:  5
================================================================================================================================== */



/*::
======================================================================================================================== *//**
How to use this file:
   This file contains a "C" language implementation for a "DLL" containing "native" methods that can be called using
   "JNI" from a java program.

   <PRE>
      How to use this file:

         Java uses JNI to call the functions in this DLL to gain to the native methods in Windows. These are the
         instructions from the associated Java file for how to make this scheme work:

            To add new "native" method signatures to this file or to modify the signatures of existing native methods in
            this file:

               1) Edit this file to make the changes.

               2) Compile this file like any other java file in this package.

               3) Use "javah" to generate a file containing the "mangled" signatures for the native methods defined in
               this file. javah will write the mangled signatures into the file "cosmicabyss_com_lib_NativeMethods.h".

                  a) This is the command to run javah; notice that you MUST use the fully qualified name
                  (cosmicabyss.com.lib.NativeMethods) in oder for javah to create an output file with the correct name
                  and generate the correct mangled signatures.

                     i)  javah -jni cosmicabyss.com.lib.NativeMethods

                  or, if the file "jni.bat" is available in this directory:

                     ii) jni NativeMethods

                  b) cosmicabyss_com_lib_NativeMethods.h is "included" (by using a "#include" preprocessor directive) in
                  the header file that is part of the C language implementation of the DLL. When the contents of
                  cosmicabyss_com_lib_NativeMethods.h are changed by running javah, the next time the DLL is rebuilt,
                  the new contents will be included in the DLL build.

               5) Replace any modified signatures in the file
               "\adb\programming\language\Java\NativeMethodDLLs\NativeMethods.c".

               6) Add a new implementation in \adb\programming\language\Java\NativeMethodDLLs\NativeMethods.c for
               any new signatures

               7) Using the C language IDE, open the project
               "\adb\programming\language\Java\NativeMethodDLLs\NativeMethods.dev" and execute "rebuild all" to
               regenerate the DLL.

               8) Test that things went right by running the "main" method in this file by using the command line:

                  a) java cosmicabyss.com.lib.NativeMethods

               or, if the file "run.bat" is available in this directory:

                  b) run NativeMethods
   </PRE>
*//*
======================================================================================================================== */



/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Private  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */



/*.
==========================================================================================
This is NativeMethods.h
------------------------------------------------------------------------------------------ */
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */



   /*.
   ==========================================================================================
   This is the javah generated .h file
      ..\cosmicabyss\com\lib\cosmicabyss_com_lib_NativeMethods.h
   ------------------------------------------------------------------------------------------ */
   /* DO NOT EDIT THIS FILE - it is machine generated */
   #include <jni.h>
   /* Header for class cosmicabyss_com_lib_NativeMethods */

   #ifndef _Included_cosmicabyss_com_lib_NativeMethods
   #define _Included_cosmicabyss_com_lib_NativeMethods
   #ifdef __cplusplus
   extern "C" {
   #endif
   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    setCreated
    * Signature: (Ljava/lang/String;IIIIIII)V
    */
   JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setCreated
     (JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint, jint, jint);

   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    setLastModified
    * Signature: (Ljava/lang/String;IIIIIII)V
    */
   JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setLastModified
     (JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint, jint, jint);

   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    setLastAccessed
    * Signature: (Ljava/lang/String;IIIIIII)V
    */
   JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setLastAccessed
     (JNIEnv *, jobject, jstring, jint, jint, jint, jint, jint, jint, jint);

   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    getCreated
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
   JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getCreated
     (JNIEnv *, jobject, jstring);

   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    getLastModified
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
   JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getLastModified
     (JNIEnv *, jobject, jstring);

   /*
    * Class:     cosmicabyss_com_lib_NativeMethods
    * Method:    getLastAccessed
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
   JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getLastAccessed
     (JNIEnv *, jobject, jstring);

   #ifdef __cplusplus
   }
   #endif
   #endif
   /*.
   ==========================================================================================
   This is the end of the javah generated .h file
      ..\cosmicabyss\com\lib\cosmicabyss_com_lib_NativeMethods.h
   ------------------------------------------------------------------------------------------ */



#endif /* _DLL_H_ */
/*.
==========================================================================================
This is the end of NativeMethods.h
------------------------------------------------------------------------------------------ */



/*.
==========================================================================================
includes
------------------------------------------------------------------------------------------ */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>



/*.
==========================================================================================
defines
------------------------------------------------------------------------------------------ */
#define  CREATE_TIME  1
#define  MODIFY_TIME  2
#define  ACCESS_TIME  3



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
this function prints out an exception message appended with the string argument.
*//*
---------------------------------------------------------------------------------------------------- */
void printError(const char* aError)
   {
   printf("\nException in NativeMethods.dll: %s\n",aError);
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
this function gets the string message corresponding to the DWORD argument error code.
*//*
---------------------------------------------------------------------------------------------------- */
static LPVOID lpMsgBuf;
void errorString(DWORD aError)
   {
   FormatMessage
      (
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      aError,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL
      );
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
this function sets the specified file time to the specified value.
*//*
---------------------------------------------------------------------------------------------------- */
BOOL setFileSystemTime(const char* aFileName, int aWhichTime, LPSYSTEMTIME aTimePtr)
   {
   char        str[4096];
   FILETIME    localTime;
   FILETIME    fileTime;
   HANDLE      fileHandle;
   LPFILETIME  creationTimePtr     = NULL;
   LPFILETIME  lastAccessTimePtr   = NULL;
   LPFILETIME  lastModifiedTimePtr = NULL;
   /*.
   ==========================================================================================
   Convert from system-time format to file-time format
   ------------------------------------------------------------------------------------------ */
   if (! SystemTimeToFileTime(aTimePtr,&localTime))
      {
      printError("SetFileSystemTime cant convert time format.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   Convert from local time to gmt time
   ------------------------------------------------------------------------------------------ */
   if (! LocalFileTimeToFileTime(&localTime,&fileTime))
      {
      printError("SetFileSystemTime cant convert to GMT time.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   Select which time to set
   ------------------------------------------------------------------------------------------ */
   switch (aWhichTime)
      {
      default:
         printError("SetFileSystemTime invalid time selector.\n");
         return FALSE;
      case CREATE_TIME:
         creationTimePtr = &fileTime;
         break;
      case MODIFY_TIME:
         lastModifiedTimePtr = &fileTime;
         break;
      case ACCESS_TIME:
         lastAccessTimePtr = &fileTime;
         break;
      }
   /*.
   ==========================================================================================
   Open the file
   ------------------------------------------------------------------------------------------ */
   fileHandle = CreateFile
      (
      aFileName,
      GENERIC_WRITE,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_FLAG_BACKUP_SEMANTICS,
      NULL
      );
   if (fileHandle == INVALID_HANDLE_VALUE)
      {
      errorString(GetLastError());
      sprintf(str,"SetFileSystemTime: %s:\n\t%s",aFileName,(char*)lpMsgBuf);
      printError(str);
      LocalFree(lpMsgBuf);
      return FALSE;
      }
   /*.
   ==========================================================================================
   Set the selected time
   ------------------------------------------------------------------------------------------ */
   if (! SetFileTime(fileHandle,creationTimePtr,lastAccessTimePtr,lastModifiedTimePtr))
      {
      printError("SetFileSystemTime cannot set file times.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   Close the file
   ------------------------------------------------------------------------------------------ */
   CloseHandle(fileHandle);
   /*.
   ==========================================================================================
   We succeeded
   ------------------------------------------------------------------------------------------ */
   return TRUE;
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
this function gets the specified file time and puts it into to the specified time structure.
*//*
---------------------------------------------------------------------------------------------------- */
BOOL getFileSystemTime(const char* aFileName, int aWhichTime, LPSYSTEMTIME aTimePtr)
   {
   FILETIME    localTime;
   LPFILETIME  fileTimePtr;
   HANDLE      fileHandle;
   FILETIME    creationTime;
   FILETIME    lastAccessTime;
   FILETIME    lastModifiedTime;
   char        str[4096];
   /*.
   ==========================================================================================
   Open the file
   ------------------------------------------------------------------------------------------ */
   fileHandle = CreateFile
      (
      aFileName,
      GENERIC_READ,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_FLAG_BACKUP_SEMANTICS,
      NULL
      );
   if (fileHandle == INVALID_HANDLE_VALUE)
      {
      errorString(GetLastError());
      sprintf(str,"GetFileSystemTime: %s:\n\t%s",aFileName,(char*)lpMsgBuf);
      printError(str);
      LocalFree(lpMsgBuf);
      }
   /*.
   ==========================================================================================
   Get all the file's times
   ------------------------------------------------------------------------------------------ */
   if (! GetFileTime(fileHandle,&creationTime,&lastAccessTime,&lastModifiedTime))
      {
      printError("GetFileSystemTime cannot get file times.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   Close the file
   ------------------------------------------------------------------------------------------ */
   CloseHandle(fileHandle);
   /*.
   ==========================================================================================
   Select the desired time
   ------------------------------------------------------------------------------------------ */
   switch (aWhichTime)
      {
      default:
         printError("GetFileSystemTime invalid time selector.\n");
         return FALSE;
      case CREATE_TIME:
         fileTimePtr = &creationTime;
         break;
      case MODIFY_TIME:
         fileTimePtr = &lastModifiedTime;
         break;
      case ACCESS_TIME:
         fileTimePtr = &lastAccessTime;
         break;
      }
   /*.
   ==========================================================================================
   Convert from gmt time to local time
   ------------------------------------------------------------------------------------------ */
   if (! FileTimeToLocalFileTime(fileTimePtr,&localTime))
      {
      printError("GetFileSystemTime cant convert to local time.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   Convert from file-time format to system-time format
   ------------------------------------------------------------------------------------------ */
   if (! FileTimeToSystemTime(&localTime,aTimePtr))
      {
      printError("GetFileSystemTime cant convert time format.\n");
      return FALSE;
      }
   /*.
   ==========================================================================================
   We succeeded
   ------------------------------------------------------------------------------------------ */
   return TRUE;
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
void set
   (
   int      aWhichTime,
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName,
   jint     aYear,
   jint     aMonth,
   jint     aDay,
   jint     aHour,
   jint     aMinute,
   jint     aSecond,
   jint     aMilliseconds
   )
   {
   SYSTEMTIME   sysTime;
   const char*  fileName = (*aEnv)->GetStringUTFChars(aEnv,aFileName,0);

   sysTime.wYear         = (WORD)aYear;
   sysTime.wMonth        = (WORD)aMonth;
   sysTime.wDay          = (WORD)aDay;
   sysTime.wHour         = (WORD)aHour;
   sysTime.wMinute       = (WORD)aMinute;
   sysTime.wSecond       = (WORD)aSecond;
   sysTime.wMilliseconds = (WORD)aMilliseconds;

   setFileSystemTime(fileName,aWhichTime,&sysTime);
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
jstring get
   (
   int      aWhichTime,
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName
   )
   {
   SYSTEMTIME   fileTime;
   const char*  fileName  = (*aEnv)->GetStringUTFChars(aEnv,aFileName,0);
   char         time[128] = "0000:00:00:00:00:00:000";

   if (getFileSystemTime(fileName,aWhichTime,&fileTime))
      {
      sprintf
         (
         time,
         "%04u:%02u:%02u:%02u:%02u:%02u:%03u",
         fileTime.wYear,
         fileTime.wMonth,
         fileTime.wDay,
         fileTime.wHour,
         fileTime.wMinute,
         fileTime.wSecond,
         fileTime.wMilliseconds
         );
      }
   return ((*aEnv)->NewStringUTF(aEnv,time));
   }



/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Exported Methods  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setCreated
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName,
   jint     aYear,
   jint     aMonth,
   jint     aDay,
   jint     aHour,
   jint     aMinute,
   jint     aSecond,
   jint     aMilliseconds
   )
   {
   set
      (
      CREATE_TIME,
      aEnv, aThis, aFileName, aYear,   aMonth,
      aDay, aHour, aMinute,   aSecond, aMilliseconds
      );
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setLastModified
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName,
   jint     aYear,
   jint     aMonth,
   jint     aDay,
   jint     aHour,
   jint     aMinute,
   jint     aSecond,
   jint     aMilliseconds
   )
   {
   set
      (
      MODIFY_TIME,
      aEnv, aThis, aFileName, aYear,   aMonth,
      aDay, aHour, aMinute,   aSecond, aMilliseconds
      );
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT void JNICALL Java_cosmicabyss_com_lib_NativeMethods_setLastAccessed
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName,
   jint     aYear,
   jint     aMonth,
   jint     aDay,
   jint     aHour,
   jint     aMinute,
   jint     aSecond,
   jint     aMilliseconds
   )
   {
   set
      (
      ACCESS_TIME,
      aEnv, aThis, aFileName, aYear,   aMonth,
      aDay, aHour, aMinute,   aSecond, aMilliseconds
      );
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getCreated
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName
   )
   {
   return(get(CREATE_TIME,aEnv,aThis,aFileName));
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getLastModified
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName
   )
   {
   return(get(MODIFY_TIME,aEnv,aThis,aFileName));
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
JNIEXPORT jstring JNICALL Java_cosmicabyss_com_lib_NativeMethods_getLastAccessed
   (
   JNIEnv*  aEnv,
   jobject  aThis,
   jstring  aFileName
   )
   {
   return(get(ACCESS_TIME,aEnv,aThis,aFileName));
   }



/*:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
*//*
---------------------------------------------------------------------------------------------------- */
BOOL WINAPI DllMain
   (
   HINSTANCE  hInst,    // Library instance handle.
   DWORD      reason,   // Reason this function is being called.
   LPVOID     reserved  // Not used.
   )
   {
   switch (reason)
      {
      case DLL_PROCESS_ATTACH: break;
      case DLL_PROCESS_DETACH: break;
      case DLL_THREAD_ATTACH:  break;
      case DLL_THREAD_DETACH:  break;
      }
   /*.
   ==========================================================================================
   Returns TRUE on success, FALSE on failure
   ------------------------------------------------------------------------------------------ */
   return TRUE;
   }