ADsOpenObject bind unsuccessful - c++

I am trying to connect to an AD server from a computer that is outside the domain using ADSI however the bind is unsuccessful. Using Visual c++ 2010 express.
Here is the code snippet:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <wchar.h>
#include <objbase.h>
#include <activeds.h>
#include <AdsHlp.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
IADs *pObject;
HRESULT hr;
hr = ADsOpenObject(L"LDAP://aswathy-server3/cn=users,dc=aswathy,dc=local",
L"administrator",
L"password",
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);
if(SUCCEEDED(hr))
{
cout<<"Success";
pObject->Release();
}
else
cout<<"Unsuccessful";
getch();
return 0;
}
I have included adsiid,lib and activeds.lib under project properties -> linker -> input -> additional dependencies.
Does anyone know why bind is not successful?

hr = ADsOpenObject(L"WinNT://aswathy.local/users",
L"administrator",
L"password",
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pObject);

Related

error C2079 uses undefined class - WinRT/UWP?

So here is my code:
// wrl-consume-component.cpp
// compile with: runtimeobject.lib
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <stdio.h>
#include <windows.networking.vpn.h>
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Networking::Vpn;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
// Prints an error string for the provided source code line and HRESULT
// value and returns the HRESULT value as an int.
int PrintError(unsigned int line, HRESULT hr)
{
wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
return hr;
}
int wmain()
{
// Initialize the Windows Runtime.
RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
{
return PrintError(__LINE__, initialize);
}
// Get the activation factory for the IUriRuntimeClass interface.
ComPtr<VpnManagementAgent> uriFactory;
HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Networking_Vpn_VpnManagementAgent).Get(), &uriFactory);
if (FAILED(hr))
{
return PrintError(__LINE__, hr);
}
// VpnManagementAgent vpn;
auto profiles = uriFactory->GetProfilesAsync().get();
wprintf(L"Found %d profiles\n", profiles.Size());
for (auto vp : profiles)
{
wprintf(L"Found profile %s\n", vp.ProfileName().c_str());
}}
/*
Output:
Domain name: microsoft.com
*/
It's basically copy paste from msdn and the answer to that question.
I'm compiling like this:
cl /diagnostics:column wrl-consume-component.cpp /link RuntimeObject.lib windows.networking.lib
Obviously I'm missing something huge here.
The error message is:
wrl-consume-component.cpp(40,27): error C2027: use of undefined type 'ABI::Windows::Networking::Vpn::VpnManagementAgent'

How to call GetAltMonthNames to fill a safe array of foreign locale month strings?

I saw this function and was wondering how to call this. I might like to write a component and export this function to a COM client so I wanted to fill a safearray of strings (other Automation types are fine). So I wanted to leverage an ATL smart class. This is what I have so far, a console application.
#include "pch.h"
#include <iostream>
// in pch.h ...
//#include "windows.h"
//#include "comutil.h"
//#include "atlbase.h"
//#include <comdef.h>
//#include "atlsafe.h"
int main()
{
LCID germany(7);
LPOLESTR *rgp;
HRESULT hr;
hr=::GetAltMonthNames(germany, &rgp); // can't see results
if (hr != S_OK) return hr;
CComSafeArray<BSTR> months;
hr = ::GetAltMonthNames(germany,(LPOLESTR**) &months); //forced compile but no joy
if (hr != S_OK) return hr;
std::cout << "Hello World!\n";
}
Your first code is ok but there's no alternate names for German language defined. Try Polish:
LPOLESTR* rgp;
if (SUCCEEDED(GetAltMonthNames(1045, &rgp)))
{
int i = 0;
while (rgp[i])
{
wprintf(L"%s\n", rgp[i++]);
}
}
Documentation says:
Useful for Hijri, Polish and Russian alternate month names.

Unable to use WaitForCompletion() in #include <dshow.h>

