view snippets/ole/Snippet81.d @ 151:47a861d0d0f2

changed filetype to unix
author Frank Benoit <benoit@tionex.de>
date Wed, 20 Aug 2008 19:56:12 +0200
parents 297120c376f7
children 8907b6374258
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Enzo Petrelli
 *******************************************************************************/
module ole.Snippet81;

/*
 * OLE and ActiveX example snippet: browse the typelibinfo for a program id (win32 only)
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */

bug
{
    pragma(lib, "DD-dwt-dbg.lib");
}
else
{
    pragma(lib, "DD-dwt.lib");
}
pragma(lib, "advapi32.lib");
pragma(lib, "comctl32.lib");
pragma(lib, "comdlg32.lib");
pragma(lib, "gdi32.lib");
pragma(lib, "gdiplus.lib");
pragma(lib, "glu32.lib");
pragma(lib, "imm32.lib");
pragma(lib, "kernel32.lib");
pragma(lib, "msimg32.lib");
pragma(lib, "ole32.lib");
pragma(lib, "oleacc.lib");
pragma(lib, "oleaut32.lib");
pragma(lib, "olepro32.lib");
pragma(lib, "opengl32.lib");
pragma(lib, "shlwapi.lib");
pragma(lib, "user32.lib");
pragma(lib, "usp10.lib");
pragma(lib, "uxtheme.lib");

import dwt.DWT;
import dwt.widgets.Display;
import dwt.widgets.Shell;

import dwt.dwthelper.utils;             // String
import dwt.internal.ole.win32.OAIDL;
import dwt.ole.win32.OLE;
import dwt.ole.win32.OleAutomation;
import dwt.ole.win32.OleControlSite;
import dwt.ole.win32.OleFrame;
import dwt.ole.win32.OleFunctionDescription;
import dwt.ole.win32.OlePropertyDescription;

import std.cstream;
import std.path;
import std.stream;

alias char[] string;

int main(string[] asArg)
{
    int iRes = 0;

    try
    {
    //  String progID = "Shell.Explorer";
        String progID = "Excel.Application";
        
        Display oDisplay = new Display();
        Shell oShell = new Shell(oDisplay);
    
        OleFrame frame = new OleFrame(oShell, DWT.NONE);
        OleControlSite oOleSite = null;
        OleAutomation oOleAutoObj = null;
        try
        {
            oOleSite = new OleControlSite(frame, DWT.NONE, progID); 
        } catch (Exception oExc)
        {
            dout.writefln("Exception %s creating OleControlSite on type library for %s", oExc.toString(), progID);
            return 1;
        }
        try
        {
            oOleAutoObj = new OleAutomation(oOleSite);
        } catch (Exception oExc)
        {
            dout.writefln("Exception %s  OleAutomation on type library for %s", oExc.toString(), progID);
            return 1;
        }
        
        dout.writefln("TypeLibrary for: '%s'", progID);
        printTypeInfo(oOleAutoObj, dout);
        
        oShell.dispose();
        oDisplay.dispose();
    } catch (Exception oExc)
    {
        dout.writefln("Exception %s in main()", oExc.toString());
        iRes = 1;
    }
    
    return iRes;
}

