/*----------------------------------------
_wenviron.C -- Environment List Box
(c) Charles Petzold, 1996
----------------------------------------*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#define MAXENV 4096
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = _T("_wenviron") ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, _T("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static TCHAR szBuffer[MAXENV + 1] ;
static HWND hwndList, hwndText ;
HDC hdc ;
int i ;
TEXTMETRIC tm ;
switch (iMsg)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
ReleaseDC (hwnd, hdc) ;
hwndList = CreateWindow (_T("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
tm.tmAveCharWidth, tm.tmHeight * 3,
tm.tmAveCharWidth * 16 +
GetSystemMetrics (SM_CXVSCROLL),
tm.tmHeight * 5,
hwnd, (HMENU) 1,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hwndText = CreateWindow (_T("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
tm.tmAveCharWidth, tm.tmHeight,
tm.tmAveCharWidth * MAXENV, tm.tmHeight,
hwnd, (HMENU) 2,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
[COLOR="Red"][SIZE="4"]for (i = 0 ; _wenviron ; i++)[/SIZE][/COLOR]
{
if (wcslen (_wenviron ) > MAXENV)
continue ;
*wcschr (wcscpy (szBuffer, _wenviron ), '=') = '\0' ;
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
}
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndList) ;
return 0 ;
case WM_COMMAND :
if (LOWORD (wParam) == 1 && HIWORD (wParam) == LBN_SELCHANGE)
{
i = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
i = SendMessage (hwndList, LB_GETTEXT, i,
(LPARAM) szBuffer) ;
wcscpy (szBuffer + i + 1, _wgetenv (szBuffer)) ;
*(szBuffer + i) = '=' ;
SetWindowText (hwndText, szBuffer) ;
}
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
Unhandled exception at ...
Цитата:
Unhandled exception at 0x0041187c in chap08.5_ENVIRON.exe: 0xC0000005: Access violation reading location 0x00000000.
Строка, генерирующая ошибку обозначена красным шрифтом.
Помогите, разобраться.
Код:
Не вижу декларации _wenviron, код не полон.
а строчка #include <stdlib.h> в программе есть.
Как же декларировать _wenviron ?
либо в свойствах проекта установи опцию использовать юникод (_UNICODE).
А замена _wenviron на _environ, то же не выход, потому что остальные
функции то же придётся заменять (wcslen на strlen, wcscpy на strcpy и
т.п.). Программа после таких замен компилируется без ошибок, но вместо слов пишет иероглифы.
Код:
#ifndef UNICODE
#define UNICODE 1
#endif
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#define MAXENV 4096
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("_wenviron") ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, TEXT("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static TCHAR szBuffer[MAXENV + 1] ;
static HWND hwndList, hwndText ;
HDC hdc ;
int i ;
TEXTMETRIC tm ;
switch (iMsg)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
ReleaseDC (hwnd, hdc) ;
hwndList = CreateWindow (TEXT("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
tm.tmAveCharWidth, tm.tmHeight * 3,
tm.tmAveCharWidth * 16 +
GetSystemMetrics (SM_CXVSCROLL),
tm.tmHeight * 5,
hwnd, (HMENU) 1,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hwndText = CreateWindow (TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
tm.tmAveCharWidth, tm.tmHeight,
tm.tmAveCharWidth * MAXENV, tm.tmHeight,
hwnd, (HMENU) 2,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
if ( _wenviron )
for (i = 0 ; _wenviron ; i++)
{
if (wcslen (_wenviron ) > MAXENV)
continue ;
*wcschr (wcscpy (szBuffer, _wenviron ), '=') = '\0' ;
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
}
else {
wchar_t* env = GetEnvironmentStringsW();
int c=0,oc=0, brk=1;
while (brk) {
if (env[c]==0) {
if (env[c+1]==0) {
brk=0;
}
wcscpy (szBuffer, &env[oc]);
*wcschr (szBuffer, '=') = '\0';
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
oc=c+1;
}
c++;
}
FreeEnvironmentStrings(env);
}
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndList) ;
return 0 ;
case WM_COMMAND :
if (LOWORD (wParam) == 1 && HIWORD (wParam) == LBN_SELCHANGE)
{
i = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
i = SendMessage (hwndList, LB_GETTEXT, i,
(LPARAM) szBuffer) ;
if (szBuffer[0]) {
wcscpy (szBuffer + i + 1, _wgetenv (szBuffer)) ;
*(szBuffer + i) = '=' ;
}
SetWindowText (hwndText, szBuffer) ;
}
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
#define UNICODE 1
#endif
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#define MAXENV 4096
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("_wenviron") ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, TEXT("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static TCHAR szBuffer[MAXENV + 1] ;
static HWND hwndList, hwndText ;
HDC hdc ;
int i ;
TEXTMETRIC tm ;
switch (iMsg)
{
case WM_CREATE :
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
ReleaseDC (hwnd, hdc) ;
hwndList = CreateWindow (TEXT("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
tm.tmAveCharWidth, tm.tmHeight * 3,
tm.tmAveCharWidth * 16 +
GetSystemMetrics (SM_CXVSCROLL),
tm.tmHeight * 5,
hwnd, (HMENU) 1,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hwndText = CreateWindow (TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
tm.tmAveCharWidth, tm.tmHeight,
tm.tmAveCharWidth * MAXENV, tm.tmHeight,
hwnd, (HMENU) 2,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
if ( _wenviron )
for (i = 0 ; _wenviron ; i++)
{
if (wcslen (_wenviron ) > MAXENV)
continue ;
*wcschr (wcscpy (szBuffer, _wenviron ), '=') = '\0' ;
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
}
else {
wchar_t* env = GetEnvironmentStringsW();
int c=0,oc=0, brk=1;
while (brk) {
if (env[c]==0) {
if (env[c+1]==0) {
brk=0;
}
wcscpy (szBuffer, &env[oc]);
*wcschr (szBuffer, '=') = '\0';
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szBuffer) ;
oc=c+1;
}
c++;
}
FreeEnvironmentStrings(env);
}
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndList) ;
return 0 ;
case WM_COMMAND :
if (LOWORD (wParam) == 1 && HIWORD (wParam) == LBN_SELCHANGE)
{
i = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
i = SendMessage (hwndList, LB_GETTEXT, i,
(LPARAM) szBuffer) ;
if (szBuffer[0]) {
wcscpy (szBuffer + i + 1, _wgetenv (szBuffer)) ;
*(szBuffer + i) = '=' ;
}
SetWindowText (hwndText, szBuffer) ;
}
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
Хотя _wenviron так не вовлечен в программу.