【问题标题】:Using win32 functions in windows forms project.(.net envir)在 windows 窗体项目中使用 win32 函数。(.net envir)
【发布时间】:2011-03-08 21:26:55
【问题描述】:

我需要在 clr 模式下在我的 windows 窗体项目中使用一些 win32 函数。(v c++ 2005) 我在表单项目中直接使用 win32 函数时遇到的错误是

**dbms.obj : error LNK2028: unresolved token (0A00000E) "extern "C" struct HWND__ * __stdcall GetForegroundWindow(void)" (?GetForegroundWindow@@$$J10YGPAUHWND__@@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
dbms.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall GetForegroundWindow(void)" (?GetForegroundWindow@@$$J10YGPAUHWND__@@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)**

我的主要 cpp 代码是:

#include "stdafx.h"
#include "Form1.h"
#include "windows.h"

using namespace dbms;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    HWND neu;
    neu=GetForegroundWindow();
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew Form1());
    return 0;
}

代码只是为了演示我得到的错误。 这可能很愚蠢,但我对 win32 prog 至少了解一点,但对 .net 平台一无所知。如果有人可以帮助我在上面的代码中使用 win32 func 将不胜感激。 (我没有包含 form.h 文件。我认为它可能不需要)

【问题讨论】:

  • FindowWindow 应该是 FindWindow。这只是您的问题中的一个错字吗?
  • 你想打电话给FindWindow而不是FindowWindow

标签: winapi


【解决方案1】:

这是一个链接器错误,而不是编译错误。您需要告诉链接器在 Windows 导入库中搜索这些 winapi 标识符。

在解决方案资源管理器窗口、属性、链接器、输入、附加依赖项设置中右键单击您的项目。删除 $(NoInherit)。这允许使用“Core Windows Libraries”项目属性表中的默认值,它指定最常见的 Windows dll 的 .lib 文件。包括user32.lib,声明GetForegroundWindow()的那个。

您可以通过单击文本框中的虚线按钮查看从项目属性表继承的 .lib 列表。 “继承值”列表。如果您使用导入库不在列表中的“晦涩”winapi,则需要将 .lib 的名称添加到设置中。您需要的 .lib 记录在文章底部“函数信息”部分中有关 winapi 函数的 MSDN 库文章中。

【讨论】:

  • 再次抱歉。我可以使用许多 win32 游戏,但我不能使用 MessageBox()。我再次收到错误(关于转换参数 LPCSTR 的不同错误)。但没关系,因为我会从 MessageBox::show 类中使用它。我必须对此做些什么吗?如果它愚蠢的juz忽略它!
  • 始终支持等效的 .NET 类或函数,如果出现问题,您将获得非常好的诊断。不要对此做任何事情。
猜你喜欢
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
相关资源
最近更新 更多