/*
 * Copyright (c) 2000-2009 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
import com.jniwrapper.Function;
import com.jniwrapper.Parameter;
import com.jniwrapper.Pointer;
import com.jniwrapper.UInt32;
import com.jniwrapper.win32.HResult;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.com.*;
import com.jniwrapper.win32.com.types.ClsCtx;
import com.jniwrapper.win32.ole.OleFunctions;
import dcom.dcomserverlib.Disk;
import dcom.dcomserverlib.impl.IDiskImpl;

/**
 * This sample demonstrates the Java DCOM client class, which calls the native server method.   
 * To run this sample:
 * <ol>
 *      <li>build the native server solution</li>
 *      <li>run DComServer.exe on the server</li>
 *      <li>change SERVER_NAME to the network name of the server</li>
 *      <li>run the sample on the client</li>
 * </ol>
 *
 */
public class DComSample
{
    public static final String SERVER_NAME = "screw";

    public static final int COLE_DEFAULT_PRINCIPAL = 0xFFFFFFFF;
    public static final int API_NULL = 0;

    public static void main(String[] args)
    {
        OleFunctions.oleInitialize();

        CoAuthIdentity identity = new CoAuthIdentity();
        String domain = "teamdev.com";
        identity.setDomainName(domain);
        identity.setDomainLength(domain.length());
        String userName = "test_user";
        identity.setUserName(userName);
        identity.setUserLength(userName.length());
        String password = "password";
        identity.setPasswordName(password);
        identity.setPasswordLength(password.length());
        identity.setFlags(CoAuthIdentity.SEC_WINNT_AUTH_IDENTITY_UNICODE);

        CoAuthInfo auth = new CoAuthInfo();
        auth.setAuthnSvc(CoAuthInfo.RPC_C_AUTHN_DEFAULT);
        auth.setAuthzSvc(CoAuthInfo.RPC_C_AUTHZ_DEFAULT);
        auth.setAuthnLevel(CoAuthInfo.RPC_C_AUTHN_LEVEL_DEFAULT);
        auth.setImpersonationLevel(CoAuthInfo.RPC_C_IMP_LEVEL_IMPERSONATE);
        auth.setpAuthIdentityData(identity);
        auth.setCapabilities(CoAuthInfo.EOAC_NONE);

        CoServerInfo info = new CoServerInfo(SERVER_NAME, auth);

        IDiskImpl disk = new IDiskImpl();
        ComFunctions.coCreateInstanceEx(Disk.CLASS_ID, null, ClsCtx.REMOTE_SERVER, info, disk);

        // Set the security on IUnknown
        Function coSetProxyBlanket = OleFunctions.getInstance().getFunction("CoSetProxyBlanket");
        HResult result = new HResult();
        coSetProxyBlanket.invoke(result, new Parameter[] {
                disk,
                new UInt32(CoAuthInfo.RPC_C_AUTHN_DEFAULT),
                new UInt32(CoAuthInfo.RPC_C_AUTHZ_DEFAULT),
                new Pointer.Void(COLE_DEFAULT_PRINCIPAL),
                new UInt32(CoAuthInfo.RPC_C_AUTHN_LEVEL_DEFAULT),
                new UInt32(CoAuthInfo.RPC_C_IMP_LEVEL_DEFAULT),
                new Pointer.Void(API_NULL),
                new UInt32(CoAuthInfo.EOAC_DEFAULT),
        });
        ComException.checkResult(result);

        BStr drive = new BStr("C:\\");
        long space = disk.getDFreeSpace(drive).getValue();
        System.out.println("Free space on drive " + drive.getValue() + " = " + space + " bytes.");
    }

}