我如何使用C ++代码并将其添加到C#gui按钮

时间:2017-01-17 10:07:37

标签: c# c++

这是我的第一篇文章..我现在还不知道如何处理这种溢出,所以我只会看看我能做些什么。因为我试图在这里理解它...(我对此很新)我无法掌握它...所以我的代码很简单,使用sendinput,现在我必须将它与C#GUI结合起来。我只是不知道怎么......有人可以帮助我,我的代码就是......

int main()
{
    ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", NULL, NULL, SW_SHOWNORMAL);

    Sleep(2000);
    INPUT ip = {};

    ip.type = INPUT_MOUSE;
    ip.mi.dx = 3500;
    ip.mi.dy = 2300;
    ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTDOWN;
    ip.mi.mouseData = 0;
    SendInput(1, &ip, sizeof(INPUT));

    Sleep(200);

    ip.type = INPUT_KEYBOARD;
    ip.ki.wVk = 0x4B;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x49;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x53;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x53;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x41;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x4E;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x49;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x4D;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x45;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x6E;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x52;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x55;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.wVk = 0x0D;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));

    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

return 0;
}

2 个答案:

答案 0 :(得分:0)

要使用C#使用本机C ++代码,请使用PInvoke检查此链接:https://msdn.microsoft.com/en-us/library/ms235282.aspx

在C ++代码中,您可以将代码放在函数中并导出它,此链接:https://msdn.microsoft.com/en-us/library/z4zxe9k8.aspx ,然后构建您的C ++并在您的C#项目中使用本机dll。

答案 1 :(得分:0)

好的,首先你必须找出你的要求。您想从少数应用程序制作管道,还是希望进行简单的调用。

要制作类似管道的内容,您必须考虑“命名”退出代码。 例如

0 == Everything went okay,
1 == There was an error with ...
2 == Uncaught exception ...

然后在 C ++ 应用程序中使用以下代码:

int main(int argc, char** argv)
{
    BOOL no_errors = TRUE;
    try
    {
        ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", NULL, NULL, SW_SHOWNORMAL);

        Sleep(2000);
        INPUT ip = {};

        ip.type = INPUT_MOUSE;
        ip.mi.dx = 3500;
        ip.mi.dy = 2300;
        ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTDOWN;
        ip.mi.mouseData = 0;
        SendInput(1, &ip, sizeof(INPUT));

        Sleep(200);

        ip.type = INPUT_KEYBOARD;
        ip.ki.wVk = 0x4B;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x49;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x53;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x53;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x41;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x4E;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x49;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x4D;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x45;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x6E;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x52;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x55;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x0D;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &ip, sizeof(INPUT));
    }
    catch(...)
    {
        no_errors = FALSE;
    }
    if(no_errors)
        return 0; // look error code at position 0
    else
        return 1; // look error code at position 1
}

完成重新编码 C ++ 应用程序后,您可以使用 C#中的Process类启动它:

Process proc = Process.Start("me_cpp_application.exe");
proc.WaitForExit();
int result = proc.ExitCode;
// result now holds your error/exit code

另一种方法是使用平台调用调用本机代码。 这将要求您将 C ++ 代码修改为库而不是应用程序。

// c++ code :
#ifdef ME_LIB
#    define LIB_SPEC __declspec(dllexport)
#else
#    define LIB_SPEC __declspec(dllimport)
#endif

#define EXPORT_DLL extern "C" LIB_SPEC 

EXPORT_DLL  int MyCall() {
    BOOL no_errors = TRUE;
    try
    {
        ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", NULL, NULL, SW_SHOWNORMAL);

        Sleep(2000);
        INPUT ip = {};

        ip.type = INPUT_MOUSE;
        ip.mi.dx = 3500;
        ip.mi.dy = 2300;
        ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTDOWN;
        ip.mi.mouseData = 0;
        SendInput(1, &ip, sizeof(INPUT));

        Sleep(200);

        ip.type = INPUT_KEYBOARD;
        ip.ki.wVk = 0x4B;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x49;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x53;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x53;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x41;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x4E;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x49;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x4D;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x45;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x6E;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x52;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x55;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.wVk = 0x0D;
        ip.ki.dwFlags = 0;
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, &ip, sizeof(INPUT));
    }
    catch(...)
    {
        no_errors = FALSE;
    }
    if (no_errors)
        return 0;
    else
        return 1;
}

然后在 C#应用程序中使用:

[DllImport("me_cpp_library.dll")]
static extern int MyCall();

void CallMyCallNative()
{
    int result = MyCall();
}