Tracking Java native code and dll interaction - java-native-interface

I am accessing a DLL file from java code.
But I am getting the below exception.
ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /Default/ws/Services/LegacyCWS: java.lang.UnsatisfiedLinkError: x:\xxx\xxxx\xxx\xxx\jboss\ext_libs\aesdpapi4j.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at aesdpapi.AesDataProtectionAPI.<clinit>(AesDataProtectionAPI.java:144)
I want to verify if there is a memory leak when this exception occurs, and why this exception occurs.

Related

Get "The application called an interface that was marshalled for a different thread" error while getting content from Clipboard

Aim: Get content from the clipboard using C++/WinRT APIs.
Problem:
Since I'm just testing this API, I tried to code a simple console app with the blocking get() method on Clipboard's async text getter. However, I'm getting the "The application called an interface that was marshalled for a different thread" error when debugging. I also tried to init apartments as single threaded apartment (STA), but I guess it's not allowed because another assertion error (!is_sta()) is thrown. Now I'm just wondering why this error is thrown when calling get and how to retrieve the content from clipboard in my console app. (I saw some examples (mostly GUI apps) are using coroutines which I'm not familiar with though.)
Code:
using namespace winrt::Windows::ApplicationModel::DataTransfer;
int main()
{
init_apartment();
hstring text{ Clipboard::GetContent().GetTextAsync().get() };
std::wcout << text.c_str() << std::endl;
}
Error message:
Exception thrown at 0x7659ACC2 (KernelBase.dll) in test-clipboard.exe: WinRT originate error - 0x8001010E : 'The application called an interface that was marshalled for a different thread.'.
'test-clipboard.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcrypt.dll'.
Exception thrown at 0x7659ACC2 in test-clipboard.exe: Microsoft C++ exception: winrt::hresult_wrong_thread at memory location 0x010FF818.
Unhandled exception at 0x7659ACC2 in test-clipboard.exe: Microsoft C++ exception: winrt::hresult_wrong_thread at memory location 0x010FF818.
Sincerely appreciate your help. Thanks.
The WinRT clipboard APIs can only be called from a UI thread with a CoreWindow. See notes In the documentation. You can just use the regular Win32 clipboard APIs in your console app.

How to catch exceptions thrown by the ConnectAsync method of MessageWebSocket?

