GithubHelp home page GithubHelp logo

reference's People

Contributors

aimglow avatar

Stargazers

 avatar

Watchers

 avatar

reference's Issues

WinMergeの差分レポートを、htmlで出力

ファイル比較

"C:\Program Files\WinMerge\WinMergeU.exe" C:\dev\winmerge\test1.txt C:\dev\winmerge\test2.txt  /minimize /noninteractive /u /or C:\dev\winmerge\out.html

フォルダ比較

"C:\Program Files\WinMerge\WinMergeU.exe" C:\dev\winmerge\abc C:\dev\winmerge\acc -minimize -noninteractive -noprefs -cfg Settings/DirViewExpandSubdirs=1 -cfg ReportFiles/ReportType=2 -cfg ReportFiles/IncludeFileCmpReport=1 -r -u -or C:\dev\winmerge\out2.html

参考
winmergeのコマンドラインオプション

開いているexcelのリスト

using System;
using System.Collections.Generic;
using System.Diagnostics;

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Excel = Microsoft.Office.Interop.Excel;

///


/// Running Object Table関連操作のラッパークラス
///

public class ROTManager
{
/*
* 参考サイト
* モニカの概念について
* モニカ
* 実際のC#の実装例
* [複数の EXCEL.EXE が動いている時に、ひとつの Excel.Application を GetObject で特定して C# で操作するには? - QA@IT]
* (https://qa.atmarkit.co.jp/q/4634)
*/

public static Excel.Workbook[] GetOpendLocalWorkbooks()
{
    return GetOpendComObjects<Excel.Workbook>();
}

/// <summary>
/// ファイルモニカを元に取得できるオブジェクトから、指定した型のオブジェクトを探す
/// </summary>
/// <typeparam name="T">取得したいCOMの型</typeparam>
/// <returns>型変換に成功したオブジェクト</returns>
public static T[] GetOpendComObjects<T>() where T : class
{
    List<T> ts = new List<T>();
    // OfType<T>は解放のタイミングが難しそう
    foreach (var o in GetRunningObjects(MKSYS.MKSYS_FILEMONIKER))
    {
        T t = o as T;
        if (t != null)
        {
            ts.Add(t);
        }
        else
        {
            Marshal.FinalReleaseComObject(o);
        }
    }
    return ts.ToArray();
}

/// <summary>
/// モニカーの種類
/// </summary>
/// <see cref="https://docs.microsoft.com/ja-jp/windows/desktop/api/objidl/ne-objidl-tagmksys"/>
enum MKSYS
{
    MKSYS_NONE,
    MKSYS_GENERICCOMPOSITE,
    MKSYS_FILEMONIKER,
    MKSYS_ANTIMONIKER,
    MKSYS_ITEMMONIKER,
    MKSYS_POINTERMONIKER,
    MKSYS_CLASSMONIKER,
    MKSYS_OBJREFMONIKER,
    MKSYS_SESSIONMONIKER,
    MKSYS_LUAMONIKER
}

/// <summary>
/// Returns a pointer to an implementation of IBindCtx (a bind context object). This object stores information about a particular moniker-binding operation.
/// </summary>
/// <param name="reserved">This parameter is reserved and must be 0.</param>
/// <param name="ppbc">Address of an IBindCtx* pointer variable that receives the interface pointer to the new bind context object. When the function is successful, the caller is responsible for calling Release on the bind context. A NULL value for the bind context indicates that an error occurred.</param>
/// <returns>This function can return the standard return values E_OUTOFMEMORY and S_OK</returns>
/// <see cref="https://docs.microsoft.com/en-us/windows/desktop/api/objbase/nf-objbase-createbindctx"/>
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);

/// <summary>
/// Running Object Table から指定されたモニカのオブジェクトを取得する
/// </summary>
/// <param name="monikerType">モニカの種類</param>
/// <returns>見つかったオブジェクト</returns>
private static object[] GetRunningObjects(MKSYS monikerType)
{
    // Running Object Table を取得する
    const uint reserved = 0;
    IBindCtx ctx;
    CreateBindCtx(reserved, out ctx);

