Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I'm trying to declare the mouse_event API in C# 2005 Express. What's wrong?

 

[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);

 

And I get the following error:

 

Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)

Edited by JumpyNET
Posted
[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);

 

Your types are the wrong size. the definition of mouse_event from the documentation is:

VOID mouse_event(
   DWORD dwFlags,
   DWORD dx,
   DWORD dy,
   DWORD dwData,
   ULONG_PTR dwExtraInfo
);

DWORD is an unsigned 32 bit integer, you're using long which is 64 bits and signed. The definitionof ULONG_PTR is debatable but i'd go with UIntPtr or IntPtr myself. I'd also say you can make the first type use a Flags marked enumeration of the values outlined in the documentation preventing lots of pointless casting internally once you've made the call.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...