Question

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)
Was it helpful?

Solution

(?SetCursorPos@@$$J18YGHHH@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:

    System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        System::Windows::Forms::Cursor::Position = Point(0, 900);
    }

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.

OTHER TIPS

You can't do that.

If you want to use C++ from .NET, you must expose a C interface to .NET which you can P/Invoke, or you need to use C++/CLI instead, which will result in managed code. (Well, I suppose you could expose a COM element as well, but thats a whole other can of worms)

This sounds like you're just asking for difficulty -- is there any reason you don't want to use either completely managed code, or completely native code? Getting them to mix is a royal PITA.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top