import com.ms.dll.DllLib;
import com.ms.dll.Root;
import com.ms.dll.Callback;
public class Win32ColorDlg extends Callback {
public static final int NUM_CUST_COLORS = 16;
public static final int COLOR_SIZE_IN_BYTES = 4;
public int[] cust = new int[NUM_CUST_COLORS];
public static final int CC_RGBINIT = 0x00000001;
public static final int CC_FULLOPEN = 0x00000002;
public static final int CC_ENABLEHOOK = 0x00000010;
public static final int WM_LBUTTONUP = 0x0202;
public static final int WM_CHAR = 0x0102;
public CHOOSECOLOR cc = new CHOOSECOLOR();
public static void main(String args[]) {
Win32ColorDlg dlg = new Win32ColorDlg();
int c = dlg.getColor();
System.out.println("r,g,b = " + (c & 0xFF) + "," + ((c >> 8) & 0xFF) +
"," + ((c >> 16) & 0xFF));
for (int i = 0; i < NUM_CUST_COLORS; i++)
System.out.println("custom[" + i + "] = " + (dlg.cust[i] & 0xFF) + "," +
((dlg.cust[i] >> 8) & 0xFF) + "," + ((dlg.cust[i] >> 16) & 0xFF));
}
public int getColor(){
// Allocate the necessary memory block (in bytes)
int custColorPtr = DllLib.allocCoTaskMem(NUM_CUST_COLORS *
COLOR_SIZE_IN_BYTES);
// Set the default colors of the array to white
for (int i = 0; i < NUM_CUST_COLORS; i++)
cust[i] = 0x00FFFFFF;
// Params (source array, array offset, dest ptr, # of array elements)
DllLib.copy(cust, 0, custColorPtr, NUM_CUST_COLORS);
// Create a pointer to the dialog's callback procedure
int hook = Root.alloc(this);
cc.lStructSize = DllLib.sizeOf(CHOOSECOLOR.class);
cc.hwndOwner = GetActiveWindow(); // Call API
cc.hInstance = 0;
cc.rgbResult = 0;
cc.lpCustColors = custColorPtr;
cc.flags = CC_RGBINIT | CC_ENABLEHOOK | CC_FULLOPEN ;
cc.lpfnHook = DllLib.addrOf(hook);
// Call the API
ChooseColor(cc);
// Params (source ptr, destination array, array offset
// number of array elements)
DllLib.copy(custColorPtr, cust, 0, NUM_CUST_COLORS);
// Free up allocated memory
DllLib.freeCoTaskMem(custColorPtr);
Root.free(hook);
return cc.rgbResult;
}
// To extend Callback, you must implement a public method called "callback"
public int callback(int hWnd, int msg, int wParam, int lParam) {
if (((msg == WM_CHAR) && (wParam == 32)) || (msg == WM_LBUTTONUP))
System.out.println("New color selected.");
return 0;
}
/** @dll.import("COMDLG32") */
static native boolean ChooseColor (CHOOSECOLOR pChoosecolor);
/** @dll.import("USER32") */
public static native int GetActiveWindow();
}
/** @dll.struct() */
class CHOOSECOLOR {
public int lStructSize;
public int hwndOwner;
public int hInstance;
public int rgbResult;
public int lpCustColors;
public int flags;
public int lCustData;
public int lpfnHook;
public String lpTemplateName;
}
Figure 4 MakeGuid.java
import com.ms.com._Guid;
public class MakeGuid {
public static void main(String args[]) {
_Guid g = CoCreateGuid();
String guid = StringFromIID(g);
System.out.println(guid);
}
/** @dll.import("OLE32",ole) */
static native _Guid CoCreateGuid();
/** @dll.import("OLE32",ole) */
static native String StringFromIID(_Guid g);
}
Figure 5 MakeGuid.java
import com.ms.dll.Win32Exception;
import com.ms.dll.DllLib;
public class ErrorDemo {
public static void main(String args[]) {
if (!LoadCursorFromFile("c:\\some_bogus_filename.cur")) {
Win32Exception e = new Win32Exception(DllLib.getLastWin32Error());
System.out.println("Error(" + e.getErrorCode() + ") :" +
e.getErrorDescription(e.getErrorCode()));
}
}
/** @dll.import("user32",setLastError) */
static native boolean LoadCursorFromFile (String lpFileName);
}