The problem
If you are using ADSI programming interface in your software in C++, you probably do things like :
USES_CONVERSION;
...
hr = pDSSearch->ExecuteSearch(...);
hr = pDSSearch->GetFirstRow(hSearch);
ADS_SEARCH_COLUMN scValue;
while (SUCCEEDED(hr) && hr != S_ADS_NOMORE_ROWS)
{
pDSSearch->GetColumn(hSearch, T2W(_T("objectClass")), &scValue);
...
}
If you are searching under a node that has thousands of children, you will probably experience crashes, caused by stack overflow or memory corruption.
This is explained on a Code Guru article: ATL String: What's wrong with the USES_CONVERSION macros? How to avoid using them?
The problem is that USES_CONVERSION macros use _alloca, which allocate memory on the stack.
For example, on my system, this simple code crashes when m equals 44390:
#include "stdafx.h"
#include <atlbase.h>
int _tmain(int argc, _TCHAR* argv[])
{
USES_CONVERSION;
LPWSTR lpszTmp = NULL;
for (int m=0;m<400000;m++)
{
printf("m: %d\n", m);
lpszTmp = T2W(_T("objectClass"));
}
return 0;
}
So when you use these macros, check that they shall not be executed in a big loop.
The solution
If you want a safe way to convert strings from and to unicode using ATL, ATL 7.0 provides you with conversion classes, CT2W and CW2T.
CT2W pszAttribute(_T("objectClass"));
pDSSearch->GetColumn(hSearch, pszAttribute, &scValue);
This is a safe and simple way. Memory is not allocated on the stack anymore, the CT2W class allocates it for you and frees it when the instance is destroyed.