Problem with changing Win32 Runtime via RoInitialize(RO_INIT_MULTITHREADED)

Hi,

I need to change Windows Runtime to multithread mode for several windows async functions. It appears that in 5.0.2 Hotfix release, an issue with ::RoInitialize(RO_INIT_MULTITHREADED) has been resolved - 5.0.2 Hotfix Released Announcement; however, I am unable to find any information on the UE public issues tracking site. I ran a test (in an UActorComponent) as follows:

int32_t cotype, qualifier;

get_apartment_result = WINRT_IMPL_CoGetApartmentType(&cotype, &qualifier);

if (cotype == APTTYPE_STA || cotype == APTTYPE_MAINSTA) {
    UE_LOG(LogTemp, Warning, TEXT("Single-threaded apartment detected, uninitializing."));
    RoUninitialize();
}

winrt::hresult result = RoInitialize(RO_INIT_MULTITHREADED);

if (result == S_OK) {
    UE_LOG(LogTemp, Display, TEXT("RoInitialize(RO_INIT_MULTITHREADED) successfully"));
}
else if (result == S_FALSE) {
    UE_LOG(LogTemp, Display, TEXT("RoInitialize(RO_INIT_MULTITHREADED) failed"));
}
else if (result == RPC_E_CHANGED_MODE) {
    UE_LOG(LogTemp, Display, TEXT("RoInitialize(RO_INIT_MULTITHREADED) returned RPC_E_CHANGED_MODE"));
}
else {
    UE_LOG(LogTemp, Warning, TEXT("RoInitialize: result= %d"), (uint32_t)result);
}

int32_t cotypeAfter, qualifierAfter;

get_apartment_result = WINRT_IMPL_CoGetApartmentType(&cotypeAfter, &qualifierAfter);

if (cotypeAfter == APTTYPE_STA || cotypeAfter == APTTYPE_MAINSTA) {//getting error C2065: 'APTTYPE_STA': undeclared identifier and error C2065: 'APTTYPE_MAINSTA': undeclared identifier
    UE_LOG(LogTemp, Warning, TEXT("Single-threaded apartment detected"));
}
else {
    UE_LOG(LogTemp, Warning, TEXT("Multi-threaded apartment detected"));
}

And the output log is as follows:

LogTemp: Warning: Single-threaded apartment detected, uninitializing.
LogTemp: Display: RoInitialize(RO_INIT_MULTITHREADED) returned RPC_E_CHANGED_MODE
LogTemp: Warning: Single-threaded apartment detected

I also searched for “RoInitialize” in the engine source in VS2022 and couldn’t find any results. Is there another way to change windows runtime threading mode?

I was able to move WinRT async calls to a different UE thread and successfully get a response without the need to use RoInitialize; prior to this, it would hang on certain async calls. UE5Coro has made it much easier to handle mutl-threading in UE so many thanks to the author who created this amazing plugin.