locked
[UWP]Universal C# App calling unmanaged C++ DLL (Access Denied) RRS feed

  • Question

  • I have a basic C# Universal App that imports a very simple unamanged c++ dll and uses p/invoke to call a functions. The code works fine in a standard WPF Application but the Universal Application returns a "Unable to load dll... Access Denied" error.

    Is it not possible to import/invoke a unmanaged code dll in a Universal Application or is there something I am missing to make this work properly? 




    Monday, August 17, 2015 7:42 PM

Answers

  • Repeating my previous post:

    Apps can p-invoke DLLs deployed with the app package. The DLL will run in the app's context and have the same API access as the app itself. See Win32 and COM APIs for an overview of Win32 API available to Windows Runtime apps.

    You haven't provided enough information to diagnose your specific issue.

    Access Denied suggests that your DLL is not included in the app package. Make sure the binary is included in the project and marked as content, then double check it ends up in the Appx after building. The app cannot read or load arbitrary DLLs from other locations.

    --Rob

    • Marked as answer by revoXover Tuesday, August 18, 2015 12:11 AM
    Tuesday, August 18, 2015 12:00 AM

All replies

  • Apps can p-invoke DLLs deployed with the app package. The DLL will run in the app's context and have the same API access as the app itself. See Win32 and COM APIs for an overview of Win32 API available to Windows Runtime apps.

    You haven't provided enough information to diagnose your specific issue.

    Monday, August 17, 2015 8:20 PM
  • It is possible to do a platform invoke or p/invoke to call a function. And the very helpful link at

    Not knowing what you are doing with the native C++ DLL, there are few dlls that you can't reference.  However, for the most part, use pinvoke in a Universal app written in .NET the same way you do so in any other .NET app, so long as the API you invoke is allowed for UWP and Windows Runtime apps.

    For the list of allowed APIs that are allowed see:

    Also you may want to check out this article:

    If want to list your code, it will be easier to discuss the specific problem.

    Hope this helped you out, and if it did, please mark it as answered. 


    Sam Stokes

    • Edited by Surf4Fun Monday, August 17, 2015 8:31 PM Edit
    Monday, August 17, 2015 8:28 PM
  • Hi Sam, thanks so much you for your answer. I apologize, I don't think I worded my question correctly. I am not trying to use a Windows API. What I meant to say is I am having trouble calling an unmanaged C++ custom .dll. When I import the dll I get the "Access Denied" error. 

    I updated the question so it makes more sense. Thanks for your help.

    Monday, August 17, 2015 11:01 PM
  • Repeating my previous post:

    Apps can p-invoke DLLs deployed with the app package. The DLL will run in the app's context and have the same API access as the app itself. See Win32 and COM APIs for an overview of Win32 API available to Windows Runtime apps.

    You haven't provided enough information to diagnose your specific issue.

    Access Denied suggests that your DLL is not included in the app package. Make sure the binary is included in the project and marked as content, then double check it ends up in the Appx after building. The app cannot read or load arbitrary DLLs from other locations.

    --Rob

    • Marked as answer by revoXover Tuesday, August 18, 2015 12:11 AM
    Tuesday, August 18, 2015 12:00 AM
  • Bada-Bing! I was importing the dll from the output directory instead of the AppX directory. That did the trick. Thanks Rob!
    Tuesday, August 18, 2015 12:12 AM
  • Hi, I have developed a sample C# universal app, where i p-invoke a function in 'C' DLL  in the way you told. But when i try to deploy the app (i used release/x86 build of the app) in the local machine using VS2015 and it throws "An issue in the App.exe process caused it to fail to activate. This process exited with a native exception" dialog. I have checked the Appx bundle after creating packages and i found the C Dll there.

    Please Help me.


    • Edited by vivekbannu Tuesday, March 14, 2017 3:03 PM
    Tuesday, March 14, 2017 3:00 PM