Skip to content

Instantly share code, notes, and snippets.

@orange-in-space
Last active November 18, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orange-in-space/564c7f839da641fa1c084a78ec29dc98 to your computer and use it in GitHub Desktop.
Save orange-in-space/564c7f839da641fa1c084a78ec29dc98 to your computer and use it in GitHub Desktop.
C# コンソールアプリでwin32apiでクリップボードにテキスト送るやつ><(間違ってたらごめんなさい><;)
using System;
using System.Runtime.InteropServices;
namespace ConsoleClipboard
{
class Program
{
static void Main(string[] args)
{
string text = "てすと!><";
SimpleClipboardSetText(text);
}
//クリップボードにテキスト送るやつ
private static void SimpleClipboardSetText(string text)
{
string nullTerminatedStr = text + "\0";
byte[] str = System.Text.Encoding.Unicode.GetBytes(nullTerminatedStr);
IntPtr hglobal = Marshal.AllocHGlobal(str.Length);
Marshal.Copy(str, 0, hglobal, str.Length);
OpenClipboard(IntPtr.Zero); // 0でもいいらしい・・・?><
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hglobal);
CloseClipboard();
Marshal.FreeHGlobal(hglobal);
}
//
private const uint CF_UNICODETEXT = 13;
[DllImport("user32.dll")]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
static extern bool EmptyClipboard();
[DllImport("user32.dll")]
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
static extern bool CloseClipboard();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment