Consume C++ library methods in UWP

Pariventhan Swaminathan 6 Reputation points
2019-12-05T21:07:19.48+00:00

I have c++ library (.dll) which is exposing few methods, i want to consume those methods in UWP application. The methods will call frequently from UWP application.

Can you please provide the solution on this.

Universal Windows Platform (UWP)
{count} vote

2 answers

Sort by: Most helpful
  1. Fay Wang - MSFT 5,221 Reputation points
    2019-12-06T01:49:18.863+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to use c++ methods in UWP, the first suggestion is to wrap that C++ logic using a WinRT component.

    But if you want to directly use your c++ library, you can PInvoke the dll. You need to follow these steps (You can also refer to this thread for more details):​

    First, add .dll into your UWP project making sure to set its type as 'content'​.

    Then in the proper .cs file, using DllImport to PInvoke the dll.​ And here is a similar thread, you can refer to it.

    Thanks.


  2. James Dailey - MSFT 336 Reputation points Microsoft Employee
    2020-02-03T22:55:42.553+00:00

    The UWP platform doesn't support all of the standard Win32 file APIs. From looking at your error it appears that the "log file" component you are using is trying to use the standard Win32 file I/O and failing.

    While some of the Win32 file APIs are supported there are a number of limitations. You will need to update the C++ code to use file APIs that work as expected from the UWP environment and / or make sure that you are accessing storage locations that are available from your UWP app.

    For example: When called from a Windows Store app, CreateFile2 is simplified. You can open only files or directories inside the ApplicationData.LocalFolder or Package.InstalledLocation directories.

    In most cases we recommend retrofitting your code to use the StorageFile APIs for easy interaction with the WinRT components. I've included links below.

    Reference:
    Alternatives to Windows APIs in Universal Windows Platform (UWP) apps
    https://zcusa.951200.xyz/en-us/uwp/win32-and-com/alternatives-to-windows-apis-uwp#storage

    APIs present on all Windows 10 devices
    https://zcusa.951200.xyz/en-us/uwp/win32-and-com/win32-apis

    CreateFile2 function
    https://zcusa.951200.xyz/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2?redirectedfrom=MSDN

    I hope this helps,

    James

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.