0

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'

エントリーポイントは?

4

1 に答える 1

2

ExactSpelling プロパティを不適切に使用しています。例外メッセージが示すように、user32.dll には FindWindow 関数はありません。FindWindowA と FindWindowW があります。1 つ目は従来の 8 ビット文字列を処理し、2 つ目は Unicode 文字列を使用します。文字列を受け入れる Windows api 関数には 2 つのバージョンがあります。UNICODE マクロが 2 つのバージョンを選択するため、これは C または C++ コードでは見られません。

winapi 関数では ExactSpelling を避けてください。pinvoke マーシャラーはこれを処理する方法を知っています。他にもいくつかの間違いがあります。適切な宣言は次のとおりです。

[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr FindWindow(String^ classname, String^ windowname);
于 2012-05-10T03:55:01.357 に答える