Re: Run-Time Check Failure #2
On Thu, 31 Jul 2008 17:43:01 +0530, "Manoj" <manojjangid@nath.com> wrote:
Hi I am getting this error
Run-Time Check Failure #2 - Stack around the variable 'tokenPrev' was
corrupted.
I am also paste the my code can anyone tell me how can I fix this problem ?
//---------------------------------------------------------------------------------------------
BOOL CnewAppDlg::OnInitDialog()
{
HANDLE hToken = NULL;
DWORD hTokenLength = 0;
TOKEN_PRIVILEGES tokenPrev = {0};
if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken))
{
if(!GetTokenInformation(hToken,TokenPrivileges,NULL,0,&hTokenLength))
Above, you're asking the function to return the required size of the
TOKEN_PRIVILEGES structure in bytes.
{
GetTokenInformation(hToken,TokenPrivileges,(LPVOID)&tokenPrev,hTokenLength,&hTokenLength);
Here, you're passing the returned size with a pointer to the original
tokenPrev, which is incorrect if the Privileges array has any size but 1.
You need to dynamically allocate a buffer hTokenLength bytes long and cast
it to TOKEN_PRIVILEGES, e.g.
std::vector<char> v(hTokenLength);
GetTokenInformation(hToken, TokenPrivileges, &v[0], hTokenLength,
&hTokenLength);
TOKEN_PRIVILEGES* privs = reinterpret_cast<TOKEN_PRIVILEGES*>(&v[0]);
Now use privs instead of tokenPrev below.
for(int nCount = 0; nCount < tokenPrev.PrivilegeCount;
nCount++)
{
TCHAR szPath[_MAX_PATH] = _T("");
DWORD len = _MAX_PATH;
LookupPrivilegeName(NULL,&tokenPrev.Privileges[nCount].Luid,szPath,&len);
TRACE(szPath);
}
}
}
CloseHandle(hToken);
}
return TRUE; // return TRUE unless you set the focus to a control
}
--
Doug Harrison
Visual C++ MVP