I have created a simple GUI using Windows Forms in visual C++ 2008. There is a button in the GUI. When the button is pressed I want mouse cursor to point at coordinates (0,900). I have created separate header and c++ source file that sets the cursor position to specified location (x,y). For this I have used Win32's SetCursorPos() function. I wrote the code for setting the cursor position in a separate file because I want only the GUI to be built using .NET. For other functions I want to use native C++ and Win32 library.
While building the code, I get following error messages at the time of linking:
1>SimpleForms.obj : error LNK2028: unresolved token (0A00000F) "extern "C" int __stdcall SetCursorPos(int,int)" (?SetCursorPos@@$$J18YGHHH@Z) referenced in function "private: void __clrcall SimpleForms::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@SimpleForms@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>SimpleForms.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall SetCursorPos(int,int)" (?SetCursorPos@@$$J18YGHHH@Z) referenced in function "private: void __clrcall SimpleForms::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@SimpleForms@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
Note how the function name got the C++ name decoration. Your declaration of the function is wrong, using extern "C" is required. Avoid these kind of mistakes by simply including
<windows.h>
.But, don't do it this way. Windows Forms lets you move the cursor too:
I was seriously misled by the name decoration, that's an unpleasant linker error message. The real problem is that your project isn't linking the required Windows import library. Right-click the project, Properties, Linker, Input. Remove the $(NoInherit) from the Additional Dependencies setting. If you use VS2010 then put "user32.lib" in that setting.
You should still use the .NET Cursor class in this specific case.