    IRunningObjectTable runningObjectTable;
    ctx.GetRunningObjectTable(out runningObjectTable);

    IEnumMoniker enumMoniker;
    runningObjectTable.EnumRunning(out enumMoniker);
    // ここまではほぼ定型

    enumMoniker.Reset();
    List<object> returnObjects = new List<object>();

    while (true) 
    {
        const int S_OK = 0;

        IMoniker[] tmpMks = new IMoniker[1];
        IntPtr fetched = IntPtr.Zero;
        // bufMonikers の数ずつモニカーを取得
        bool successNext = enumMoniker.Next(tmpMks.Length, tmpMks, fetched) == S_OK;
        if (!successNext)
        {
            break;
        }

        try
        {
            //for Debug
            string dispName;
            tmpMks[0].GetDisplayName(ctx, null,out dispName);
            Debug.WriteLine("DisplayName\t" + dispName);

            Guid clsId;
            tmpMks[0].GetClassID(out clsId);
            Debug.WriteLine(clsId);
            // Debug.WriteLine(Marshal.GetTypeFromCLSID(clsId)); //-> System.__ComObject

            int pdwMksys;
            if (tmpMks[0].IsSystemMoniker(out pdwMksys) != S_OK)
            {
                Debug.WriteLine("not SystemMoniker");
                continue;
            }

            MKSYS mkType = (MKSYS)Enum.ToObject(typeof(MKSYS), pdwMksys);
            Debug.WriteLine(mkType);
            if (mkType != monikerType)
            {
                continue;
            }

            object obj;
            if (runningObjectTable.GetObject(tmpMks[0], out obj) != S_OK)
            {
                continue;
            }
            returnObjects.Add(obj);
        }
        finally
        {
            Marshal.FinalReleaseComObject(tmpMks[0]);
        }
    }

    Marshal.FinalReleaseComObject(enumMoniker);
    Marshal.FinalReleaseComObject(runningObjectTable);
    Marshal.FinalReleaseComObject(ctx);

    return returnObjects.ToArray();
}

}

Inputbox powershell

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
}

複数excelの取得

using System;
using System.Collections.Generic;
using System.Diagnostics;

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Excel = Microsoft.Office.Interop.Excel;

///


/// Running Object Table関連操作のラッパークラス
///

public class ROTManager
{
/*
* 参考サイト
* モニカの概念について
* モニカ
* 実際のC#の実装例
* [複数の EXCEL.EXE が動いている時に、ひとつの Excel.Application を GetObject で特定して C# で操作するには? - QA@IT]
* (https://qa.atmarkit.co.jp/q/4634)
*/

public static Excel.Workbook[] GetOpendLocalWorkbooks()
{
    return GetOpendComObjects<Excel.Workbook>();
}

/// <summary>
/// ファイルモニカを元に取得できるオブジェクトから、指定した型のオブジェクトを探す
/// </summary>
/// <typeparam name="T">取得したいCOMの型</typeparam>
/// <returns>型変換に成功したオブジェクト</returns>
public static T[] GetOpendComObjects<T>() where T : class
{
    List<T> ts = new List<T>();
    // OfType<T>は解放のタイミングが難しそう
    foreach (var o in GetRunningObjects(MKSYS.MKSYS_FILEMONIKER))
    {
        T t = o as T;
        if (t != null)
        {
            ts.Add(t);
        }
        else
        {
            Marshal.FinalReleaseComObject(o);
        }
    }
    return ts.ToArray();
}

/// <summary>
/// モニカーの種類
/// </summary>
/// <see cref="https://docs.microsoft.com/ja-jp/windows/desktop/api/objidl/ne-objidl-tagmksys"/>
enum MKSYS
{
    MKSYS_NONE,
    MKSYS_GENERICCOMPOSITE,
    MKSYS_FILEMONIKER,
    MKSYS_ANTIMONIKER,
    MKSYS_ITEMMONIKER,
    MKSYS_POINTERMONIKER,
    MKSYS_CLASSMONIKER,
    MKSYS_OBJREFMONIKER,
    MKSYS_SESSIONMONIKER,
    MKSYS_LUAMONIKER
}

/// <summary>
/// Returns a pointer to an implementation of IBindCtx (a bind context object). This object stores information about a particular moniker-binding operation.
/// </summary>
/// <param name="reserved">This parameter is reserved and must be 0.</param>
/// <param name="ppbc">Address of an IBindCtx* pointer variable that receives the interface pointer to the new bind context object. When the function is successful, the caller is responsible for calling Release on the bind context. A NULL value for the bind context indicates that an error occurred.</param>
/// <returns>This function can return the standard return values E_OUTOFMEMORY and S_OK</returns>
/// <see cref="https://docs.microsoft.com/en-us/windows/desktop/api/objbase/nf-objbase-createbindctx"/>
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);

