Search code examples
fileiouwpc++-cli

file access in UWP through c++ cli DLL


I have a wpf application which uses a DLL created with C++/CLI.

The DLL reads a file from the installation folder. It does this at start-up without use of a file picker.

Actually the DLL uses OpenCV and I call an opencv function to load the file.

I do this..

 std::string pathUnmanged = msclr::interop::marshal_as<std::string>(file)

then pass pathUnmanaged to the OpenCV function which loads the file.

here path is just filename and extension.

This works for me in release build. However, if i convert to a windows store app UWP using the desktop bridge then the file does not load.

Can someone point me in the right direction?


Solution

  • In WPF release mode, when you click your exe to start your application, the current directory is set to the same folder of the exe, so the program can locate the file using "filename.ext".

    After you package this to a Store app, however, the current directory maybe somewhere else.

    You can add this line to the WPF program's Main method to check where is the current directory.

    MessageBox.Show(Environment.CurrentDirectory);
    

    If it is the case, use full path of the file. The full path can be retrieved in this way

    var fullFileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
                "filename.ext");
    

    I write it in C#, but you can translate it to C++/CLI easily.