LoginSignup
0
0

cscの作法 その499

Posted at

概要

cscの作法、調べてみた。
bit数判定を、見つけたので、コンパイルしてみた。

参考にしたページ

サンプルコード


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class ExcelUtility {
	[DllImport("kernel32.dll")]
	private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);
	public static bool IsExcel32Bit() {
		bool result;
		dynamic app = null;
		try
		{
			var appType = Type.GetTypeFromProgID("Excel.Application");
			if (appType == null)
			{
				return false;
			}
			if (!Environment.Is64BitOperatingSystem)
			{
				return true;
			}
			app = Activator.CreateInstance(appType);
			var procs = Process.GetProcessesByName("EXCEL");
			if (procs.Length == 0)
			{
				return false;
			}
			bool bWow64;
			if (!IsWow64Process(procs[0].Handle, out bWow64))
			{
				return false;
			}
			result = bWow64;
		}
		catch (COMException)
		{
			return false;
		}
		finally
		{
			if (app != null)
			{
				app.Quit();
				Marshal.ReleaseComObject(app);
				app = null;
				GC.Collect();
				GC.WaitForPendingFinalizers();
				GC.Collect();
			}
		}
		return result;
	}
}
namespace app
{
	class test0 {
		static void Main() {
			if (System.Environment.Is64BitOperatingSystem)
			{
				Console.WriteLine("64ビットOSです。");
			}
			if (IntPtr.Size == 4)
			{
				Console.WriteLine("32ビットで動作しています");
			}
			else if (IntPtr.Size == 8)
			{
				Console.WriteLine("64ビットで動作しています");
			}
			if (ExcelUtility.IsExcel32Bit())
			{
				Console.WriteLine("ok Excel is 64bit");
			}
			else
			{
				Console.WriteLine("ng Excel is 32bit");
			}
		}
	}
}




実行結果

c>ex6
64ビットOSです。
64ビットで動作しています
ok Excel is 64bit

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0