I am using Qt patch in visual studio 2008. I am trying to run a video using #include <dshow.h>. I can run video successfully, but on using WaitForCompletion() my video gets hang. here is my code:-
MediaPlayer = new Media(ui.stackedWidget->currentWidget());
connect(ui.stackedWidget->currentWidget(), SIGNAL(videograph()), MediaPlayer,SLOT(HandleGraphEvent()));
MediaPlayer->pMediaControl->Run();
long evCode;
MediaPlayer->g_pEvent1->WaitForCompletion(INFINITE,&evCode);
My header file:-
Media.cpp
#include <dshow.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
//#include <atlbase.h>
#include "Media.h"
#include <qtconcurrentrun.h>
Media::Media(QWidget *parent)
{
HRESULT hr;
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
hr = pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pWc);
hr = pWc->SetNumberOfStreams(2);
hr = pVmr->QueryInterface(IID_IVMRMixerBitmap,(void **)&pBitMAp);
if(SUCCEEDED(hr))
{
hr = pWc->SetRenderingMode(VMRMode_Windowless);
hr = pWc->SetRenderingPrefs( RenderPrefs_ForceOffscreen| RenderPrefs_AllowOffscreen );
pWc->Release();
}
if(SUCCEEDED(hr))
{
hr = pVmr->QueryInterface(IID_IVMRWindowlessControl, (void**)&pWl);
if(SUCCEEDED(hr))
hr = pWl->SetVideoClippingWindow(parent->winId());
}
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGraph);
pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
pGraph->QueryInterface(IID_IMediaEventEx, (void **)&g_pEvent);
pGraph->QueryInterface(IID_IBasicAudio,(void **)&pAudio);
pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pMediaSeeking );
pGraph->QueryInterface( IID_IMediaPosition, (void **) &pMediaPosition);
hr = pGraph->AddFilter(pVmr, L"Video Mixing Renderer");
g_pEvent->SetNotifyWindow((OAHWND)parent->winId(), WM_GRAPHNOTIFY, 0);
RECT grc;
GetWindowRect(parent->winId(), &grc);
pGraph->RenderFile(L"/FlashDisk/test.mp4", NULL);
long lWidth, lHeight;
hr = pWl->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL);
if (SUCCEEDED(hr))
{
SetRect(&g_rcSRc, 0, 0, lWidth, lHeight);
GetWindowRect(parent->winId(), &g_rcDest);
SetRect(&g_rcDest, 0, 0, g_rcDest.right, g_rcDest.bottom);
}
video_rendered = 1;
pWl->SetVideoPosition(&g_rcSRc, &g_rcDest);
}
Media::~Media()
{
CleanUp();
}
void Media::HandleGraphEvent()
{
if (g_pEvent == NULL)
return;
long evCode;
LONG_PTR param1, param2;
while (SUCCEEDED(g_pEvent->GetEvent(&evCode, &param1, &param2, 0)))
{
g_pEvent->FreeEventParams(evCode, param1, param2);
switch (evCode)
{
case EC_STATE_CHANGE: //ADDED for state change from pause to play to indiacte video paused.
//SetEvent(sync_event);
return;
case EC_COMPLETE: // Fall through.
CleanUp();
return;
case EC_USERABORT: // Fall through.
case EC_ERRORABORT:
CleanUp();
//media.play_next_file();
return;
}
}
}
/*#######################################################################################
CleanUp
#######################################################################################*/
void Media::CleanUp(void)
{
video_rendered = 0;
g_pEvent->SetNotifyWindow(NULL, 0, 0);
g_pEvent->Release();
g_pEvent = NULL;
pMediaControl->Release();
pAudio->Release();
pGraph->Release();
}
Media.h
#ifndef MEDIA_H
#define MEDIA_H
//#define max(a,b) (((a) > (b)) ? (a) : (b))
//#define min(a,b) (((a) < (b)) ? (a) : (b))
#include <windef.h>
#include <QObject>
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QTimer>
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#include <Phonon>
#include <dshow.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
//#include <atlbase.h>
#include <qtconcurrentrun.h>
#pragma comment (lib, "strmiids.lib")
#define WM_GRAPHNOTIFY WM_APP + 1
#define WM_AUDIOGRAPHNOTIFY WM_APP + 2
class Media: public QObject
{
Q_OBJECT
public:
Media(QWidget *parent);
~Media();
IMediaControl *pMediaControl;
IMediaEventEx *g_pEvent;
IMediaEvent *g_pEvent1;
IVideoWindow *pVidWin;
IVMRMixerBitmap *pBitMAp;
IVMRMixerBitmap *pBitMAp1;
IMediaSeeking *pMediaSeeking;
IMediaPosition *pMediaPosition;
IVMRFilterConfig *pWc;
IBaseFilter *pVmr;
IVMRWindowlessControl *pWl;
RECT g_rcSRc;
RECT g_rcDest;
IBasicAudio *pAudio;
IVMRMixerControl *pVmc;
IGraphBuilder *pGraph;
DWORD width;
unsigned char video_rendered;
void CleanUp(void);
public slots:
void HandleGraphEvent(void);
};
#endif
Please suggest me what i am missing here.
On a COM STA thread you are responsible to dispatch window messages and WaitForCompletion is a blocking call without a promise to implement message loop.
See DShow Sample Code for playing video does not play the video
An easy way to find out if #2 is the problem is placing MessageBox call between Run and WaitForCompletion. MessageBox dispatches messages for you and as long as you keep the box open, video plays as well (or start playing well and keeps playing even after you close the box). Proper solution is to wait and displach messages in the same time (WaitDispatchingMessages, this SO question or similar).
This applies to your case as well, change to finite timeout, dispatch messages and get back to another wait attempt.

