通过 Windows 窗体元素使用 Win32 库
我在 Visual C++ 2008 中使用 Windows 窗体创建了一个简单的 GUI。GUI 中有一个按钮。当按下按钮时,我希望鼠标光标指向坐标(0,900)。我创建了单独的标头和 C++ 源文件,将光标位置设置为指定位置 (x,y)。为此,我使用了 Win32 的 SetCursorPos() 函数。我在单独的文件中编写了用于设置光标位置的代码,因为我只想使用 .NET 构建 GUI。对于其他功能,我想使用本机 C++ 和 Win32 库。
在构建代码时,我在链接时收到以下错误消息:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意函数名称如何获得 C++ 名称修饰。您的函数声明是错误的,需要使用 extern "C" 。只需包含
即可避免此类错误。但是,不要这样做。 Windows 窗体也允许您移动光标:
我被名称装饰严重误导了,这是一条令人不快的链接器错误消息。真正的问题是您的项目没有链接所需的 Windows 导入库。右键单击项目、属性、链接器、输入。从“附加依赖项”设置中删除 $(NoInherit)。如果您使用 VS2010,则将“user32.lib”放入该设置中。
在这种特定情况下,您仍然应该使用 .NET Cursor 类。
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.
你不能那样做。
如果要从 .NET 使用 C++,则必须向 .NET 公开一个可以 P/Invoke 的 C 接口,或者需要使用 C++/CLI,这将产生托管代码。 (好吧,我想您也可以公开 COM 元素,但这完全是另一回事)
这听起来像是您在自找困难——您是否有任何理由不想使用完全托管的代码,还是完全本机代码?让它们混合起来就是皇家皮塔饼。
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.