gpt4 book ai didi

C++ FindWindow() 问题

转载 作者:行者123 更新时间:2023-11-30 02:46:42 25 4
gpt4 key购买 nike

我试图让一个函数找到一个窗口句柄。我已经通过以下方式多次这样做了:

HWND windowHandle
windowHandle = FindWindow(NULL, "NameOfWindowIAmLookingFor");

但是,我随后尝试执行以下操作:

string myString = "NameOfWindowIAmLookingFor";
HWND windowHandle
windowHandle = FindWindow(NULL, myString);

出现以下错误:

error: cannot convert 'std::string {aka std::basic_string<char>)' to 'LPCSTR {aka const char*} ' for argument '2' to 'HWND__* FindWindowA(LPCSTR, LPCSTR)';

我有另一个函数给 myString 一个值,所以我想将该值作为变量传递给 FindWindow() 函数,但是出现了这个错误,我不明白发生了什么。

问题::为什么我会收到此错误以及如何将字符串变量放入 FindWindow() 函数?

最佳答案

Why am I getting this error and how can I go about putting a string variable into the FindWindow() function?

编译器错误信息非常清楚。 FindWindow() 函数需要一个 const char* 作为第二个参数,而 std::string 不是。
要获取指向由 std::string 实例管理的原始字符数组数据的 (const) 指针,请使用 c_str() 方法:

FindWindow(NULL, myString.c_str()); 

关于C++ FindWindow() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com