2 つの WinApi 関数をインポートして、クラスで使用します
using namespace System::Runtime::InteropServices;
[DllImport("user32",ExactSpelling = true)]
extern bool CloseWindow(int hwnd);
[DllImport("user32",ExactSpelling = true)]
extern int FindWindow(String^ classname,String^ windowname);
public ref class WinApiInvoke
{
public:
bool CloseWindowCall(String^ title)
{
int pointer = FindWindow(nullptr,title);
return CloseWindow(pointer);
}
};
次に、メインプログラムでオブジェクトを作成し、CloseWindowCall
メソッドを呼び出します
Console::WriteLine("Window's title");
String ^s = Console::ReadLine();
WinApiInvoke^ obj = gcnew WinApiInvoke();
if (obj->CloseWindowCall(s))
Console::WriteLine("Window successfully closed!");
else Console::WriteLine("Some error occured!");
たとえば Chess Titans を閉じるようにコンソールに書き込むと、エラーが発生しました
Unable to find an entry point named 'FindWindow' in DLL 'user32'
エントリーポイントは?