java-native-access/jna · error · Ras32Exception

Ras32Exception(err)

Error message

Ras32Exception(err)

What it means

Rasapi32Util.getRasConnection looks up an active RAS connection by name using RasEnumConnections. Native RAS error codes other than success/ERROR_BUFFER_TOO_SMALL (on the sizing call) or ERROR_SUCCESS (on the data call) are wrapped in Ras32Exception carrying the RAS error code.

Solutions

  1. Check the error code inside Ras32Exception against WinRas constants (e.g. ERROR RasMan issues)
  2. Ensure the Remote Access Connection Manager (RasMan) Windows service is running
  3. Verify RAS/VPN support exists on the target machine
  4. Handle the null return (connection name not found) separately from the exception path

Example fix

// before
HANDLE h = Rasapi32Util.getRasConnection(connName);
// after
HANDLE h;
try {
    h = Rasapi32Util.getRasConnection(connName);
} catch (Ras32Exception e) {
    LOG.warn("RAS lookup failed with code " + e.getErrorCode());
    h = null;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (connName == null || connName.isEmpty()) throw new IllegalArgumentException("connection name required");

Try / catch

try { return Rasapi32Util.getRasConnection(connName); } catch (Ras32Exception e) { LOG.warn("RAS error " + e.getErrorCode()); return null; }

Prevention

When it happens

Trigger: RasEnumConnections failing — e.g. RAS not installed/available on the machine, insufficient permissions to read connection data, or the data-call phase returning a RAS error code after the sizing call succeeded.

Common situations: Calling RAS APIs on machines without dial-up/VPN (RasMan) service enabled; RasMan service stopped; searching for a connection name that simply does not exist (the method then returns null rather than throwing); restricted environments blocking RAS API access.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/1e37f4f0769c0cf5. Report an issue: GitHub.

Appendix: source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Rasapi32Util.java:149

     * @return the descriptive text
     */
    public static String getRasConnectionStatusText(int connStatus) {
        if (!CONNECTION_STATE_TEXT.containsKey(connStatus)) return Integer.toString(connStatus);
        return (String)CONNECTION_STATE_TEXT.get(connStatus);
    }

    /**
     * Return a RAS connection by name
     * @param connName the connection name
     * @return the RAS connection structure
     * @throws Ras32Exception errors
     */
    public static HANDLE getRasConnection(String connName) throws Ras32Exception {
        // size the array needed
        IntByReference lpcb  = new IntByReference(0);
        IntByReference lpcConnections  = new IntByReference();
        int err = Rasapi32.INSTANCE.RasEnumConnections(null, lpcb, lpcConnections);
        if (err != WinError.ERROR_SUCCESS && err != WinRas.ERROR_BUFFER_TOO_SMALL) throw new Ras32Exception(err);
        if (lpcb.getValue() == 0) return null;

        // get the connections
        RASCONN[] connections = new RASCONN[lpcConnections.getValue()];
        for (int i = 0; i < lpcConnections.getValue(); i++) connections[i] = new RASCONN();
        lpcb  = new IntByReference(connections[0].dwSize * lpcConnections.getValue());
        err = Rasapi32.INSTANCE.RasEnumConnections(connections, lpcb, lpcConnections);
        if (err != WinError.ERROR_SUCCESS) throw new Ras32Exception(err);

        // find connection
        for (int i = 0; i < lpcConnections.getValue(); i++) {
            if (new String(connections[i].szEntryName).equals(connName)) return connections[i].hrasconn;
        }
        return null;
    }

    /**
     * Hangup a connection by name

View on GitHub (pinned to d036ad9781)