/// <summary>
/// Running Object Table から指定されたモニカのオブジェクトを取得する
/// </summary>
/// <param name="monikerType">モニカの種類</param>
/// <returns>見つかったオブジェクト</returns>
private static object[] GetRunningObjects(MKSYS monikerType)
{
    // Running Object Table を取得する
    const uint reserved = 0;
    IBindCtx ctx;
    CreateBindCtx(reserved, out ctx);

    IRunningObjectTable runningObjectTable;
    ctx.GetRunningObjectTable(out runningObjectTable);

    IEnumMoniker enumMoniker;
    runningObjectTable.EnumRunning(out enumMoniker);
    // ここまではほぼ定型

    enumMoniker.Reset();
    List<object> returnObjects = new List<object>();

    while (true) 
    {
        const int S_OK = 0;

        IMoniker[] tmpMks = new IMoniker[1];
        IntPtr fetched = IntPtr.Zero;
        // bufMonikers の数ずつモニカーを取得
        bool successNext = enumMoniker.Next(tmpMks.Length, tmpMks, fetched) == S_OK;
        if (!successNext)
        {
            break;
        }

        try
        {
            //for Debug
            string dispName;
            tmpMks[0].GetDisplayName(ctx, null,out dispName);
            Debug.WriteLine("DisplayName\t" + dispName);

            Guid clsId;
            tmpMks[0].GetClassID(out clsId);
            Debug.WriteLine(clsId);
            // Debug.WriteLine(Marshal.GetTypeFromCLSID(clsId)); //-> System.__ComObject

            int pdwMksys;
            if (tmpMks[0].IsSystemMoniker(out pdwMksys) != S_OK)
            {
                Debug.WriteLine("not SystemMoniker");
                continue;
            }

            MKSYS mkType = (MKSYS)Enum.ToObject(typeof(MKSYS), pdwMksys);
            Debug.WriteLine(mkType);
            if (mkType != monikerType)
            {
                continue;
            }

            object obj;
            if (runningObjectTable.GetObject(tmpMks[0], out obj) != S_OK)
            {
                continue;
            }
            returnObjects.Add(obj);
        }
        finally
        {
            Marshal.FinalReleaseComObject(tmpMks[0]);
        }
    }

    Marshal.FinalReleaseComObject(enumMoniker);
    Marshal.FinalReleaseComObject(runningObjectTable);
    Marshal.FinalReleaseComObject(ctx);

    return returnObjects.ToArray();
}

}

How to get PID of created COM Object

I'm trying to get PID of ComObject Excel to kill it specifically without other ones. So far I got that but how can I isolate it? Maybe other ideas?

(FYI window of Excel via script is not visible)

Get-WmiObject Win32_Process -Filter "name = 'EXCEL.exe'" | Select-Object CommandLine, ProcessId| format-list

CommandLine : "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE"
ProcessId : 11488

CommandLine : "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" /automation -Embedding
ProcessId : 37920
5
9 comments
Copy this post's permalink to the clipboard

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.