I'm implementing an UWP application for the HoloLens which streams a point cloud over a MessageWebSocket to a companion PC. If I launch the app on the HoloLens while the server on the companion PC is not yet running, calling the ConnectAsync method of MessageWebSocket triggers an exception (because it can't connect to the server) which causes my app to crash. However, I can't figure out how to catch this exception.
Inspired by the example code shown in the official documentation of MessageWebSocket, this is a small sample which reproduces the issue:
void TryConnectToWebsocket()
{
Windows::Networking::Sockets::MessageWebSocket^ websocket = ref new Windows::Networking::Sockets::MessageWebSocket();
websocket->Control->MessageType = Windows::Networking::Sockets::SocketMessageType::Utf8;
try
{
::OutputDebugString(L"Trying to connect...\n");
auto connectTask = Concurrency::create_task(websocket->ConnectAsync(ref new Windows::Foundation::Uri(L"ws://192.168.0.38:9090")));
connectTask.then([this, websocket]
{
::OutputDebugString(L"Connected successfully!");
websocket->Close(1000, "Application caused the connection to close.");
});
}
catch (...)
{
::OutputDebugString(L"Couldn't connect to websocket!");
}
}
Please note, that the original example code from the docs catches exceptions of type Platform::Exception. I chose to catch exceptions of all types in this code snippet to assert that I don't miss the exception in case it is not a Platform::Exception (or a subtype of it).
If I run this code snippet (without the server being started), I would expect the following console output:
Trying to connect...
Couldn't connect to the websocket!
However, what I get is the following: (Console outputs about loaded and unloaded DLLs have been left out. Some of the messages describing what went wrong were originally in German, so I've translated them.)
Trying to connect...
Exception thrown at 0x76B34592 (KernelBase.dll) in Test.exe: WinRT originate error - 0x80072EFD : 'A connection with the server could not be established.'.
Exception thrown at 0x76B34592 in Test.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0C5AE500. HRESULT:0x80072EFD The message for this error code could not be found.
WinRT information: A connection with the server could not be established.
Stack trace:
>[External Code]
As you can see, the catch block isn't being executed at all. Furthermore, the very short and unprecise stack trace makes it appear as if the exception is thrown somewhere in a background thread where I don't even have a chance of catching it.
I'd really like to handle this exception instead of having the application crash. Is there any way how I can do this?
From the official sample, it doesn't put every operation that might throw within a try…catch block. Instead, it adds a task-based continuation at the end of the chain and handles all errors there. You can try the following code to catch the exception.
create_task(messageWebSocket->ConnectAsync(ref new Windows::Foundation::Uri(L"ws://192.168.0.38:9090")))
.then([this](task<void> previousTask)
{
try
{
previousTask.get();
::OutputDebugString(L"Connected successfully!");
websocket->Close(1000, "Application caused the connection to close.");
}
catch (Exception^ ex)
{
::OutputDebugString(L"Couldn't connect to websocket!");
}
});

Need help loading Cortana VCD file on using C++ DirectX/XAML

I am adding Cortana to a DirectX/XAML Windwos 10 game. Every example I can find is given in C#, not C++. Normally this wouldn't be a problem, but apparently I'm not implementing it correctly in C++ and need some help with this one.
The purpose of this chunk of code is to load a VCD file which Cortana uses for my app-related voice commands. I have already created the VCD file using standard examples.
This is the specific C# code I'm having trouble implementing in C++:
var storageFile =
await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///myvcdfile.xml"));
await
Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager.
InstallCommandDefinitionFromStorageFileAsync(storageFile);
My attempted C++ implementation of this is:
Uri^ uri = ref new Uri("ms-appx:///myvcdfile.xml");
create_task(StorageFile::GetFileFromApplicationUriAsync(uri)).then([](task<StorageFile^> t) {
StorageFile^ sfile = t.get();
Windows::ApplicationModel::VoiceCommands::VoiceCommandDefinitionManager::InstallCommandDefinitionsFromStorageFileAsync(sfile);
});
When I run this, no visible error is thrown (no immediate crash) but in the output window the following exceptions are thrown:
Exception thrown at 0x76473E28 (KernelBase.dll) in darksong.exe: 0x40080201: WinRT originate error (parameters: 0x8000000B, 0x00000040, 0x018BE280).
Exception thrown at 0x76473E28 (KernelBase.dll) in darksong.exe: 0x00000005: Access is denied.
Exception thrown at 0x76473E28 (KernelBase.dll) in darksong.exe: 0x000006D9: There are no more endpoints available from the endpoint mapper.
Exception thrown at 0x76473E28 (KernelBase.dll) in darksong.exe: 0x000006D9: There are no more endpoints available from the endpoint mapper.
Exception thrown at 0x76473E28 (KernelBase.dll) in darksong.exe: 0x40080201: WinRT originate error (parameters: 0x80004005, 0x00000013, 0x0EEAF160).
The "access is denied" error makes me think there is some issue opening the file itself, although if I purposely enter an invalid filename it's a completely different error which crashes, so I know it's finding the file but perhaps is having some issue actually accessing it?
Also, even if I exclude the "InstallCommandDefinitionsFromStorageFileAsync()" line The exceptions are all still thrown.
Any help is appreciated, thanks in advance!
You have certainly an error in your xml file (if you have set for exemple in command an item using PhraseList but you have forgot the PhraseList node in your xml)

Poco - failure to openApplication log causes subsystem shutdown failure

I'm using Poco 1.6.0 and the Util::ServerApplication structure.
At the start of int main(const ArgVec& args) in my main class, I redirect all the logging to a file:
Poco::AutoPtr<Poco::FileChannel> chanFile = new Poco::FileChannel;
chanFile->setProperty("path", "C:\\doesnotexist\\file.log");
Poco::Util::Application::instance().logger().setChannel(chanFile);
If the log file cannot be opened, this causes an exception to be thrown, which I catch, and return an error code from main(). The Application::run() code in Poco's Application.cpp then calls Application::uninitialize().
The implementation of Application::uninitialize()iterates through each SubSystem executing that subsystem's uninitialize().
But one of those is LogFile::uninitialize(), which causes the following message to be logged: Uninitializing subsystem: Logging Subsystem.
When it attempts to log that message, an exception is thrown since the log file could not be opened (for the same reason as before). That exception is caught somewhere in Poco's code and it attempts to log an error, which causes an exception, and that one finally terminates the program.
How should I deal with this issue? E.g. is it possible to tell the logging subsystem to not throw any exceptions?
There seems to be a greater issue too; if any subsystem uninitialize() throws, this will cause execution to leave the subsystem shutdown loop in Application.cpp , so other subsystems will not have a chance to shut down either.
You should make sure that the path exist before setting up the file channel, e.g.:
if (Poco::File("C:\\doesnotexist").exists())
{
Poco::AutoPtr<Poco::FileChannel> chanFile = new Poco::FileChannel;
chanFile->setProperty("path", "C:\\doesnotexist\\file.log");
Poco::Util::Application::instance().logger().setChannel(chanFile);
}
Application::unitialize() will loop through subsystems and log iterations as debug messages - the idea is to catch problems before release.
UPDATE: as pointed in the comments, the directory may exist at the time of the check but may not exist (or not be accessible) afterwards, when logging actually happens. There is nothing in Poco that shields user from that; so, you will have to make sure the directory exists and is accessible throughout the lifetime of the FileChannel using it. I have not found this to be an obstacle in practice. I did find the initial non-existence of a directory to be an annoying problem and there is a proposal for addition of such (optional/configurable) feature but it has not been scheduled yet for inclusion in upcoming releases.

expat exception handling

I had been trying hard to figure out why the exceptions thrown from StartElement event handler are not being caught by my application which makes use of expat parser( in C). The application just terminates saying that it cannot find catch blocks, though I have all the catch blocks in place. It is just that since exceptions are being thrown from my own event handlers, XML_Parse API of expat is unable to pass it on to my code where I have all the catch blocks. One of the stackoverflow user with name 'Michael Anderson" suggested to rebuild expat lib with necessary gcc flags to make expat lib handle exceptions. Can someone let me know what flags are those? Or Suggest a better way out to handle errors in event handlers like startelement, endelement etc.
I somehow want XML_Parse API to return 0 if I encounter any exception in my registered event handlers. Please help. Thanks in advance.
Here is the code:
try
{
if( ! XML_Parse(.....) )
{
throw exception;
}
}
catch(...)
{
}
In the working scenario, if XML_Parse encounters a malformed xml file, it promptly returns zero, and I get into if block and throw exception, and it is caught fine.
But in the problematic case, the exceptions are being thrown from the event handlers but my application dumps core, and core stack says that it cannot find catch and finally calling std::terminate and abort.
Now, how do I make XML_Parse to return zero when I want to throw user defined exception from event handlers?
As per expat.h, you should invoke XML_StopParser(parser, 0) when you encounter an error in your handler that warrants aborting the parse.
XML_Parse will then return XML_FALSE. At that point you can invoke your application-specific error handling