Delete handle of a PLC variable

In this sample a handle of a PLC variable is fetched and deleted afterwards:

Unpack the sample program 'Delete handle of a PLC variable': sample08.zip

import de.beckhoff.jni.Convert;
import de.beckhoff.jni.JNIByteBuffer;
import de.beckhoff.jni.tcads.AdsCallDllFunction;
import de.beckhoff.jni.tcads.AmsAddr;

public class Main {
    public static void main(String[] args) {
    long err;
    AmsAddr addr = new AmsAddr();
    JNIByteBuffer symBuff;
    JNIByteBuffer handleBuff = new JNIByteBuffer(Integer.SIZE / Byte.SIZE);

    try {
        // Open communication        
        AdsCallDllFunction.adsPortOpen();
        err = AdsCallDllFunction.getLocalAddress(addr);
        addr.setPort(AdsCallDllFunction.AMSPORT_R0_PLC_RTS1);

        if (err != 0) {
        System.out.println("Error: Open communication: 0x"
            + Long.toHexString(err));
        } else {
        System.out.println("Success: Open communication!");
        } 

        // Convert Symbol name to byte buffer
        symBuff = new JNIByteBuffer(Convert.StringToByteArr(
            "MAIN.PLCVar",
            true));

        // Get handle via symbol name
        err = AdsCallDllFunction.adsSyncReadWriteReq(addr,
            AdsCallDllFunction.ADSIGRP_SYM_HNDBYNAME,
            0,
            handleBuff.getUsedBytesCount(),
            handleBuff,         //buffer for getting handle
            symBuff.getUsedBytesCount(),
            symBuff);           //buffer containg symbol name
        
        if(err!=0) { 
        System.out.println("Error: Get Handle: 0x" + Long.toHexString(err)); 
        } else {
        System.out.println("Success: Get Handle!");
        }
        
        // Release handle
        err = AdsCallDllFunction.adsSyncWriteReq(addr,
            AdsCallDllFunction.ADSIGRP_SYM_RELEASEHND,
            0,
            handleBuff.getUsedBytesCount(),
            handleBuff);
        
        if(err!=0) {
        System.out.println("Error: Release Handle: 0x"
            + Long.toHexString(err));
        } else {
        System.out.println("Success: Handle removed!");
        }

        // Close communication
        err = AdsCallDllFunction.adsPortClose();
        if(err!=0) { 
        System.out.println("Error: Close Communication: 0x" 
            + Long.toHexString(err)); 
        }

        System.out.println("Press enter to continue..");
        System.in.read();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    }
}