SendInput just doesn't work.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sin Jeong-hun

    SendInput just doesn't work.

    Hi. I need to simulate keyboard and mouse events. For the first step,
    I tried to move mouse using SendInput. But it didn't work. I searched
    all over the internet for an example, but only questions were there.

    Please give me what was wrong in the following code. I simplified the
    code so that you can just copy it to a console app project, and run
    it. Thank you for any help.

    PS: I need to work with SendInput the way it is, not SendMessage or
    modified INPUT structure, because there are a lot of other things to
    do with SendInput.
    Thank you.

    --------Code (Program.cs)-------------
    using System;
    using System.Componen tModel;
    using System.Runtime. InteropServices ;

    class Program
    {
    static void Main(string[] args)
    {
    MOUSEINPUT m = new MOUSEINPUT();
    m.dx = 0;
    m.dy = 0;
    m.mouseData = 0;
    m.time = 0;
    m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;

    INPUT i = new INPUT();
    i.type = INPUT_MOUSE;
    i.mi = m;

    INPUT[] inputs = new INPUT[] { i };
    int isize = Marshal.SizeOf( i);
    SendInput(1, inputs, isize);
    }
    [DllImport("user 32.dll", SetLastError = true)]
    static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
    cbSize);
    [StructLayout(La youtKind.Explic it)]
    struct INPUT
    {
    [FieldOffset(0)]
    public int type;
    [FieldOffset(4)]
    public MOUSEINPUT mi;
    [FieldOffset(4)]
    public KEYBDINPUT ki;
    [FieldOffset(4)]
    public HARDWAREINPUT hi;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct MOUSEINPUT
    {
    public int dx;
    public int dy;
    public uint mouseData;
    public uint dwFlags;
    public uint time;
    public IntPtr dwExtraInfo;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct KEYBDINPUT
    {
    ushort wVk;
    ushort wScan;
    uint dwFlags;
    uint time;
    IntPtr dwExtraInfo;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct HARDWAREINPUT
    {
    uint uMsg;
    ushort wParamL;
    ushort wParamH;
    }
    const uint MOUSEEVENTF_MOV E = 0x0001;
    const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
    const uint MOUSEEVENTF_LEF TUP = 0x0004;
    const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
    const uint MOUSEEVENTF_RIG HTUP = 0x0010;
    const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
    const uint MOUSEEVENTF_MID DLEUP = 0x0040;
    const uint MOUSEEVENTF_XDO WN = 0x0080;
    const uint MOUSEEVENTF_XUP = 0x0100;
    const uint MOUSEEVENTF_WHE EL = 0x0800;
    const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
    const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
    const int INPUT_MOUSE = 0;
    const int INPUT_KEYBOARD = 1;
    const int INPUT_HARDWARE = 2;
    }

  • Sin Jeong-hun

    #2
    Re: SendInput just doesn't work.

    While wating for a reply from kind people, I tried myself that code in
    native C++ code.

    #include "stdafx.h"
    #include "windows.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    MOUSEINPUT mi;
    mi.dx=0;
    mi.dy=0;
    mi.dwExtraInfo= 0;
    mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
    mi.mouseData=0;
    INPUT pInputs[1];
    pInputs[0].mi=mi;
    pInputs[0].type = INPUT_MOUSE;
    SendInput(1,pIn puts,sizeof(INP UT));
    return 0;
    }
    It just worked fine. Why the C# code below doesn't work?
    Please give me a hint. Thank you very much.


    On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
    Hi. I need to simulate keyboard and mouse events. For the first step,
    I tried to move mouse using SendInput. But it didn't work. I searched
    all over the internet for an example, but only questions were there.
    >
    Please give me what was wrong in the following code. I simplified the
    code so that you can just copy it to a console app project, and run
    it. Thank you for any help.
    >
    PS: I need to work with SendInput the way it is, not SendMessage or
    modified INPUT structure, because there are a lot of other things to
    do with SendInput.
    Thank you.
    >
    --------Code (Program.cs)-------------
    using System;
    using System.Componen tModel;
    using System.Runtime. InteropServices ;
    >
    class Program
    {
    static void Main(string[] args)
    {
    MOUSEINPUT m = new MOUSEINPUT();
    m.dx = 0;
    m.dy = 0;
    m.mouseData = 0;
    m.time = 0;
    m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
    >
    INPUT i = new INPUT();
    i.type = INPUT_MOUSE;
    i.mi = m;
    >
    INPUT[] inputs = new INPUT[] { i };
    int isize = Marshal.SizeOf( i);
    SendInput(1, inputs, isize);
    }
    [DllImport("user 32.dll", SetLastError = true)]
    static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
    cbSize);
    [StructLayout(La youtKind.Explic it)]
    struct INPUT
    {
    [FieldOffset(0)]
    public int type;
    [FieldOffset(4)]
    public MOUSEINPUT mi;
    [FieldOffset(4)]
    public KEYBDINPUT ki;
    [FieldOffset(4)]
    public HARDWAREINPUT hi;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct MOUSEINPUT
    {
    public int dx;
    public int dy;
    public uint mouseData;
    public uint dwFlags;
    public uint time;
    public IntPtr dwExtraInfo;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct KEYBDINPUT
    {
    ushort wVk;
    ushort wScan;
    uint dwFlags;
    uint time;
    IntPtr dwExtraInfo;
    }
    [StructLayout(La youtKind.Sequen tial)]
    struct HARDWAREINPUT
    {
    uint uMsg;
    ushort wParamL;
    ushort wParamH;
    }
    const uint MOUSEEVENTF_MOV E = 0x0001;
    const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
    const uint MOUSEEVENTF_LEF TUP = 0x0004;
    const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
    const uint MOUSEEVENTF_RIG HTUP = 0x0010;
    const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
    const uint MOUSEEVENTF_MID DLEUP = 0x0040;
    const uint MOUSEEVENTF_XDO WN = 0x0080;
    const uint MOUSEEVENTF_XUP = 0x0100;
    const uint MOUSEEVENTF_WHE EL = 0x0800;
    const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
    const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
    const int INPUT_MOUSE = 0;
    const int INPUT_KEYBOARD = 1;
    const int INPUT_HARDWARE = 2;
    >
    }

    Comment

    • Sin Jeong-hun

      #3
      Re: SendInput just doesn't work.

      I have found out the reason. My Windows is 64bit version and the .NET
      app becomes 64bit application. I changed the build option to x86, and
      it worked.

      Still don't know why the same API call fails if the .NET app is 64bit.
      Do you know why?


      On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
      While wating for a reply from kind people, I tried myself that code in
      native C++ code.
      >
      #include "stdafx.h"
      #include "windows.h"
      int _tmain(int argc, _TCHAR* argv[])
      {
      MOUSEINPUT mi;
      mi.dx=0;
      mi.dy=0;
      mi.dwExtraInfo= 0;
      mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
      mi.mouseData=0;
      INPUT pInputs[1];
      pInputs[0].mi=mi;
      pInputs[0].type = INPUT_MOUSE;
      SendInput(1,pIn puts,sizeof(INP UT));
      return 0;}
      >
      It just worked fine. Why the C# code below doesn't work?
      Please give me a hint. Thank you very much.
      >
      On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
      >
      Hi. I need to simulate keyboard and mouse events. For the first step,
      I tried to move mouse using SendInput. But it didn't work. I searched
      all over the internet for an example, but only questions were there.
      >
      Please give me what was wrong in the following code. I simplified the
      code so that you can just copy it to a console app project, and run
      it. Thank you for any help.
      >
      PS: I need to work with SendInput the way it is, not SendMessage or
      modified INPUT structure, because there are a lot of other things to
      do with SendInput.
      Thank you.
      >
      --------Code (Program.cs)-------------
      using System;
      using System.Componen tModel;
      using System.Runtime. InteropServices ;
      >
      class Program
      {
      static void Main(string[] args)
      {
      MOUSEINPUT m = new MOUSEINPUT();
      m.dx = 0;
      m.dy = 0;
      m.mouseData = 0;
      m.time = 0;
      m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
      >
      INPUT i = new INPUT();
      i.type = INPUT_MOUSE;
      i.mi = m;
      >
      INPUT[] inputs = new INPUT[] { i };
      int isize = Marshal.SizeOf( i);
      SendInput(1, inputs, isize);
      }
      [DllImport("user 32.dll", SetLastError = true)]
      static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
      cbSize);
      [StructLayout(La youtKind.Explic it)]
      struct INPUT
      {
      [FieldOffset(0)]
      public int type;
      [FieldOffset(4)]
      public MOUSEINPUT mi;
      [FieldOffset(4)]
      public KEYBDINPUT ki;
      [FieldOffset(4)]
      public HARDWAREINPUT hi;
      }
      [StructLayout(La youtKind.Sequen tial)]
      struct MOUSEINPUT
      {
      public int dx;
      public int dy;
      public uint mouseData;
      public uint dwFlags;
      public uint time;
      public IntPtr dwExtraInfo;
      }
      [StructLayout(La youtKind.Sequen tial)]
      struct KEYBDINPUT
      {
      ushort wVk;
      ushort wScan;
      uint dwFlags;
      uint time;
      IntPtr dwExtraInfo;
      }
      [StructLayout(La youtKind.Sequen tial)]
      struct HARDWAREINPUT
      {
      uint uMsg;
      ushort wParamL;
      ushort wParamH;
      }
      const uint MOUSEEVENTF_MOV E = 0x0001;
      const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
      const uint MOUSEEVENTF_LEF TUP = 0x0004;
      const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
      const uint MOUSEEVENTF_RIG HTUP = 0x0010;
      const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
      const uint MOUSEEVENTF_MID DLEUP = 0x0040;
      const uint MOUSEEVENTF_XDO WN = 0x0080;
      const uint MOUSEEVENTF_XUP = 0x0100;
      const uint MOUSEEVENTF_WHE EL = 0x0800;
      const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
      const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
      const int INPUT_MOUSE = 0;
      const int INPUT_KEYBOARD = 1;
      const int INPUT_HARDWARE = 2;
      >
      }

      Comment

      • Sin Jeong-hun

        #4
        Re: SendInput just doesn't work.

        I have found out the reason. My Windows is 64bit version and the .NET
        app becomes 64bit application. I changed the build option to x86, and
        it worked.

        Still don't know why the same API call fails if the .NET app is 64bit.
        Do you know why?


        On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
        While wating for a reply from kind people, I tried myself that code in
        native C++ code.
        >
        #include "stdafx.h"
        #include "windows.h"
        int _tmain(int argc, _TCHAR* argv[])
        {
        MOUSEINPUT mi;
        mi.dx=0;
        mi.dy=0;
        mi.dwExtraInfo= 0;
        mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
        mi.mouseData=0;
        INPUT pInputs[1];
        pInputs[0].mi=mi;
        pInputs[0].type = INPUT_MOUSE;
        SendInput(1,pIn puts,sizeof(INP UT));
        return 0;}
        >
        It just worked fine. Why the C# code below doesn't work?
        Please give me a hint. Thank you very much.
        >
        On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
        >
        Hi. I need to simulate keyboard and mouse events. For the first step,
        I tried to move mouse using SendInput. But it didn't work. I searched
        all over the internet for an example, but only questions were there.
        >
        Please give me what was wrong in the following code. I simplified the
        code so that you can just copy it to a console app project, and run
        it. Thank you for any help.
        >
        PS: I need to work with SendInput the way it is, not SendMessage or
        modified INPUT structure, because there are a lot of other things to
        do with SendInput.
        Thank you.
        >
        --------Code (Program.cs)-------------
        using System;
        using System.Componen tModel;
        using System.Runtime. InteropServices ;
        >
        class Program
        {
        static void Main(string[] args)
        {
        MOUSEINPUT m = new MOUSEINPUT();
        m.dx = 0;
        m.dy = 0;
        m.mouseData = 0;
        m.time = 0;
        m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
        >
        INPUT i = new INPUT();
        i.type = INPUT_MOUSE;
        i.mi = m;
        >
        INPUT[] inputs = new INPUT[] { i };
        int isize = Marshal.SizeOf( i);
        SendInput(1, inputs, isize);
        }
        [DllImport("user 32.dll", SetLastError = true)]
        static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
        cbSize);
        [StructLayout(La youtKind.Explic it)]
        struct INPUT
        {
        [FieldOffset(0)]
        public int type;
        [FieldOffset(4)]
        public MOUSEINPUT mi;
        [FieldOffset(4)]
        public KEYBDINPUT ki;
        [FieldOffset(4)]
        public HARDWAREINPUT hi;
        }
        [StructLayout(La youtKind.Sequen tial)]
        struct MOUSEINPUT
        {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
        }
        [StructLayout(La youtKind.Sequen tial)]
        struct KEYBDINPUT
        {
        ushort wVk;
        ushort wScan;
        uint dwFlags;
        uint time;
        IntPtr dwExtraInfo;
        }
        [StructLayout(La youtKind.Sequen tial)]
        struct HARDWAREINPUT
        {
        uint uMsg;
        ushort wParamL;
        ushort wParamH;
        }
        const uint MOUSEEVENTF_MOV E = 0x0001;
        const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
        const uint MOUSEEVENTF_LEF TUP = 0x0004;
        const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
        const uint MOUSEEVENTF_RIG HTUP = 0x0010;
        const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
        const uint MOUSEEVENTF_MID DLEUP = 0x0040;
        const uint MOUSEEVENTF_XDO WN = 0x0080;
        const uint MOUSEEVENTF_XUP = 0x0100;
        const uint MOUSEEVENTF_WHE EL = 0x0800;
        const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
        const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
        const int INPUT_MOUSE = 0;
        const int INPUT_KEYBOARD = 1;
        const int INPUT_HARDWARE = 2;
        >
        }

        Comment

        • Sin Jeong-hun

          #5
          Re: SendInput just doesn't work.

          I have found out the reason. My Windows is 64bit version and the .NET
          app becomes 64bit application. I changed the build option to x86, and
          it worked.

          Still don't know why the same API call fails if the .NET app is 64bit.
          Do you know why?


          On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
          While wating for a reply from kind people, I tried myself that code in
          native C++ code.
          >
          #include "stdafx.h"
          #include "windows.h"
          int _tmain(int argc, _TCHAR* argv[])
          {
          MOUSEINPUT mi;
          mi.dx=0;
          mi.dy=0;
          mi.dwExtraInfo= 0;
          mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
          mi.mouseData=0;
          INPUT pInputs[1];
          pInputs[0].mi=mi;
          pInputs[0].type = INPUT_MOUSE;
          SendInput(1,pIn puts,sizeof(INP UT));
          return 0;}
          >
          It just worked fine. Why the C# code below doesn't work?
          Please give me a hint. Thank you very much.
          >
          On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
          >
          Hi. I need to simulate keyboard and mouse events. For the first step,
          I tried to move mouse using SendInput. But it didn't work. I searched
          all over the internet for an example, but only questions were there.
          >
          Please give me what was wrong in the following code. I simplified the
          code so that you can just copy it to a console app project, and run
          it. Thank you for any help.
          >
          PS: I need to work with SendInput the way it is, not SendMessage or
          modified INPUT structure, because there are a lot of other things to
          do with SendInput.
          Thank you.
          >
          --------Code (Program.cs)-------------
          using System;
          using System.Componen tModel;
          using System.Runtime. InteropServices ;
          >
          class Program
          {
          static void Main(string[] args)
          {
          MOUSEINPUT m = new MOUSEINPUT();
          m.dx = 0;
          m.dy = 0;
          m.mouseData = 0;
          m.time = 0;
          m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
          >
          INPUT i = new INPUT();
          i.type = INPUT_MOUSE;
          i.mi = m;
          >
          INPUT[] inputs = new INPUT[] { i };
          int isize = Marshal.SizeOf( i);
          SendInput(1, inputs, isize);
          }
          [DllImport("user 32.dll", SetLastError = true)]
          static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
          cbSize);
          [StructLayout(La youtKind.Explic it)]
          struct INPUT
          {
          [FieldOffset(0)]
          public int type;
          [FieldOffset(4)]
          public MOUSEINPUT mi;
          [FieldOffset(4)]
          public KEYBDINPUT ki;
          [FieldOffset(4)]
          public HARDWAREINPUT hi;
          }
          [StructLayout(La youtKind.Sequen tial)]
          struct MOUSEINPUT
          {
          public int dx;
          public int dy;
          public uint mouseData;
          public uint dwFlags;
          public uint time;
          public IntPtr dwExtraInfo;
          }
          [StructLayout(La youtKind.Sequen tial)]
          struct KEYBDINPUT
          {
          ushort wVk;
          ushort wScan;
          uint dwFlags;
          uint time;
          IntPtr dwExtraInfo;
          }
          [StructLayout(La youtKind.Sequen tial)]
          struct HARDWAREINPUT
          {
          uint uMsg;
          ushort wParamL;
          ushort wParamH;
          }
          const uint MOUSEEVENTF_MOV E = 0x0001;
          const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
          const uint MOUSEEVENTF_LEF TUP = 0x0004;
          const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
          const uint MOUSEEVENTF_RIG HTUP = 0x0010;
          const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
          const uint MOUSEEVENTF_MID DLEUP = 0x0040;
          const uint MOUSEEVENTF_XDO WN = 0x0080;
          const uint MOUSEEVENTF_XUP = 0x0100;
          const uint MOUSEEVENTF_WHE EL = 0x0800;
          const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
          const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
          const int INPUT_MOUSE = 0;
          const int INPUT_KEYBOARD = 1;
          const int INPUT_HARDWARE = 2;
          >
          }

          Comment

          • Sin Jeong-hun

            #6
            Re: SendInput just doesn't work.

            I have found out the reason. My Windows is 64bit version and the .NET
            app becomes 64bit application. I changed the build option to x86, and
            it worked.

            Still don't know why the same API call fails if the .NET app is 64bit.
            Do you know why?


            On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
            While wating for a reply from kind people, I tried myself that code in
            native C++ code.
            >
            #include "stdafx.h"
            #include "windows.h"
            int _tmain(int argc, _TCHAR* argv[])
            {
            MOUSEINPUT mi;
            mi.dx=0;
            mi.dy=0;
            mi.dwExtraInfo= 0;
            mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
            mi.mouseData=0;
            INPUT pInputs[1];
            pInputs[0].mi=mi;
            pInputs[0].type = INPUT_MOUSE;
            SendInput(1,pIn puts,sizeof(INP UT));
            return 0;}
            >
            It just worked fine. Why the C# code below doesn't work?
            Please give me a hint. Thank you very much.
            >
            On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
            >
            Hi. I need to simulate keyboard and mouse events. For the first step,
            I tried to move mouse using SendInput. But it didn't work. I searched
            all over the internet for an example, but only questions were there.
            >
            Please give me what was wrong in the following code. I simplified the
            code so that you can just copy it to a console app project, and run
            it. Thank you for any help.
            >
            PS: I need to work with SendInput the way it is, not SendMessage or
            modified INPUT structure, because there are a lot of other things to
            do with SendInput.
            Thank you.
            >
            --------Code (Program.cs)-------------
            using System;
            using System.Componen tModel;
            using System.Runtime. InteropServices ;
            >
            class Program
            {
            static void Main(string[] args)
            {
            MOUSEINPUT m = new MOUSEINPUT();
            m.dx = 0;
            m.dy = 0;
            m.mouseData = 0;
            m.time = 0;
            m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
            >
            INPUT i = new INPUT();
            i.type = INPUT_MOUSE;
            i.mi = m;
            >
            INPUT[] inputs = new INPUT[] { i };
            int isize = Marshal.SizeOf( i);
            SendInput(1, inputs, isize);
            }
            [DllImport("user 32.dll", SetLastError = true)]
            static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
            cbSize);
            [StructLayout(La youtKind.Explic it)]
            struct INPUT
            {
            [FieldOffset(0)]
            public int type;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
            }
            [StructLayout(La youtKind.Sequen tial)]
            struct MOUSEINPUT
            {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
            }
            [StructLayout(La youtKind.Sequen tial)]
            struct KEYBDINPUT
            {
            ushort wVk;
            ushort wScan;
            uint dwFlags;
            uint time;
            IntPtr dwExtraInfo;
            }
            [StructLayout(La youtKind.Sequen tial)]
            struct HARDWAREINPUT
            {
            uint uMsg;
            ushort wParamL;
            ushort wParamH;
            }
            const uint MOUSEEVENTF_MOV E = 0x0001;
            const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
            const uint MOUSEEVENTF_LEF TUP = 0x0004;
            const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
            const uint MOUSEEVENTF_RIG HTUP = 0x0010;
            const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
            const uint MOUSEEVENTF_MID DLEUP = 0x0040;
            const uint MOUSEEVENTF_XDO WN = 0x0080;
            const uint MOUSEEVENTF_XUP = 0x0100;
            const uint MOUSEEVENTF_WHE EL = 0x0800;
            const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
            const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
            const int INPUT_MOUSE = 0;
            const int INPUT_KEYBOARD = 1;
            const int INPUT_HARDWARE = 2;
            >
            }

            Comment

            • Sin Jeong-hun

              #7
              Re: SendInput just doesn't work.

              I have found out the reason. My Windows is 64bit version and the .NET
              app becomes 64bit application. I changed the build option to x86, and
              it worked.

              Still don't know why the same API call fails if the .NET app is 64bit.
              Do you know why?


              On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
              While wating for a reply from kind people, I tried myself that code in
              native C++ code.
              >
              #include "stdafx.h"
              #include "windows.h"
              int _tmain(int argc, _TCHAR* argv[])
              {
              MOUSEINPUT mi;
              mi.dx=0;
              mi.dy=0;
              mi.dwExtraInfo= 0;
              mi.dwFlags=MOUS EEVENTF_ABSOLUT E|MOUSEEVENTF_M OVE;
              mi.mouseData=0;
              INPUT pInputs[1];
              pInputs[0].mi=mi;
              pInputs[0].type = INPUT_MOUSE;
              SendInput(1,pIn puts,sizeof(INP UT));
              return 0;}
              >
              It just worked fine. Why the C# code below doesn't work?
              Please give me a hint. Thank you very much.
              >
              On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmai l.comwrote:
              >
              Hi. I need to simulate keyboard and mouse events. For the first step,
              I tried to move mouse using SendInput. But it didn't work. I searched
              all over the internet for an example, but only questions were there.
              >
              Please give me what was wrong in the following code. I simplified the
              code so that you can just copy it to a console app project, and run
              it. Thank you for any help.
              >
              PS: I need to work with SendInput the way it is, not SendMessage or
              modified INPUT structure, because there are a lot of other things to
              do with SendInput.
              Thank you.
              >
              --------Code (Program.cs)-------------
              using System;
              using System.Componen tModel;
              using System.Runtime. InteropServices ;
              >
              class Program
              {
              static void Main(string[] args)
              {
              MOUSEINPUT m = new MOUSEINPUT();
              m.dx = 0;
              m.dy = 0;
              m.mouseData = 0;
              m.time = 0;
              m.dwFlags = MOUSEEVENTF_ABS OLUTE | MOUSEEVENTF_MOV E;
              >
              INPUT i = new INPUT();
              i.type = INPUT_MOUSE;
              i.mi = m;
              >
              INPUT[] inputs = new INPUT[] { i };
              int isize = Marshal.SizeOf( i);
              SendInput(1, inputs, isize);
              }
              [DllImport("user 32.dll", SetLastError = true)]
              static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
              cbSize);
              [StructLayout(La youtKind.Explic it)]
              struct INPUT
              {
              [FieldOffset(0)]
              public int type;
              [FieldOffset(4)]
              public MOUSEINPUT mi;
              [FieldOffset(4)]
              public KEYBDINPUT ki;
              [FieldOffset(4)]
              public HARDWAREINPUT hi;
              }
              [StructLayout(La youtKind.Sequen tial)]
              struct MOUSEINPUT
              {
              public int dx;
              public int dy;
              public uint mouseData;
              public uint dwFlags;
              public uint time;
              public IntPtr dwExtraInfo;
              }
              [StructLayout(La youtKind.Sequen tial)]
              struct KEYBDINPUT
              {
              ushort wVk;
              ushort wScan;
              uint dwFlags;
              uint time;
              IntPtr dwExtraInfo;
              }
              [StructLayout(La youtKind.Sequen tial)]
              struct HARDWAREINPUT
              {
              uint uMsg;
              ushort wParamL;
              ushort wParamH;
              }
              const uint MOUSEEVENTF_MOV E = 0x0001;
              const uint MOUSEEVENTF_LEF TDOWN = 0x0002;
              const uint MOUSEEVENTF_LEF TUP = 0x0004;
              const uint MOUSEEVENTF_RIG HTDOWN = 0x0008;
              const uint MOUSEEVENTF_RIG HTUP = 0x0010;
              const uint MOUSEEVENTF_MID DLEDOWN = 0x0020;
              const uint MOUSEEVENTF_MID DLEUP = 0x0040;
              const uint MOUSEEVENTF_XDO WN = 0x0080;
              const uint MOUSEEVENTF_XUP = 0x0100;
              const uint MOUSEEVENTF_WHE EL = 0x0800;
              const uint MOUSEEVENTF_VIR TUALDESK = 0x4000;
              const uint MOUSEEVENTF_ABS OLUTE = 0x8000;
              const int INPUT_MOUSE = 0;
              const int INPUT_KEYBOARD = 1;
              const int INPUT_HARDWARE = 2;
              >
              }

              Comment

              • Mattias Sjögren

                #8
                Re: SendInput just doesn't work.

                >Still don't know why the same API call fails if the .NET app is 64bit.
                >Do you know why?
                I believe all the FieldOffset(4) must be FieldOffset(8) when running
                as a 64-bit app.

                Since you're only using the MOUSEINPUT part of the union, and it
                happens to be the biggest one of the three, you can remove the other
                two and change the declaration of INPUT to

                struct INPUT
                {
                public int type;
                public MOUSEINPUT mi;
                }

                That should hopefully make the code work on both 32-bit and 64-bit.



                Mattias

                --
                Mattias Sjögren [C# MVP] mattias @ mvps.org
                http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                Please reply only to the newsgroup.

                Comment

                Working...