从“DWORD”到“const char *”的转换无效 [英] Invalid conversion from 'DWORD' to 'const char *'
问题描述
我试图编译一些代码,但我收到一个错误:
I'm trying to compile some code, but I'm getting an error:
错误从
DWORD
至const char *
这里是我试图编译的代码:
Here's the code I'm trying to compile:
hWindow = FindWindow(NULL, "Window");
if (hWindow){
GetWindowThreadProcessId(hWindow, &pid);
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
if(hProcess != NULL) {
SetWindowText(GetDlgItem(MyWindow, MyStatic), pid);
}
如何转换 DWORD
到 const char *
?
推荐答案
SetWindowText(GetDlgItem(MyWindow, MyStatic), pid);
pid
它在 GetWindowThreadProcessId(hWindow,& pid)
它接受 LPDWORD
作为第二个参数)。然而, SetWindowText
需要一个C字符串作为它的第二个参数,所以代替 pid
,你必须传递一个值类型 char *
或 char []
。
pid
is a DWORD (as you used it in GetWindowThreadProcessId(hWindow, &pid)
which takes a LPDWORD
as the second argument). However, SetWindowText
expects a C-string as it's second argument, so instead of pid
, you must pass a value of type char *
or char []
.
显示 pid
的值,您可以使用 sprintf
:
To display the value of pid
, you can make use of sprintf
:
char * str = new char[10];
sprintf(str,"%d",pid);
您可能需要修改 str
一点(10可能太小,或大于必要 - 这取决于你和你的情况)。
You may have to modify the size of str
a little (10 might be too small, or bigger than necessary - that's up to you and your situation).
这篇关于从“DWORD”到“const char *”的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!