EDN Admin
Well-known member
Hello
I have the following code :
<pre class="prettyprint void showError(HRESULT hr)
{
if (FAILED(hr))
{
wprintf(L"An error occurred.n HRESULT: %xn",hr);
if (HRESULT_FACILITY(hr)==FACILITY_WIN32)
{
DWORD dwLastError;
WCHAR szErrorBuf[MAX_PATH];
WCHAR szNameBuf[MAX_PATH];
//Get extended error value.
HRESULT hr_return =S_OK;
hr_return = ADsGetLastError( &dwLastError,
szErrorBuf,
MAX_PATH-1,
szNameBuf,
MAX_PATH-1);
if (SUCCEEDED(hr_return))
{
wprintf(L"Error Code: %dn Error Text: %wsn Provider: %wsn", dwLastError, szErrorBuf, szNameBuf);
}
}
}
}
void removeChar(char del, char* str)
{
char *dst, c = del;
dst = str;
for (int i = 0; str != 0; i++ )
{
if ( str != c )
*dst++ = str;
}
*dst = 0;
}
int checkCurrentUserWithLDAP()
{
TCHAR buff[100];
char ldap[512] = "LDAP://myDomain/<guid=";
char ldapTail[] = ";
unsigned long buffSize = 1024;
IADs *pADs;
BSTR bstr;
GetUserNameEx(NameUniqueId, buff, &buffSize);
removeChar({,buff);
removeChar(},buff);
strcat(ldap,buff);
strcat(ldap,ldapTail);
HRESULT hr = CoInitialize(NULL);
if (hr != S_OK)
{
showError(hr);
return 0;
}
wchar_t wtext[1024];
mbstowcs(wtext, ldap, strlen(ldap));
LPWSTR ptr = wtext;
// hr = ADsGetObject(L"LDAP://myDomain/<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>", IID_IADs, (void**)&pADs);
hr = ADsGetObject(ptr, IID_IADs, (void**)&pADs);
if (hr !=S_OK)
{
showError(hr);
return 0;
}
if( S_OK == pADs->get_Name(&bstr)) {
printf("Object Name: %Sn",bstr);
}
if( S_OK == pADs->get_ADsPath(&bstr)) {
printf("AD path: %Sn",bstr);
}
// Clean-up
if ( pADs )
{
pADs->Release();
}
CoUninitialize();
return 1;
}[/code]
I get the active users GUID using GetUserNameEx() and than create a ldap string and use it in the ADsGetObject(). my problem is that I get the following error from it
<pre class="prettyprint An error occurred.
HRESULT: 80072032
Error Code: 8335
Error Text: 0000208F: NameErr: DSID-031001BA, problem 2006 (BAD_NAME), data 835
0, best match of:
<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>
Provider: LDAP Provider[/code]
which
<table border="1" style="color:#cccccc; font-family:Arial,Tahoma,Helvetica,FreeSans,sans-serif; font-size:13px; background-color:#333333
<tbody>
<tr>
<td>0x80072032</td>
<td>An invalid dn syntax has been specified.</td>
<td>ERROR_DS_INVALID_DN_SYNTAX</td>
</tr>
</tbody>
</table>
now if I send the following code
<pre class="prettyprint hr = ADsGetObject(L"LDAP://myDomain/<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>", IID_IADs, (void**)&pADs);[/code]
I get the correct result, which is the users info. So my question is what am I doing wrong when trying to create the ptr pointer in
<pre class="prettyprint hr = ADsGetObject(ptr, IID_IADs, (void**)&pADs); [/code]
thanks,
ehsan
<br/>
View the full article
I have the following code :
<pre class="prettyprint void showError(HRESULT hr)
{
if (FAILED(hr))
{
wprintf(L"An error occurred.n HRESULT: %xn",hr);
if (HRESULT_FACILITY(hr)==FACILITY_WIN32)
{
DWORD dwLastError;
WCHAR szErrorBuf[MAX_PATH];
WCHAR szNameBuf[MAX_PATH];
//Get extended error value.
HRESULT hr_return =S_OK;
hr_return = ADsGetLastError( &dwLastError,
szErrorBuf,
MAX_PATH-1,
szNameBuf,
MAX_PATH-1);
if (SUCCEEDED(hr_return))
{
wprintf(L"Error Code: %dn Error Text: %wsn Provider: %wsn", dwLastError, szErrorBuf, szNameBuf);
}
}
}
}
void removeChar(char del, char* str)
{
char *dst, c = del;
dst = str;
for (int i = 0; str != 0; i++ )
{
if ( str != c )
*dst++ = str;
}
*dst = 0;
}
int checkCurrentUserWithLDAP()
{
TCHAR buff[100];
char ldap[512] = "LDAP://myDomain/<guid=";
char ldapTail[] = ";
unsigned long buffSize = 1024;
IADs *pADs;
BSTR bstr;
GetUserNameEx(NameUniqueId, buff, &buffSize);
removeChar({,buff);
removeChar(},buff);
strcat(ldap,buff);
strcat(ldap,ldapTail);
HRESULT hr = CoInitialize(NULL);
if (hr != S_OK)
{
showError(hr);
return 0;
}
wchar_t wtext[1024];
mbstowcs(wtext, ldap, strlen(ldap));
LPWSTR ptr = wtext;
// hr = ADsGetObject(L"LDAP://myDomain/<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>", IID_IADs, (void**)&pADs);
hr = ADsGetObject(ptr, IID_IADs, (void**)&pADs);
if (hr !=S_OK)
{
showError(hr);
return 0;
}
if( S_OK == pADs->get_Name(&bstr)) {
printf("Object Name: %Sn",bstr);
}
if( S_OK == pADs->get_ADsPath(&bstr)) {
printf("AD path: %Sn",bstr);
}
// Clean-up
if ( pADs )
{
pADs->Release();
}
CoUninitialize();
return 1;
}[/code]
I get the active users GUID using GetUserNameEx() and than create a ldap string and use it in the ADsGetObject(). my problem is that I get the following error from it
<pre class="prettyprint An error occurred.
HRESULT: 80072032
Error Code: 8335
Error Text: 0000208F: NameErr: DSID-031001BA, problem 2006 (BAD_NAME), data 835
0, best match of:
<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>
Provider: LDAP Provider[/code]
which
<table border="1" style="color:#cccccc; font-family:Arial,Tahoma,Helvetica,FreeSans,sans-serif; font-size:13px; background-color:#333333
<tbody>
<tr>
<td>0x80072032</td>
<td>An invalid dn syntax has been specified.</td>
<td>ERROR_DS_INVALID_DN_SYNTAX</td>
</tr>
</tbody>
</table>
now if I send the following code
<pre class="prettyprint hr = ADsGetObject(L"LDAP://myDomain/<guid=a6788430-a47a-41e7-bc69-a3ad76824cc3>", IID_IADs, (void**)&pADs);[/code]
I get the correct result, which is the users info. So my question is what am I doing wrong when trying to create the ptr pointer in
<pre class="prettyprint hr = ADsGetObject(ptr, IID_IADs, (void**)&pADs); [/code]
thanks,
ehsan
<br/>
View the full article