UrlToDownloadFile function not downloading

I am using UrlToDownloadFile function, but it doesn't download the file. No error shown in compiler (using VStudio 2012)
Here is the code:
#include <Windows.h>
#include "urlmon.h"
#pragma lib "urlmon.lib"
using namespace std;
void dwFile();
int _tmain(int argc, _TCHAR* argv[])
{
dwFile ();
return 0;
}
void dwFile ()
{
LPCSTR url = ("http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf");
LPCSTR fPath = ("C:\\Users\\Andyy\\Desktop\\test\\n3337.pdf");
HRESULT URLDownloadToFile ((NULL, url, fPath, 0, NULL));
}
Your code is not doing any error handling, and your string handling is wrong. Use this instead:
#include <Windows.h>
#include "urlmon.h"
#pragma lib "urlmon.lib"
using namespace std;
void dwFile();
int _tmain(int argc, _TCHAR* argv[])
{
dwFile ();
return 0;
}
void dwFile ()
{
LPCTSTR url = TEXT("http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf");
LPCTSTR fPath = TEXT("C:\\Users\\Andyy\\Desktop\\test\\n3337.pdf");
HRESULT hr = URLDownloadToFile (NULL, url, fPath, 0, NULL);
if (FAILED(hr))
{
// do something ...
}
/* or more preffered:
LPCWSTR url = L"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf";
LPCWSTR fPath = L"C:\\Users\\Andyy\\Desktop\\test\\n3337.pdf");
HRESULT hr = URLDownloadToFileW (NULL, url, fPath, 0, NULL);
if (FAILED(hr))
{
// do something ...
}
*/
}
Do note the following comment in the documentation:
URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.

IAudioEndpointVolume has no member named GetMasterVolumeLevelScalar

Consider this program:
#include <stdio.h>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <math.h>
int main() {
IAudioEndpointVolume *wh;
IMMDevice *ya;
IMMDeviceEnumerator *xr;
CoInitialize(0);
CoCreateInstance(__uuidof(MMDeviceEnumerator), 0, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (void**)&xr);
xr->GetDefaultAudioEndpoint(eRender, eConsole, &ya);
ya->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, 0, (void**)&wh);
float zu;
wh->GetMasterVolumeLevelScalar(&zu);
printf("%d\n", (int) round(100 * zu));
}
I can compile it as C++ with no issue:
x86_64-w64-mingw32-g++ vol.cpp -lole32
However if I try to compile it as C:
x86_64-w64-mingw32-gcc vol.c -lole32
I get errors such as:
error: ‘IAudioEndpointVolume’ has no member named ‘GetMasterVolumeLevelScalar’
This program does not seem to be particularly “C++”, so what is causing the
problem? Also, can I change something so that it compiles as C?
This seems to do it:
#include <stdio.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <math.h>
int main() {
IAudioEndpointVolume *wh;
IMMDevice *ya;
IMMDeviceEnumerator *xr;
CoInitialize(0);
CoCreateInstance(&CLSID_MMDeviceEnumerator, 0, CLSCTX_INPROC_SERVER,
&IID_IMMDeviceEnumerator, (void**)&xr);
xr->lpVtbl->GetDefaultAudioEndpoint(xr, eRender, eConsole, &ya);
ya->lpVtbl->Activate(ya, &IID_IAudioEndpointVolume, CLSCTX_ALL,
0, (void**)&wh);
float zu;
wh->lpVtbl->GetMasterVolumeLevelScalar(wh, &zu);
printf("%d\n", (int) round(100 * zu));
}
Changes:
#include <initguid.h>
&CLSID_MMDeviceEnumerator instead of __uuidof(MMDeviceEnumerator)
lpVtbl->GetDefaultAudioEndpoint instead of GetDefaultAudioEndpoint
Source