001: /**
002: * @creation 28/02/05
003: *
004: */package com.memoire.vainstall;
005:
006: import java.io.File;
007: import java.io.IOException;
008:
009: /**
010: * A means of creating Windows shortcuts using the JNI interface.
011: * @author Dick Repasky
012: *
013: */
014:
015: public class JNIWindowsShortcut {
016: /** Value for location.
017: If you use this value,
018: the value of linkname
019: should include the
020: full name of the
021: directory that will
022: contain the new link. */
023: public static final int CUSTOM_LOCATION = 0;
024: /** Value for location. */
025: public static final int ON_DESKTOP = 1;
026: /** Value for location. */
027: public static final int ON_START_MENU = 2;
028:
029: /** Value for forwhom. */
030: public static final int USER_ONLY = 0;
031: /** Value for forwhom. */
032: public static final int EVERYBODY = 1;
033:
034: /** Value for openas. */
035: public static final int SHOW_NORMAL = 0;
036: /** Value for openas. */
037: public static final int SHOW_MAX = 1;
038: /** Value for openas. */
039: public static final int SHOW_MIN = 2;
040:
041: public static boolean isDllLoaded_;
042:
043: public static void loadLib() {
044: if (!isDllLoaded_) {
045: isDllLoaded_ = true;
046: System.load(LIB_NAME);
047: }
048: }
049:
050: /** Name of dll. */
051: public static String LIB_NAME = "JNIWinShortcut";
052: private static final String DEFAULT_LINK = "LaunchIt";
053:
054: /** Create a Windows Shortcut using the information provided.
055: @parm target name - binary to be executed. Required.
056: Must be fully qualified path to work.
057: @parm workingdir - name of working directory for running
058: binary. Required.
059: Must be fully qualified path to work.
060: @parm args - arguments to be passed to the binary.
061: Optional.
062: @parm linkname - name of the link file. Omit .LNK suffix!
063: Include leading directory names only if
064: location is CUSTOM_LOCATION.
065: @parm location - one of CUSTOM_LOCATION, ON_DESKTOP,
066: ON_STARTMENU.
067: @parm forwhom - one of USER_ONLY or EVERYBODY. Ignored if
068: location is CUSTOM_LOCATION.
069: @parm iconfile - name of icon file. Must be fully qualified
070: path to work.
071: @parm iconoffset - offest of icon within iconfile.
072: @parm description - speak freely here. But, be brief. I don't
073: know what the limit is.
074: */
075: public static final String createShortcut(String targetname,
076: String workingdir, String args, int openas,
077: String linkname, int location, int forwhom,
078: String iconfile, int iconoffset, String description)
079: throws IOException {
080: loadLib();
081: if (targetname == null)
082: targetname = "";
083: if (workingdir == null)
084: workingdir = "";
085: if (args == null)
086: args = "";
087: if (linkname == null || linkname.length() == 0)
088: linkname = DEFAULT_LINK;
089: if (iconfile == null)
090: iconfile = "";
091: if (description == null)
092: description = "";
093: String f = createShortcutJNI(targetname, workingdir, args,
094: openas, linkname, location, forwhom, iconfile,
095: iconoffset, description);
096: File shortcutFile = new File(f);
097: if (!shortcutFile.exists()) {
098: if (VAGlobals.DEBUG)
099: VAGlobals.printDebug("shortcut not found "
100: + shortcutFile.getName());
101: String name = shortcutFile.getName();
102: int idxDot = name.lastIndexOf('.');
103: if (idxDot > 0) {
104: name = name.substring(0, idxDot);
105: File test = new File(shortcutFile.getParentFile(), name
106: + ".pif");
107: System.err.println("file tested " + test);
108: if (test.exists()) {
109: if (VAGlobals.DEBUG)
110: VAGlobals.printDebug("shortcut guessed "
111: + test.getName());
112:
113: f = test.getAbsolutePath();
114: }
115: }
116: }
117: return f;
118: }
119:
120: /**
121: * Return the name of the directory that would contain
122: * a link of the type give.
123: * @parm location should be one of ON_DESKTOP or ON_START_MENU.
124: * @parm forwhom should be one of EVERYBODY or USER_ONLY.
125: *
126: */
127:
128: public static final String getShortcutDir(int location, int forwhom) {
129: loadLib();
130: return getLinkDir(location, forwhom);
131: }
132:
133: /**
134: * Return the name of the shortcut that would be created if
135: * the following arguments were used. Arguments are as
136: * for createShortcut.
137: * @exception java.io.IOException if full path name is
138: * longer than maximum allowed.
139: */
140:
141: public static final String getShortcutName(String linkname,
142: int location, int forwhom) throws IOException {
143: loadLib();
144: if (linkname == null || linkname.length() == 0)
145: linkname = DEFAULT_LINK;
146: return getLinkName(linkname, location, forwhom);
147: }
148:
149: /**
150: * Return maximum allowable Win32 path length (i.e.,
151: * value of the constant MAX_PATH).
152: *
153: */
154:
155: public static final int getMaxPathlength() {
156: loadLib();
157: return getMAX_PATH();
158: }
159:
160: /** Create a shortcut. */
161: private static final native String createShortcutJNI(
162: String targetname, String workingdir, String args,
163: int openas, String linkname, int location, int forwhom,
164: String iconfile, int iconoffset, String descriptions)
165: throws IOException;
166:
167: /**
168: * Get the name of the directory for links of the type
169: * given.
170: *
171: */
172: private static final native String getLinkDir(int location,
173: int forwhom);
174:
175: /**
176: * Get the full file name of a shortcut that would
177: * be created using the arguments.
178: * @exception java.io.IOException if full path name is
179: * longer than maximum allowed.
180: */
181:
182: private static final native String getLinkName(String linkname,
183: int location, int forwhom) throws IOException;
184:
185: /**
186: * Return value of Win32 MAX_PATH.
187: *
188: */
189:
190: private static final native int getMAX_PATH();
191:
192: }
|