2011-08-14 3 views
4

まあ、まあ、ここに事があります。このすばらしいウェブサイトを使用して、基本的なクリックスクリプトを作成するのに役立つコードスニペットが見つかりました。問題は、デバッグ時にエラーが発生し続けていることです。マウスカーソルを右に向けることです。以下、コードタグにエラーを追加しました。C#でマウスクリックをシミュレートする方法は? (私は間違いを続けています)

PInvokeStackImbalance was detected 
Message: A call to PInvoke function 'MagicMouse!MagicMouse.Form1::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. 

私はそれを私の最善の努力を与え、そしてカップルのGoogle検索を経て..しかし、私は多分私はちょうどそれが間違ってやっている、何かを見つけることができませんしました。いずれにせよ、もしあなたが私を助けることができたら、私は大好きです。以下は私の実際のコードです。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.Runtime.InteropServices; 

namespace MagicMouse 
{ 
    public partial class Form1 : Form 
    { 
     //all this stuff has to do with being able to actually click a mouse 
     [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] 
     public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); 
     private const int MOUSEEVENTF_LEFTDOWN = 0x02; 
     private const int MOUSEEVENTF_LEFTUP = 0x04; 
     private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 
     private const int MOUSEEVENTF_RIGHTUP = 0x10; 

     //this function will click the mouse using the parameters assigned to it 
     public void DoMouseClickLeft() 
     { 
      //Call the imported function with the cursor's current position 
      int X = Cursor.Position.X; 
      int Y = Cursor.Position.Y; 
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); 
     } 

     public void NumberCheck(string ValidateNum) 
     { 
      long CanConvertOut = 0; 
      bool CanConvertBool = long.TryParse(ValidateNum, out CanConvertOut); 
      if (CanConvertBool == false) 
      { 
       MessageBox.Show("Please enter a valid number"); 
       return; 
      } 
     } 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     //button 1 is start alching 
     private void button1_Click(object sender, EventArgs e) 
     { 
      //this section sends data to the number validation function 
      string ValidateNum = NumberOfItems.Text; 
      NumberCheck(ValidateNum); 
      int Alchs = Convert.ToInt16(NumberOfItems.Text); 

      for (int i = 1; i < Alchs; i++) 
      { 
       DoMouseClickLeft(); 
      } 
     } 

     //button 2 is exit 
     private void button2_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 

     } 


    } 
} 
+1

あなたはこのような質問に遭遇した場合、適切な署名のためpinvoke.netを見てみてください。 –

+0

[CSharp - 十分に文書化された関数で検出されたPInvokeStackImbalanceの重複がありますか?](http://stackoverflow.com/questions/2943883/csharp-pinvokestackimbalance-detected-on-a-well-documented-function) –

答えて

6

それはエラーメッセージが言うまさにである - にDLLIMPORT宣言を変更します、詳細については

[DllImport("user32.dll")] 
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, 
    UIntPtr dwExtraInfo); 

http://pinvoke.net/default.aspx/user32.mouse_event

を参照してください。しかしmouse_eventは廃止されているので、代わりにSendInputの使用を検討。

参照:

+0

ありがとう非常に迅速な回答、私はこれに新しいので、私のnoobishnessを言い訳してください。 – Vasu

+0

まったく問題ありません - 私たちはみなここで学びます。私は助けてくれるとうれしいです:-) – Yahia

+0

2番目の宣言が間違っています。 'int'はネイティブ型' ULONG_PTR'と等価ではありません。 –

関連する問題