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'
Related
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.
I am getting exception while creating MSMQqueue in Visual Studio 2017, find the code and exception details below:
Exception details :
app.exe!`queue::CreateQueue'::`1'::catch$3() Line 56 C++ Symbols loaded.
Please suggest the queue for implementing serial port read and write.
queue.cpp
#include "stdafx.h"
#include "MSMQ_Queue.h"
#import "mqoa.dll"
using namespace MSMQ;
queue::queue() {}
queue::~queue() {}
HRESULT queue::CreateQueue(WCHAR *wszPathName)
{
HRESULT hr = S_OK;
if (wszPathName == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
}
try
{
IMSMQQueueInfoPtr pInfo("MSMQ.MSMQQueueInfo");
// Set the queue's path name and label.
pInfo->PathName = wszPathName;
pInfo->Label = "TestQueue";
// Create the queue.
**pInfo->Create();**//Hitting exception at this point
WCHAR wszMessage[1024] = { 0 };
}
catch (const _com_error& comerr)
{
hr = comerr.Error();
WCHAR wszMessage[2048] = { 0 };
}
return hr;
}
main.cpp
#include "stdafx.h"
#include"queue.h"
#include <stdio.h> // for printf
int main()
{
wchar_t name[] = L".\\vniqueue";
queue msmqueue;
//CoInitialize(0);
OleInitialize(NULL);
HRESULT returnValue = msmqueue.CreateQueue(name);
getchar();
return 0;
}
Exception create queue
Finally I found solutions for this..
I have given public path instead of private pat. Changed “.//queuename” to .//private$//queuename”.
According to this MSDN doc, we may pass NULL for the path argument:
path [in]
The name of the task. If this value is NULL, the task will be registered in the root task folder and the task name will be a GUID value created by the Task Scheduler service.
I have a code that use this behavior. The code works fine in Win7 and 8.1, but not in my Win10 box (ver 1709 64-bit, build 16299). In Win10, it will return 0x80070005 aka "Access Denied" when path is NULL. If I specify a name like "Foobar", it will work fine.
Test code:
// Link comsuppw.lib and taskschd.lib.
#include <stdio.h>
#include <windows.h>
#include <atlbase.h>
#include <atlstr.h>
#include <shlobj.h>
#include <taskschd.h>
#include <comutil.h>
class AutoHR {
HRESULT hr;
public:
void operator=(HRESULT hr)
{
this->hr = hr;
if (FAILED(hr)) {throw *this;}
}
HRESULT GetHR() const { return hr; }
};
static void TestTaskSched()
{
AutoHR hr;
CComPtr<ITaskService> taskSvc;
CComPtr<ITaskFolder> taskFol;
CComPtr<ITaskDefinition> taskDef;
CComPtr<IActionCollection> taskAC;
CComPtr<IAction> taskAction;
CComPtr<IExecAction> taskEA;
CComPtr<IRegisteredTask> registeredTask;
try {
hr = taskSvc.CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_ALL);
hr = taskSvc->Connect(CComVariant(),CComVariant(),CComVariant(),CComVariant());
hr = taskSvc->GetFolder(_bstr_t(L""), &taskFol);
hr = taskSvc->NewTask(0, &taskDef);
hr = taskDef->get_Actions(&taskAC);
hr = taskAC->Create(TASK_ACTION_EXEC, &taskAction);
hr = taskAction.QueryInterface<IExecAction>(&taskEA);
hr = taskEA->put_Path(_bstr_t(L"C:\\Windows\\System32\\cmd.exe"));
hr = taskEA->put_Arguments(_bstr_t(L"/k echo Testing"));
// Note that NULL is passed as the first argument.
hr = taskFol->RegisterTaskDefinition(nullptr, taskDef,
TASK_CREATE_OR_UPDATE, CComVariant(), CComVariant(),
TASK_LOGON_NONE, CComVariant(), ®isteredTask);
MessageBoxW(nullptr, L"Succeeded!", L"OK", MB_ICONINFORMATION);
}
catch (AutoHR const &autohr) {
WCHAR buf[99] = {0};
wsprintfW(buf, L"HRESULT error 0x%.8X\n", autohr.GetHR());
MessageBoxW(nullptr, buf, nullptr, MB_ICONERROR);
}
}
int main()
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (SUCCEEDED(hr))
{
TestTaskSched();
CoUninitialize();
}
return 0;
}
Test result:
Questions:
1) Is there a behavior change between Win10 and older Windows? I suspect there is, but I cannot find any doc that mentions it.
2) Any good alternative for this behavior? I hope I don't have to generate GUID by myself for temporary task creation.
I'm following an example with a basic idea of getting a web page async. The code is here:
#include <atlbase.h>
#include <msxml6.h>
#include "comutil.h"
int main(int argc, char* argv)
{
HRESULT hr;
CComPtr<IXMLHTTPRequest> request;
hr = request.CoCreateInstance(CLSID_XMLHTTP60);
hr = request->open(
_bstr_t("GET"),
_bstr_t("https://www.google.com/images/srpr/logo11w.png"),
_variant_t(VARIANT_FALSE),
_variant_t(),
_variant_t());
hr = request->send(_variant_t());
// get status - 200 if succuss
long status;
hr = request->get_status(&status);
}
When I try to compile this I get the error:
error C2504: 'IXMLHTTPRequest' : base class undefined
Is anyone able to see what's wrong with this?
Thanks.
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);