question

PariventhanSwaminathan-1996 avatar image
1 Vote"
PariventhanSwaminathan-1996 asked ·

Consume C++ library methods in UWP

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.

windows-uwp
· 5
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,


I have tried the pInvoke approach, but i am getting the exception as "External Component thrown error".


below is my code, could you please help on this


aalt text


alt text


0 Votes 0 · ·
slnlog.png (21.6 KiB)
codec.png (25.9 KiB)
FayWang-MSFT avatar image FayWang-MSFT PariventhanSwaminathan-1996 ·

From the code about calling the dll, it looks no problem. So There may be something wrong in the C ++ code. Can you provide the C++ source code about the LibLog_CreateLog method which can be compiled for us to test?

0 Votes 0 · ·
FayWang-MSFT avatar image FayWang-MSFT PariventhanSwaminathan-1996 ·

Hi, is there any update? Have you solved your issue?

0 Votes 0 · ·
FayWang-MSFT avatar image FayWang-MSFT PariventhanSwaminathan-1996 ·

Hi, have you solved your issue? Do you have any other question?

0 Votes 0 · ·
FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

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.


· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi


Following is the exception i am getting in my code.


alt text


0 Votes 0 · ·
logerror.png (16.6 KiB)
jadailey-MSFT avatar image
0 Votes"
jadailey-MSFT answered ·

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://docs.microsoft.com/en-us/uwp/win32-and-com/alternatives-to-windows-apis-uwp#storage

APIs present on all Windows 10 devices
https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis

CreateFile2 function
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2?redirectedfrom=MSDN

I hope this helps,

James


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.