wchar_t **WinList 指向字符数组的动态指针数组
wchar_t **WinList Dynamic pointer array to character array
我在尝试将 wchar_t 字符数组存储在全局数组中时遇到问题,
下面是我想做的代码:-
wchar_t **WinList //Global array store
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
int NumMembers;
if (WinList == nullptr)
{
NumMembers = 0;
}
else
{
NumMembers = (sizeof(**WinList) / sizeof(wchar_t));
}
wchar_t class_name[300];
GetClassName(hwnd, class_name, 300);
WinList = new wchar_t * [NumMembers + 1];
WinList[NumMembers] = class_name;
return TRUE;
}
错误:
Unhandled exception at 0x765CE2C3 (usp10.dll) in Win32Project4.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD." is occuring below at the TextOut function, WinList = 0x002802a0 {0xcdcdcdcd <Error reading characters of string.>} : -
case WM_PAINT:
if (WinList != nullptr)
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
for (int iLoop = 0; iLoop != sizeof(WinList); iLoop++)
{
TextOut(hDC, 5, iLoop*10, WinList[iLoop], (sizeof(WinList[iLoop]) / sizeof(wchar_t)));
}
}
break;
我尝试过使用 malloc 和 mcscpy 等函数和代码的各种变体。然而,我似乎误解了我认为的指针的范围。我似乎无法理解它。任何解释将不胜感激。我正在使用 VS 2013,这更像是一个学习概念,而不是有更好的方法。不过,我也很感激 'better way' 解决方案。
此致,
HonkeyPig
您的主要问题是您将回调函数的结果存储在局部变量中,该变量在 return..
时立即被丢弃
除此之外
(sizeof(**WinList) / sizeof(wchar_t))
是exactly sizeof(pointer)/2
,在 32 位架构上是 2....
我在尝试将 wchar_t 字符数组存储在全局数组中时遇到问题, 下面是我想做的代码:-
wchar_t **WinList //Global array store
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
int NumMembers;
if (WinList == nullptr)
{
NumMembers = 0;
}
else
{
NumMembers = (sizeof(**WinList) / sizeof(wchar_t));
}
wchar_t class_name[300];
GetClassName(hwnd, class_name, 300);
WinList = new wchar_t * [NumMembers + 1];
WinList[NumMembers] = class_name;
return TRUE;
}
错误:
Unhandled exception at 0x765CE2C3 (usp10.dll) in Win32Project4.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD." is occuring below at the TextOut function, WinList = 0x002802a0 {0xcdcdcdcd <Error reading characters of string.>} : -
case WM_PAINT:
if (WinList != nullptr)
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
for (int iLoop = 0; iLoop != sizeof(WinList); iLoop++)
{
TextOut(hDC, 5, iLoop*10, WinList[iLoop], (sizeof(WinList[iLoop]) / sizeof(wchar_t)));
}
}
break;
我尝试过使用 malloc 和 mcscpy 等函数和代码的各种变体。然而,我似乎误解了我认为的指针的范围。我似乎无法理解它。任何解释将不胜感激。我正在使用 VS 2013,这更像是一个学习概念,而不是有更好的方法。不过,我也很感激 'better way' 解决方案。
此致,
HonkeyPig
您的主要问题是您将回调函数的结果存储在局部变量中,该变量在 return..
时立即被丢弃除此之外
(sizeof(**WinList) / sizeof(wchar_t))
是exactly sizeof(pointer)/2
,在 32 位架构上是 2....