[UWP] - Native P/Invoke (dll compiled with mingw) RRS feed

  • Question

  • Hi there

    I was trying to make p invoke calls from the library that is compiled with mingw. I added my dll in portable project as content; dll appears in Appx folder after building. But I had the following error when p invoke call is reached :

    An exception of type 'System.DllNotFoundException' occurred in ..... The specified module could not be found. (Exception from HRESULT: 0x8007007E)

    I suspect uwp don't allow calls from native dll other then it have white-listed. My dll is also making called to further dll libs which are closed source. I don't need to publish the app on market but internally on our devices.

    If my feeling of restriction by uwp on native dll is true; is it thr way around it or internal use only?


    Monday, September 21, 2015 7:38 PM

All replies

  • Hi,

    I think the UWP does allow calls to native DLLs - e.g. I'm pretty sure that the current SQLite implementation is packaged as a native DLL for PInvoke access?

    Apologies if I'm not reading your question correctly but to reproduce the situation I tried (in VS2015);

    File->New->Project->Visual C#->Windows->Universal->Blank App.

    Solution-> Add New Project-> Visual C++->Win32->Win32 Project->DLL->Empty Project.

    To the CPP project, add a new CPP file with code;


    intAPIENTRYAdd(intx, inty)


    return(x+ y);

















    To the CPP project add a .DEF file (via Project-> Add New Item) with;



    Now, build the CPP project, take the DLL produced and add it to the C# project making sure to set its type as 'content' and 'copy if newer' on it.

    Now, change the MainPage.xaml.cs to include;

    publicsealedpartialclassMainPage: Page





    varten = Add(8, 2);

    vary = 0;



    staticexternintAdd(intx, inty);


    And that seemed to work for me in the sense that the native function 'Add' got called via PInvoke.

    Does that help at all?



    Monday, September 21, 2015 11:22 PM
  • Hi Guys

    Can I do something equivalent of adding private path in UWP app.


    I have dep folder containing native dll's i.e ./Appx/dep/*.dll.

    Need to add dep folder to that path of the application. 

    • Merged by Barry Wang Thursday, October 8, 2015 2:06 AM the same issue
    Tuesday, September 22, 2015 9:01 PM
  • Hello jas.bio,

    It seems in UWP to create a folder in a path we can only use StorageFolder class 

    But the Appx sounds like you want to add your dll to the installed location?

    Please tell us how you want to add this dll, at runtime or design time.

    Actually if you already have this dll file, I recommend you just create this folder in your project at design time. Then the dll file can be copied when deploy on the target machine.

    Best regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 23, 2015 12:48 PM
  • Hi Barry

    Thanks for replying; Let me try to do better job of asking the question.

    So I have lets say OpenCV library with like 10+ dlls. Right now I add all of dlls in root of UWP project as content and its packaged to Appx folder (where they are can be accessed for PInvoke calls).

    But if put them into folder and then add to UWP project as content; that folder will end up at Appx project. Now I am doing this so root of the project is not messy.

    Now the problem is dll will be not at the root of Appx folder which will not allow it to be accessible for PInvoke calls directly.

    In classic application dll we looked in the same folder, path, private path etc. I just want UWP app programmatically add a folder inside of Appx that will analogous to add folder to path for searching off dll to be loaded.

    Let me if my question is not clear yet again. I am willing to give it third go :)



    Friday, September 25, 2015 12:11 AM
  • Hi Mike

    Thx for reply mate.

    The above example works for me. But I am facing allot of issues and weird behavior during PInvoking under UWP app. Let me describe one example that I want to discuss with you.

    Below is the link..


    UWP_PInvoke.zip contains the solution with UWP project.

    native_opencv.zip is native dll project created with cmake for PInvoking.

    Native dll code seems like this.

    #include "opencv2/core.hpp"
    #include <opencv2/highgui/highgui.hpp>
    using namespace cv;
    extern "C" __declspec(dllexport)
    int __stdcall opencv()
        Mat image;
        image = imread("micro.png", CV_LOAD_IMAGE_COLOR);
        namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
        imshow( "Display window", image );                   // Show our image inside it.
        return 0;

    In UWP_PInvoke solution I have project named classic which is classic c# project; PInvoking with this works fine. But UWP project causes the following error.

    An exception of type 'System.DllNotFoundException' occurred in UWP_PInvoke.Windows.exe but was not handled in user code Additional information: Unable to load DLL 'native_opencv': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

    My troubles started when I was not able to run the following code under UWP resulting in the same error.


    Let me know if you want any other information or have any trouble using/accessing the attached samples.



    • Edited by jas.bio Friday, September 25, 2015 2:02 AM
    Friday, September 25, 2015 1:59 AM
  • This sounds like your DLL is calling API that aren't available to Windows Runtime apps and is failing to initialize. In particular, the DllNotFound error suggests the Dll may be linking to another Dll which isn't available. You can use loader snaps to confirm and diagnose the specific loading error.

    I'm not familiar enough with openCV to know what namedWindow is, but if it's trying to create a Win32 Window then that will fail when called from the app container. Make sure you're using the version of openCV which supports Windows apps (see https://msopentech.com/blog/2014/03/20/easily-build-opencv-powered-apps-for-windows-store/ ). All code run by the app, whether it's .Net or native, is subject to the app container and should use only supported API (p-invoke will bypass the compile-time checks, but it won't evade runtime checks and unsupported API may not behave as desired). See Win32 and COM for Windows Runtime apps and Universal Windows Platform (UWP) apps

    Friday, September 25, 2015 2:32 AM
  • @jas,

    As far as I know the content file will just under the root folder. May I know how you call it from p/invoke? I will try build a project to test. Please wait for my result.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Edited by Barry Wang Tuesday, September 29, 2015 12:32 PM
    Tuesday, September 29, 2015 12:22 PM
  • @Barry

    Hey for reply. Sorry for such an delayed response.

    Please refer to my another msdn thread for that.



    Monday, October 5, 2015 3:19 AM
  • Hi Rob

    Sorry for so late reply. I read you reply; was trying out things based info your provided. Then team put our UWP app under pause and I forgot to reply :P

    Anyways you response was very helpful. Include this one on stack-trace. 


    Sorry can create links for now :(

    Couple quick questions though; So do you think Project Centennial will be help to make old c++ libs available to new UI. Can we package the side loaded apps along with UWP UI app that will be for internal use and closed customer base and not for market.


    Monday, October 5, 2015 3:32 AM
  • This works with native Dll, however if the Dll is a Universal Windows Dll, then the .def file will be ignored and it will somehow not generate a format which DllImport in C# accepts - why I do not know - yet

    Friday, May 18, 2018 10:40 AM