ポインタを使った値型の参照渡し – takym.code.blog
こんにちは、Takymです。
http://takym-program.hateblo.jp/entry/20150415/1429096357からの転載記事です。(※一部修正しております。)
ポインタを使った暗黙的な型変換
ポインタを使ってメモリにアクセスできるので、暗黙的に型を別の型に変換できるのではないかと思いましたのでC#で試してみました。
(※ポインタを使うには ‘/unsafe’ でコンパイルする必要があります。)
using System;
namespace MemoryTest
{
// ポインタにするためアンセーフにする。
static unsafe class Program
{
static void Main(string[] args)
{
// ポインタを使う変数。
Target tc = new Target();
// tc のポインタ。
Target* tc_pointer = &tc;
// 値を代入する。
tc_pointer->number1 = 0x12345678;
tc_pointer->number2 = 0x00ABCDEF;
// tc と同じアドレスで、LongTarget型のポインタを作る。
LongTarget* lt_pointer = ((LongTarget*)(tc_pointer));
// 結果の表示
Console.WriteLine("アドレス:0x{0:X}", new IntPtr(tc_pointer));
Console.WriteLine("number1=0x{0:X}", tc.number1);
Console.WriteLine("number2=0x{0:X}", tc.number2);
Console.WriteLine("number3=0x{0:X}", lt_pointer->number3);
}
// ターゲットの型。
// 値型でなければいけない。(フィールドも。)
struct Target
{
public uint number1;
public uint number2;
}
// ターゲットの型。
struct LongTarget
{
public ulong number3;
}
}
}
ソースコードのダウンロード
ポインタを使った暗黙的な型変換
ポインタを使った暗黙的な型変換 – takym.code.blog
こんにちは、Takymです。
http://takym-program.hateblo.jp/entry/20150409/1428537898からの転載記事です。(※一部修正しております。)
C#のMicrosoft.Win32.RegistryKeyクラスを使用してプロトコルの登録
C#でプロトコルの登録する方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sample
{
public class Protocol
{
public Protocol()
{
// GUIDの取得。
Assembly asm = Assembly.GetExecutingAssembly();
GuidAttribute ga = ((GuidAttribute)(Attribute.GetCustomAttribute(asm, typeof(GuidAttribute))));
string guid = ga.Value;
// 登録するプロトコル。
string ProtcolType = "test";
// 実行するコマンドライン。
string commandline = "\"" + Application.ExecutablePath + "\" \"%1\"";
// 説明。
string description1 = "test: Test Protocol Handler";
string description2 = "URL:test Protocol";
// 動詞。
string verb = "open";
// アイコンの指定。
string iconPath = Application.ExecutablePath;
int iconIndex = 0;
// ※以下のコードは書き変えないで使ってください。
RegistryKey rootkey = Registry.ClassesRoot;
RegistryKey regkey = rootkey.CreateSubKey("PROTOCOLS\\Handler\\" + ProtcolType);
regkey.SetValue("", description1);
regkey.SetValue("CLSID", guid);
regkey.Close();
RegistryKey typekey = rootkey.CreateSubKey(ProtcolType);
typekey.SetValue("", description2);
typekey.SetValue("Source Filter", guid);
typekey.SetValue("URL Protocol", "");
typekey.Close();
RegistryKey verblkey = rootkey.CreateSubKey(ProtcolType + "\\shell\\");
verblkey.SetValue("", verb);
verblkey.Close();
RegistryKey open = rootkey.CreateSubKey(ProtcolType + "\\shell\\" + verb);
open.SetValue("CommandId", "");
open.SetValue("DontReturnProcessHandle", "");
open.Close();
RegistryKey cmdkey = rootkey.CreateSubKey(ProtcolType + "\\shell\\" + verb + "\\command");
cmdkey.SetValue("", commandline);
cmdkey.SetValue("DelegateExecute", guid);
cmdkey.Close();
RegistryKey iconkey = rootkey.CreateSubKey(ProtcolType + "\\DefaultIcon");
iconkey.SetValue("", iconPath + "," + iconIndex.ToString());
iconkey.Close();
}
}
}
ソースコードのダウンロード
C#のMicrosoft.Win32.RegistryKeyクラスを使用して拡張子登録
リメイク版の記事ができましたのでこちらの記事を見てください。
C#で拡張子を登録する方法を見つけました。
C#のMicrosoft.Win32.RegistryKeyクラスを使用します。
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sample
{
public class Extension
{
public Extension()
{
// 登録する拡張子。
// (※お好みで指定してください。)
string extension = ".test";
// 実行するコマンドライン。
string commandline = "\"" + Application.ExecutablePath + "\" \"%1\"";
string fileType = Application.ProductName;
// 説明。
// (※お好みで指定してください。)
string description = "test ファイル";
// 動詞。
// (※お好みで指定してください。)
string verb = "open";
string verbDescription = "開く(&O)";
// アイコンの指定。
string iconPath = Application.ExecutablePath;
int iconIndex = 0;
// ※以下のコードは書き変えないで使ってください。
RegistryKey rootkey = Registry.ClassesRoot;
RegistryKey regkey = rootkey.CreateSubKey(extension);
regkey.SetValue("", fileType);
regkey.Close();
RegistryKey typekey = rootkey.CreateSubKey(fileType);
typekey.SetValue("", description);
typekey.Close();
RegistryKey verblkey = rootkey.CreateSubKey(fileType + "\\shell\\" + verb);
verblkey.SetValue("", verbDescription);
verblkey.Close();
RegistryKey cmdkey = rootkey.CreateSubKey(fileType + "\\shell\\" + verb + "\\command");
cmdkey.SetValue("", commandline);
cmdkey.Close();
RegistryKey iconkey = rootkey.CreateSubKey(fileType + "\\DefaultIcon");
iconkey.SetValue("", iconPath + "," + iconIndex.ToString());
iconkey.Close();
}
}
}
ソースコードのダウンロード
C#のMicrosoft.Win32.RegistryKeyクラスを使用してアプリケーションのインストール
C#でアプリケーションをインストールする方法を見つけました。
Microsoft.Win32.RegistryKeyクラスを使用します。
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sample
{
public class Install
{
public Install()
{
// GUID の取得。
Assembly asm = Assembly.GetExecutingAssembly();
GuidAttribute ga = ((GuidAttribute)(Attribute.GetCustomAttribute(asm, typeof(GuidAttribute))));
string guid = ga.Value;
// アイコンの設定。
string iconPath = Application.ExecutablePath;
int iconIndex = 0;
// アプリケーションの名前を登録。
string displayName = Application.ProductName;
// アプリケーションのバージョンを登録。
string displayVersion = Application.ProductVersion;
// アプリケーションの場所を登録。
string location = Application.StartupPath;
string source = Application.StartupPath;
// アプリケーションの言語を指定。
// (※現在は日本語で指定している。)
int lang = 00000409;
string publisher = Application.CompanyName;
// アンインストーラのパス。
// (※お好みで指定してください。)
string uninstall = "";
// ※以下のコードは書き変えないで使ってください。
RegistryKey rootkey =
Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
RegistryKey regkey = rootkey.CreateSubKey(guid);
regkey.SetValue("DisplayIcon", iconPath + "," + iconIndex.ToString());
regkey.SetValue("DisplayName", displayName);
regkey.SetValue("DisplayVersion", displayVersion);
regkey.SetValue("InstallLocation", location);
regkey.SetValue("InstallSource", source);
regkey.SetValue("Language", lang, RegistryValueKind.DWord);
regkey.SetValue("Publisher", publisher);
regkey.SetValue("UninstallPath", "\"" + uninstall + "\"");
regkey.SetValue("UninstallString", "\"" + uninstall + "\"");
regkey.Close();
}
}
}
ソースコードのダウンロード
C#のMicrosoft.Win32.RegistryKeyクラスを使用してプロトコルの登録
C#のMicrosoft.Win32.RegistryKeyクラスを使用して拡張子登録
C#のMicrosoft.Win32.RegistryKeyクラスを使用してアプリケーションのインストール
C#でプロセスの操作
前回はスクリーンショットを作成するソフトを作りました。
今回はC#でプロセスを操作してみました。
仕様は以下のとうりです。
- 起動してすぐにメモ帳を起動します。
- メモ帳が閉じるまで待機します。
ソースコードは以下のとうりです。
using System;
using System.Diagnostics;
using System.Threading;
// 名前空間とクラスの名前が同じなので名前を変える。
using ProcessControl = System.Diagnostics.Process;
namespace Process
{
class Program
{
static void Main(string[] args)
{
// 起動するファイルの設定。
ProcessStartInfo psi = new ProcessStartInfo();
// 起動すファイル。
psi.FileName = "C:\\Windows\\notepad.exe";
// シェルを使用しない。
psi.UseShellExecute = false;
// プロセスを開始する。
ProcessControl pc = ProcessControl.Start(psi);
// ループの初期化。
char[] bars = { '/', '―', '\', '|' }; int i = 0;
Console.CursorVisible = false; Console.ForegroundColor = ConsoleColor.Magenta;
while (true)
{
Console.Write(bars[i]);
Console.Write(" 待機中です。");
Console.SetCursorPosition(0, Console.CursorTop);
// ファイルが終了するまで待機。
if (pc.HasExited) break;
i++; if (i >= 4) i = 0;
Thread.Sleep(100);
}
pc.WaitForExit();
Console.CursorVisible = true; Console.ResetColor();
// コンソールウィンドウが閉じないようにする。
Console.WriteLine("続行するには何かキーを押してください......");
Console.ReadKey(true);
}
}
}
実行画面です。
感想
このアプリケーションは応用しないと使い道がないと思いました。