private static void printTypeInfo(OleAutomation oOleAutoObj, OutputStream oOut)
{
    dwt.internal.ole.win32.OAIDL.TYPEATTR * pTypeAttr = oOleAutoObj.getTypeInfoAttributes();
    if (pTypeAttr !is null)
    {       
        if (pTypeAttr.cFuncs > 0)
        {
            oOut.writefln("Functions :");
            for (int iIdx = 0; iIdx < pTypeAttr.cFuncs; ++iIdx)
            {           
                OleFunctionDescription oData = oOleAutoObj.getFunctionDescription(iIdx);
                string sArgList = "";
                int iFirstOptionalArgIndex = oData.args.length - oData.optionalArgCount;
                for (int iArg = 0; iArg < oData.args.length; ++iArg)
                {
                    sArgList ~= "[";
                    if (iArg >= iFirstOptionalArgIndex)
                        sArgList ~= "optional, ";
                    sArgList ~= getDirection(oData.args[iArg].flags) ~ "] " ~
                        getTypeName(oData.args[iArg].type) ~ " " ~
                        oData.args[iArg].name
                        ;
                    if (iArg < oData.args.length - 1)
                        sArgList ~= ", ";
                }
                oOut.writefln("%s (id = %d (0x%08X))", getInvokeKind(oData.invokeKind), oData.id, oData.id);
                oOut.writefln("\tSignature  : %s %s(%s)", getTypeName(oData.returnType), oData.name, sArgList);
                oOut.writefln("\tDescription: %s", oData.documentation);
                oOut.writefln("\tHelp File  : %s", oData.helpFile);
                oOut.writefln();
            }
        }
        
        if (pTypeAttr.cVars > 0)
        {
            oOut.writefln("\n\nVariables :");
            for (int iIdx = 0; iIdx < pTypeAttr.cVars; ++iIdx)
            {
                OlePropertyDescription oData = oOleAutoObj.getPropertyDescription(iIdx);
                oOut.writefln("PROPERTY (id = %d (0x%08X)", oData.id, oData.id);
                oOut.writefln("\tName : %s", oData.name);
                oOut.writefln("\tType : %s", getTypeName(oData.type));
                oOut.writefln();
            }
        }
    }
}
private static string getTypeName(int iType)
{
    int iBase = iType & ~OLE.VT_BYREF;
    String sDsc = null;
    switch (iBase)
    {
        case OLE.VT_BOOL :          sDsc = "boolean"; break;
        case OLE.VT_R4 :            sDsc = "float"; break;
        case OLE.VT_R8 :            sDsc = "double"; break;
        case OLE.VT_I4 :            sDsc = "int"; break;
        case OLE.VT_DISPATCH :      sDsc = "IDispatch"; break;
        case OLE.VT_UNKNOWN :       sDsc = "IUnknown"; break;
        case OLE.VT_I2 :            sDsc = "short"; break;
        case OLE.VT_BSTR :          sDsc = "String"; break;
        case OLE.VT_VARIANT :       sDsc = "Variant"; break;
        case OLE.VT_CY :            sDsc = "Currency"; break;
        case OLE.VT_DATE :          sDsc = "Date"; break;
        case OLE.VT_UI1 :           sDsc = "unsigned char"; break;
        case OLE.VT_UI4 :           sDsc = "unsigned int"; break;
        case OLE.VT_USERDEFINED :   sDsc = "UserDefined"; break;
        case OLE.VT_HRESULT :       sDsc = "HRESULT"; break;
        case OLE.VT_VOID :          sDsc = "void"; break;
        
        case OLE.VT_UI2 :           sDsc = "unsigned short"; break;
        case OLE.VT_UINT :          sDsc = "unsigned int"; break;
        case OLE.VT_PTR :           sDsc = "void *"; break;
        default: break;
    }
    if (sDsc !is null)
    {
        if ((iType & OLE.VT_BYREF) == OLE.VT_BYREF)
            return sDsc ~ " *";
        return sDsc;
    }
    return std.string.format("unknown %d (0x%04X)", iType, iType);
}
private static string getDirection(int bDirection)
{
    string sDirString = "";
    bool bComma = false;
    if ((bDirection & OLE.IDLFLAG_FIN) != 0)
    {
        sDirString ~= "in";
        bComma = true;
    }
    if ((bDirection & OLE.IDLFLAG_FOUT) != 0)
    {
        if (bComma) sDirString ~= ", ";
        sDirString ~= "out";
        bComma = true;
    }
    if ((bDirection & OLE.IDLFLAG_FLCID) != 0)
    {
        if (bComma) sDirString ~= ", ";
        sDirString ~= "lcid";
        bComma = true;
    }
    if ((bDirection & OLE.IDLFLAG_FRETVAL) != 0)
    {
        if (bComma) sDirString ~= ", "; 
        sDirString ~= "retval";
    }
    return sDirString;
}
private static string getInvokeKind(int iInvKind) {
    switch (iInvKind)
    {
        case OLE.INVOKE_FUNC : return "METHOD";
        case OLE.INVOKE_PROPERTYGET : return "PROPERTY GET";
        case OLE.INVOKE_PROPERTYPUT : return "PROPERTY PUT";
        case OLE.INVOKE_PROPERTYPUTREF : return "PROPERTY PUT BY REF";
        default: break;
    }
    return std.string.format("unknown %d", iInvKind);
}