Hi all,

I've use a CStringArray to store some CStrings in my application. Now I want to use another CStringArray on the same application. So I add another CStringArray instance on the class view, VC++ .Net 2003.

When I add and compile it don't give any error message. But when I run the code it gives the following error.

Unhandled exception at 0x00414ff3 in TimeZone.exe: 0xC0000005: Access violation writing location 0x00000020.

on following line of code,

m_pMainWnd = &dlg;

Why is that. Can you guys give me a clue.

Recommended Answers

All 4 Replies

m_pMainWnd = &dlg; <<< wrong m_pMainWnd = dlg.m_hWnd; <<< correct assuming m_pMainWnd is type HWND.

Actually I don't write that line of code. It is automatically generated when I create the first CStringArray in my application.

please post code. I don't have MFC project is front of me, I think my previous post was incorrect. The original code is ok. Your problem is really something else because I used CStringArray successfully lots of times but not with your version of the compiler.

Here is the part of code where I use the first CStringArray.

void CTimeZoneDlg::GetTM(void)
{
	HKEY hKey, Key;// Key, subkey
	DWORD dwType = REG_SZ;
	int iRet = 0;
	CString strName, strValue, strPath, strKey;

// Set the Times Zones key path
	strPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");

// Open the main key
	iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_READ, &hKey);

// Check the succed of the handler
	if (iRet != ERROR_SUCCESS)
	{
		return;
	}

// For RegQueryInfoKey
    TCHAR    achKey[MAX_KEY_LENGTH];		// buffer for subkey name
    DWORD    cbName;						// size of name string 
    TCHAR    achClass[MAX_PATH] = TEXT(""); // buffer for class name 
    DWORD    cchClassName = MAX_PATH;		// size of class string 
    DWORD    cSubKeys = 0;			// number of subkeys 
    DWORD    cbMaxSubKey;			// longest subkey size 
    DWORD    cchMaxClass;			// longest class string 
	DWORD    cValues;				// number of values for key 
    DWORD    cchMaxValue;			// longest value name 
    DWORD    cbMaxValueData;		// longest value data 
    DWORD    cbSecurityDescriptor;	// size of security descriptor 
    FILETIME ftLastWriteTime;		// last write time

	DWORD i, retCode;

	TCHAR buffer[MAX_PATH];
	DWORD dwSize = sizeof(buffer);

// Get the class name and the value count.
	retCode = RegQueryInfoKey(
		hKey,					// key handle
		achClass,				// buffer for class name
		&cchClassName,			// length of class string
		NULL,					// reserved
		&cSubKeys,				// number of subkeys
		&cbMaxSubKey,			// longest subkey size
		&cchMaxClass,			// longest class string
		&cValues,				// number of values for this key
		&cchMaxValue,			// longest value name
		&cbMaxValueData,		// longest value data
		&cbSecurityDescriptor,	// security descriptor
		&ftLastWriteTime);		// last write time

// Enumerate the child keys, until RegEnumKeyEx fails. Then
// get the name of each sub key and copy it into the list box.

	if(cSubKeys)// Make sure that main key has sub keys
	{
		for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
		{
			ZeroMemory(&achKey, sizeof(achKey));
			cbMaxSubKey = sizeof(achKey);
			retCode = RegEnumKeyEx(hKey, i, achKey, &cbMaxSubKey, NULL, NULL, NULL, &ftLastWriteTime);
			if (retCode == (DWORD)ERROR_SUCCESS)
			{
				strName.Format( _T("%s"), achKey);

				strKey = strPath + "\\" + strName;
				iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, &Key);
				if (iRet == ERROR_SUCCESS)
				{
					ZeroMemory(&buffer, sizeof(buffer));
					dwSize = sizeof(buffer); //again
					iRet = RegQueryValueEx(Key, _T("Display"), NULL, &dwType, (BYTE *)buffer, &dwSize);
					if (iRet == ERROR_SUCCESS)
					{
						strValue.Format(_T("%s"), buffer);
						m_pszTimeZones.Add( strValue );
					}
					RegCloseKey(Key);
				}
			}
		}
	}
	RegCloseKey( hKey );
}

Defined the CStringArray in .h file as

private:
	CStringArray m_pszTimeZones;

Always use the IDE to add variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.