Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

GetCaretPos

Hi all,

I am writing some codes in Visual C++ 2005 which I need to import the function GetCaretPos in user32.dll. I tried this:

using namespace System::Runtime::InteropServices;

namespace SysWin32
{

[DllImport("user32.dll", EntryPoint="GetCaretPos", SetLastError = true,
CharSet = CharSet::Unicode, ExactSpelling = true,
CallingConvention = CallingConvention::StdCall)]
bool GetCaretPos(Point lpPoint);

}

However, when I use the function in my code:

Point MyPoint;
SysWin32::GetCaretPos(MyPoint);

I get an error saying: A call to PInvoke function has unbalanced the stack. This is likely because the managed Invoke signature does not match the unmanaged target signature...

How do I declare that function properly?

Best regards,
zion
[868 byte] By [zionykl] at [2007-11-11 8:42:14]
# 1 Re: GetCaretPos
If the signature sdon't match, stack unbalancing is the least I would expect;) the question is how these two functions are declared. i.e., what are their signature? I guess that one of them takes a pointer to Point rather than a reference.
Danny at 2007-11-11 21:01:03 >
# 2 Re: GetCaretPos
thanks danny. am still new to C++, may i know if one of them takes a pointer to Point, how would I declare it? Would it be:

bool GetCaretPos(Point* lpPoint);
zionykl at 2007-11-11 21:02:03 >
# 3 Re: GetCaretPos
yes, that's right. You may also need to pass objects by reference:

bool GetCaretPos(Point & Point);

Rarely, if ever, do you need to pass objects by value:

bool GetCaretPos(Point point); //pass by value
Danny at 2007-11-11 21:03:08 >
# 4 Re: GetCaretPos
many thanks Danny, i have tried and it worked properly now, it is really helpful of you!

this might be out of topic, i have been searching and would you recommend me any good books out there for me to pick up solid Visual C++ .Net programming skill?

thanks again.
zionykl at 2007-11-11 21:04:14 >
# 5 Re: GetCaretPos
good Visual C++. Net books are still a raity these days because the .Net specification is being drastically modified these days. Managed C++ for instance is being abandoned in favor of a new langauges misleadingly called C++/CLI. It's not really C++ but yet another C# like language that tries to port some C++ features to this framework. I think the best place to look for is MSDN. They have plenty of tutorials and code samples.
Danny at 2007-11-11 21:05:13 >