I'm creating a C++/WinRT console apps and am getting an error creating PropertyChangedEventArgs while other types are activated correctly. Here's the code:
#include "pch.h"
#include <winrt/Windows.UI.Xaml.Data.h>
int main()
{
winrt::init_apartment();
winrt::Windows::UI::Xaml::Data::PropertyChangedEventArgs args(L"Title");
}
The exception is:
Unhandled exception at 0x00007FFCDE9BA839 in ConsoleApplication1.exe: Microsoft C++ exception: winrt::hresult_wrong_thread at memory location 0x0000002CF310EF48. occurred
This is using v2.0.190730.2 of Microsoft.Windows.CppWinRT and targeting Windows 10.0.18362.0 running on 10.0.18362.295.
It seems to be a thread problem.The call to winrt::init_apartment initializes the thread in the Windows Runtime, by default, in a multithreaded apartment. So when you want to use the content of UI,you need to return to UI thread like below.
IAsyncAction MyMethod()
{
winrt::apartment_context ui_thread;
co_await ui_thread;
winrt::Windows::UI::Xaml::Data::PropertyChangedEventArgs args{ L"Title" };
}
int main()
{
winrt::init_apartment();
MyMethod();
}
Related
I am currently work on a C++/WinRt which connects(Sender) to one of the available devices(Client) around a specific region using WifiDirect. When the device wants to connect, it sends a connection request to the sender. The sender needs to detect the connection request sent by the client and connect to the client. For this I need to add an event - (On Connection requested). As soon as I add it should execute the code of OnConnectionRequested.
#include "pch.h"
#include <winrt/Windows.Foundation.Collections.h>
#include "winrt/Windows.Devices.WiFiDirect.h"
#pragma once
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::WiFiDirect;
using namespace Windows::Storage::Streams;
using namespace winrt::Windows::Devices::WiFiDirect;
using namespace winrt::Windows::Devices::Enumeration;
enum class NotifyType
{
StatusMessage,
ErrorMessage
};
enum class CallbackContext
{
Any,
Same
};
class st
{
public:
void OnConnectionRequested(WiFiDirectConnectionListener sender,
WiFiDirectConnectionRequestedEventArgsconnection EventArgs)
{
WiFiDirectConnectionRequest connectionRequest = connectionEventArgs.GetConnectionRequest();
printf("Connection request received from ", connectionRequest.DeviceInformation().Name(), "Connection Request");
printf("Connecting to ", connectionRequest.DeviceInformation().Name(), NotifyType::StatusMessage);
}
void start()
{
Windows::Devices::WiFiDirect::WiFiDirectAdvertisementPublisher _publisher;
Windows::Devices::WiFiDirect::WiFiDirectConnectionListener _listener;
winrt::event_token _connectionRequestedToken;
try
{
_connectionRequestedToken = _listener.ConnectionRequested({this, &st::OnConnectionRequested});
_publisher.Start();
printf("Advertisement started, waiting for StatusChangedcallback...", NotifyType::StatusMessage);
}
catch (...)
{
printf("Error starting Advertisement: ", NotifyType::ErrorMessage);
}
getchar();
}
};
int main()
{
st s;
s.start();
}
Is this the right way to add an event in C++/WinRt :
_connectionRequestedToken = _listener.ConnectionRequested({this, &st::OnConnectionRequested});
The errors are :
LNK1120 1 unresolved externals - error in Winrt.exe file
LNK2019
unresolved external symbol "public: struct winrt::hstring __thiscall
winrt::impl::consume_Windows_Devices_Enumeration_IDeviceInformation::Name(void)const
"
(?Name#?$consume_Windows_Devices_Enumeration_IDeviceInformation#UIDeviceInformation#Enumeration#Devices#Windows#winrt###impl#winrt##QBE?AUhstring#3#XZ)
referenced in function "public: void __thiscall
st::OnConnectionRequested(struct
winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionListener,struct
winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionRequestedEventArgs)"
(?OnConnectionRequested#st##QAEXUWiFiDirectConnectionListener#WiFiDirect#Devices#Windows#winrt##UWiFiDirectConnectionRequestedEventArgs#3456##Z) - error in Program.obj 1
What changes should I make at this line to clear the error? What does the error actually mean ? Or is there any other way to add an event in C++/WinRt project?
You are missing an #include directive:
#include <winrt/Windows.Devices.Enumeration.h>
See Why is the linker giving me a "LNK2019: Unresolved external symbol" error?:
If the unresolved symbol is an API from the Windows namespace headers for the C++/WinRT projection (in the winrt namespace), then the API is forward-declared in a header that you've included, but its definition is in a header that you haven't yet included. Include the header named for the API's namespace, and rebuild. For more info, see C++/WinRT projection headers.
For general instructions on how to handle events see Handle events by using delegates in C++/WinRT.
Capturing this (raw pointer) into the delegate might be dangerous, too. This breaks the link between object lifetime and visibility, and puts the burden of lifetime management on you. See Safely accessing the this pointer with an event-handling delegate for safer, more manageable alternatives.
Actually i am working on c++/CLI dll which is using C# dll and c++/cli dll will use from native c.
c++/cli code is like:-
public ref class Class1
{
// TODO: Add your methods for this class here.
public:
static Managed_EMV_DLL::Managed_EMV ^obj = gcnew Managed_EMV(); // object of c# class
bool INIT_READER(unsigned int *);
bool READ_KEY(unsigned int *ERROR_CODE,unsigned char *RETURN_ARRAY, unsigned int *Array_LENGTH);
};
-i want to handle the exception in c++/CLI code,
-handle exception when c# dll not found.
how i can make it.
What is the exact problem you're facing .... See the below example format.
try
{
}
catch(FormatException ^) // display an appropriate message
{
Console::WriteLine(L"You must enter a valid number "
L"and no other character!");
}
Just sort out what exception could be thrown from C# dll and then put appropriate handles in your C++/CLI code.
I want to handle any kind of exception not only FormatException or TypeInitializationException.
Here i write code like:-
try
{
}
catch (Exception^ ex)
{
Console::WriteLine("Error in C++/CLI INIT function: {0}", ex->ToString());
}
catch (...)
{
Console::WriteLine("Error in INIT");
}
and it works good.... i hope it will catch any kind of exception...
In the code below I would like to replace Windows WinSock error WSAEINTR=10004 with a generic boost system error code, but how do I map the code I found in the debugger, with the generic enums?
timeout_.expires_from_now(posix_time::seconds(15));
timeout_.async_wait( boost::bind(&cancel_socket_fn,this,_1) );
asio::streambuf rd_buf;
unsigned length = read_until( socket_, rd_buf,delimiter_string, error );
timeout_.cancel();
if(error)
{
// how do I make this portable?
if(error.value()==WSAEINTR) throw request_timeout_exception()
else throw my_asio_exception(error,"Unable to read header");
}
...
cancel_socket_fn(system::error_code e) { socket_.cancel(); }
if (error == boost::asio::error::interrupted)...
And I think here is a design error because if this code is called from the thread where io_service::run() (or similar) is called then cancel_socket_fn() will not be called until read_until() finishes. Or if they are in different threads then here are synchronization problems because timer methods are not thread-safe.
I am writing an application to interface with COM components and I have run into a problem when working with the Excel.Application component while running my application in the Visual Studio 10 IDE. I am getting a fatal Out of Memory error. Everything runs fine if I just run the EXE, but this severely limits my debugging capabilities. All other COM components I have accessed this way work fine, including both home-grown and commercially available components.
Here is a console app that demonstrates this crash. I have removed all error handling for simplicity's sake. Putting a try/catch block around the offending code does not help. This project requires a reference to the CustomMarshalers.dll.
class Program
{
static void Main(string[] args)
{
InstantiateCOMComponent("Excel.Application");
}
private static void InstantiateCOMComponent(string name)
{
Type typeInfo = Type.GetTypeFromProgID(name);
object instance = Activator.CreateInstance(typeInfo);
IDispatch dispatch = instance as IDispatch;
// NOTE: THIS CALL FAILS WITH Excel.Application in the IDE
// but succeeds at run-time!! (Out of Memory fatal error)
Type comTypeInfo;
dispatch.GetTypeInfo(0, 0, out comTypeInfo);
}
}
[ComImport,
Guid("00020400-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch
{
void Reserved();
[PreserveSig]
int GetTypeInfo(uint nInfo, int lcid,
[MarshalAs(
UnmanagedType.CustomMarshaler,
MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))]
out System.Type typeInfo);
}
I am thinking the problem is simply due to Excel's size.
Im trying to expose a local server that is written in C# to unmanaged code to allow interop! The managed code looks like that:
[Guid("A0D470AF-0618-40E9-8297-8C63BAF3F1C3")]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyLocalInterface
{
void LogToServer(string message);
}
[Guid("9E9E5403-7993-49ED-BAFA-FD9A63A837E3")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class MyLocalClass : IMyLocalInterface
{
public MyLocalClass()
{
Console.WriteLine("Object created!");
}
public void LogToServer(string message)
{
Console.WriteLine("Log > " + message);
}
}
class Program
{
[MTAThread]
static void Main(string[] args)
{
var srv = new RegistrationServices();
var cookie = srv.RegisterTypeForComClients(typeof(MyLocalClass), RegistrationClassContext.LocalServer | RegistrationClassContext.RemoteServer, RegistrationConnectionType.MultipleUse);
Console.ReadLine();
srv.UnregisterTypeForComClients(cookie);
}
}
And my unmanaged code does the following:
#import "ManagedLocServer.tlb" no_namespace raw_interfaces_only
int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
{
IMyLocalInterfacePtr ptr;
ptr.CreateInstance(__uuidof(MyLocalClass));
ptr->LogToServer(L"Initializing...");
}
CoUninitialize();
return 0;
}
After debugging ive seen that CoCreateInstance works without any problems, that means "Object created" is printed into the managed servers console. But then QueryInterface on that object fails with E_NOINTERFACE. Im a bit confused why this happens. Is it a problem with registration (i only have a LocalServer32 entry for my CLSID)? Is it a problem within my managed code? Would be nice if someone could give me a hint :)
Greetings
Joe
You are using out-of-process COM. That requires marshaling support, to make a method call the arguments of the method need to be serialized. That's normally done by building the proxy/stub DLL from the code generated by midl.exe from the .idl file. Or by using the standard marshaller which works with the type library. Both require registry entries in HKCR\Interface
You get the E_NOINTERFACE because COM cannot find a marshaller. This is trivial to solve if you have an .idl file but you don't, the server is implemented in .NET. No idea how to solve this, I never tried to make this work. A remote possibility is that the CLR interop layer provides marshaling support. But you'd surely at least have to use ComInterfaceType.InterfaceIsDual. This is just a guess. If I tried to make this work, I'd start from an .idl first.