| 1 | /* |
| 2 | * $Id: LastHRESULT.java,v 1.2 2005/11/16 21:48:12 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | package com.moesol.bindings.platform_sdk.component_services; |
| 13 | |
| 14 | /** |
| 15 | * Provide access to the raw value for the last HRESULT returned by a |
| 16 | * COM interface call generated by tlb2java. The tlb2java code generator |
| 17 | * transforms the return type of all [dual] methods so that the [retval] parameter |
| 18 | * is instead used as the return value. If there is no [retval] parameter |
| 19 | * void is returned. The original HRESULT is checked for a failure bit |
| 20 | * and if set a COMException is thrown. |
| 21 | * |
| 22 | * Occasionally, the HRESULT encodes additional information |
| 23 | * beyond success and failure. For example, IEnumVARIANT::Next is documented |
| 24 | * as returning S_OK, if all elements requested are retrieved, but S_FALSE |
| 25 | * if there are no errors but fewer elements than requested are returned. |
| 26 | * |
| 27 | * In these cases you can use LastHRESULT to get the actual HRESULT. |
| 28 | * <pre> |
| 29 | * LastHRESULT.instance().hresult(); |
| 30 | * </pre> |
| 31 | * This returns the last HRESULT returned by the last COM call on this thread. |
| 32 | * |
| 33 | * @author Robert |
| 34 | */ |
| 35 | public class LastHRESULT extends ThreadLocal { |
| 36 | private static final LastHRESULT s_instance = new LastHRESULT(); |
| 37 | public static LastHRESULT instance() { |
| 38 | return s_instance; |
| 39 | } |
| 40 | private LastHRESULT() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The initial value is an array of size one to hold the thread local |
| 45 | * HRESULT. |
| 46 | */ |
| 47 | protected Object initialValue() { |
| 48 | return new int[1]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the thread local HRESULT. |
| 53 | * @param v |
| 54 | */ |
| 55 | public void hresult(int v) { |
| 56 | int[] tl = (int[])get(); |
| 57 | tl[0] = v; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the thread local HRESULT. |
| 62 | * @return this threads HRESULT |
| 63 | */ |
| 64 | public int hresult() { |
| 65 | int[] tl = (int[])get(); |
| 66 | return tl[0]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Called from JNI code to set the last HRESULT |
| 71 | * without having to call instance() from JNI. |
| 72 | * Reduces the JNI crossings. |
| 73 | * |
| 74 | * @param hr |
| 75 | */ |
| 76 | static void setLastHRESULT(int hr) { |
| 77 | instance().hresult(hr); |
| 78 | } |
| 79 | } |