From 3bbeef4a3ed0ee95cd0da3465cb3d91a393731cb Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Tue, 15 Sep 2020 13:20:12 -0700 Subject: [PATCH] Update P/Invoke sample to find MSVC compiler as part of build (#3749) * Update P/Invoke sample: metadata and CI build * PR feedback * readme -> README * OSX -> macOS --- .../marshaling/{readme.md => README.md} | 38 +++++---- .../MarshalingSample/MarshalingSample.csproj | 14 +++- .../MarshalingSampleNative.csproj | 80 +++++++++++++++++-- 3 files changed, 106 insertions(+), 26 deletions(-) rename core/interop/pinvoke/marshaling/{readme.md => README.md} (56%) diff --git a/core/interop/pinvoke/marshaling/readme.md b/core/interop/pinvoke/marshaling/README.md similarity index 56% rename from core/interop/pinvoke/marshaling/readme.md rename to core/interop/pinvoke/marshaling/README.md index d215d96024a..7f85ea00f13 100644 --- a/core/interop/pinvoke/marshaling/readme.md +++ b/core/interop/pinvoke/marshaling/README.md @@ -1,26 +1,35 @@ -.NET Core PInvoke Marshaling Sample -================ +--- +languages: +- csharp +- cpp +products: +- dotnet-core +page_type: sample +name: "P/Invoke Marshaling Sample" +urlFragment: "pinvoke-marshal-arguments" +description: "A .NET application that demonstrates different ways to marshal arguments to native functions when using P/Invokes." +--- -This project demonstrates different ways to marshal arguments to native function when using PInvokes. Documentation can be found here for [PInvokes](https://docs.microsoft.com/dotnet/standard/native-interop/pinvoke) and here for the [Type marshaling](https://docs.microsoft.com/dotnet/standard/native-interop/type-marshaling). +# .NET Core P/Invoke Marshaling Sample -Prerequisites ------------- +This project demonstrates different ways to marshal arguments to native functions when using P/Invokes. Documentation can be found here for [P/Invokes](https://docs.microsoft.com/dotnet/standard/native-interop/pinvoke) and here for the [Type marshaling](https://docs.microsoft.com/dotnet/standard/native-interop/type-marshaling). -* .NET Core 3.0 (at least Preview 6) - [https://dot.net](https://github.com/dotnet/core-sdk#installers-and-binaries) +## Prerequisites + +* [.NET Core 3.1 SDK](https://dotnet.microsoft.com/download) * C++ compiler * Windows: `cl.exe` - * Linux/OSX: `g++` + * See [installation instructions](https://docs.microsoft.com/cpp/build/building-on-the-command-line#download-and-install-the-tools). + * Linux/macOS: `g++` -Build and Run -------------- +## Build and Run 1) In order to build and run, all prerequisites must be installed. The following are also required: - * The C++ compiler (`cl.exe` or `g++`) must be on the path. - * On Windows, a [Developer Command Prompt for Visual Studio](https://docs.microsoft.com/cpp/build/building-on-the-command-line#developer_command_prompt_shortcuts) should be used. + * On Linux/macOS, the C++ compiler (`g++`) must be on the path. * The C++ compiler (`cl.exe` or `g++`) and `dotnet` must be the same bitness (32-bit versus 64-bit). - * On Windows, the default developer command prompt for VS uses the 32-bit compilers, but `dotnet` is typically 64-bit by default. Make sure to select the "x64 Native Tools Command Prompt for VS 2019" (or 2017). + * On Windows, the sample is set up to use the bitness of `dotnet` to find the corresponding `cl.exe` 1) Navigate to the root directory and run `dotnet build` @@ -33,7 +42,6 @@ Build and Run Note: The way the sample is built is relatively complicated. The goal is that it's possible to build and run the sample with simple `dotnet run` with minimal requirements on pre-installed tools. Typically real-world projects which have both managed and native components will use different build systems for each; for example msbuild/dotnet for managed and CMake for native. -Visual Studio support ---------------------- +## Visual Studio support -The `src\MarshalingSample.sln` can be used to open the sample in Visual Studio 2019. In order to be able to build from Visual Studio, though, it has to be started from the correct developer environment. From the developer environment console, start it with `devenv src\MarshalingSample.sln`. With that, the solution can be built. To run it set the start project to `MarshalingSample`. +The `src\MarshalingSample.sln` can be used to open the sample in Visual Studio 2019. In order to be able to build from Visual Studio, though, it has to be started from the correct developer environment. From the developer environment console, start it with `devenv src\MarshalingSample.sln`. With that, the solution can be built. To run it, set the start project to `MarshalingSample`. diff --git a/core/interop/pinvoke/marshaling/src/MarshalingSample/MarshalingSample.csproj b/core/interop/pinvoke/marshaling/src/MarshalingSample/MarshalingSample.csproj index 774acf6d5ec..8b5d664a69c 100644 --- a/core/interop/pinvoke/marshaling/src/MarshalingSample/MarshalingSample.csproj +++ b/core/interop/pinvoke/marshaling/src/MarshalingSample/MarshalingSample.csproj @@ -1,9 +1,14 @@  - netcoreapp3.0 + netcoreapp3.1 exe $(DefineConstants);WINDOWS + + + true @@ -12,10 +17,13 @@ - + + + + - + diff --git a/core/interop/pinvoke/marshaling/src/MarshalingSampleNative/MarshalingSampleNative.csproj b/core/interop/pinvoke/marshaling/src/MarshalingSampleNative/MarshalingSampleNative.csproj index b0dcdc4876f..e0258320de5 100644 --- a/core/interop/pinvoke/marshaling/src/MarshalingSampleNative/MarshalingSampleNative.csproj +++ b/core/interop/pinvoke/marshaling/src/MarshalingSampleNative/MarshalingSampleNative.csproj @@ -1,9 +1,28 @@  - netcoreapp3.0 + netcoreapp3.1 + + $(BinRoot)/$(Configuration) + MarshalingSampleNative + + $([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture) + $(MSBuildThisFileDirectory)obj/$(Configuration)/$(NativePlatform)/ + + + + + $(NativePlatform) + + + + + + + + @@ -12,19 +31,64 @@ false + + + + - - + + - $(BinRoot)/$(Configuration) + .so + .dylib + .dll + $(NativeBinDir)/$(NativeOutputName)$(NativeOutputExtension) + + @(NativeSource-> '"%(RootDir)%(Directory)%(Filename)%(Extension)"', ' ') - + + + - + + + -Iinc + -g -shared -fPIC + + + -D LINUX + -ldl -Wl,-rpath,'$ORIGIN',--disable-new-dtags + + + -D OSX + -ldl -Wl,-rpath,'@loader_path' + + + + - + + + @(MSVCIncludePaths-> '/I "%(RootDir)%(Directory)%(Filename)"', ' ') + $(IncPaths) /I inc + /EHsc /Od /GS /sdl /Zi + /D WINDOWS + @(MSVCLibPaths-> '/LIBPATH:"%(RootDir)%(Directory)%(Filename)"', ' ') + - +