How to get Mouse input from unmanaged DLL

A

Andy

Hi all. My question is simple. How can I get mouse information from an
unmanaged DLL called from a C# windows application.

I have a very very simple C# windows application that simply overrides
the OnPaint command like so:

protected override void OnPaint(PaintEventArgs e)
{
Wrapper.OnIdle(this.Handle);
this.Invalidate(true);
}

And I the Wrapper class is defined as:
public class Wrapper
{
[ DllImport( "CppDLL.dll", EntryPoint="OnIdle" ) ]
public static extern void NDL_OnIdle(IntPtr hWnd);
}


In my C++ DLL, my OnIdle function looks like:

extern "C" PINVOKELIB_API int OnIdle(HWND hWnd)
{
// TESTING...

POINT kPt;
HDC hdc = ::GetDC(hWnd);

int iResult = ::GetCurrentPositionEx(hdc,&kPt);

if (kPt.x != 0 || kPt.y !=0)
{
int x;
x= 0; // My BREAKPOINT here never gets hit.
}

return 1;
}

In the above code, my x,y position of the mouse is never anything
other than zero. What is wrong???? I've asserted the the hdc and
iResult values are good. And I've also used the hWnd for doing things
like ::GetClientRect(hWnd,&rect); and it works... it seems that only
mouse calls don't work. I had attempted to use DirectInput from my
unmanaged DLL. I had keyboard and gamepad working... but the mouse
would not. That is why I've made the simple test above... even that
won't work. Any ideas??

Thanks,
Andy.

(e-mail address removed)
 
N

Nicholas Paldino [.NET/C# MVP]

Andy,

The problem comes from the fact that you should be calling GetCursorPos,
not GetCurrentPositionEx. GetCurrentPositionEx is used to determine the
current position on the device context for drawing, not the current mouse
position.

Remember, GetCursorPos will return the values in screen. You will have
to translate them to whatever coordinate space you want if this is not
acceptable.

Hope this helps.
 
A

Andy .

Hi Nicholas,

Thanks for the quick reply. One question though....

What is "GetCursorPos"?

I searched in the MSDN and nothing shows up. Also, it doesn't show up in
my global scope.

( Usually when you use "::" in Studio you get a drop down showing what's
available in the global scope... it's not there. )

In my C++ unmanaged Dll, I'm only including <windows.h>. What would I
need to include for GetCursorPos?

Thanks again!

Andy.

(e-mail address removed)
 
A

Andy .

Hi Nicholas,

Sorry, my bad... I read your email to quick. Thanks for the help, it
worked.

-Andy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top