-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remarks to last error Marshal APIs
- Loading branch information
1 parent
f532ec7
commit e7a7126
Showing
4 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
samples/snippets/csharp/System.Runtime.InteropServices/Marshal/GetLastPInvokeError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//<snippet1> | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
// These functions specify SetLastError=true to propagate the last error from the p/invoke | ||
// such that it can be retrieved using Marshal.GetLastPInvokeError(). | ||
internal static class Kernel32 | ||
{ | ||
[DllImport(nameof(Kernel32), ExactSpelling = true, SetLastError = true)] | ||
internal static extern bool SetCurrentDirectoryW([MarshalAs(UnmanagedType.LPWStr)] string path); | ||
} | ||
|
||
internal static class libc | ||
{ | ||
[DllImport(nameof(libc), SetLastError = true)] | ||
internal static extern int chdir([MarshalAs(UnmanagedType.LPUTF8Str)] string path); | ||
} | ||
|
||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
// Call p/invoke with valid arguments. | ||
CallPInvoke(AppContext.BaseDirectory); | ||
|
||
// Call p/invoke with invalid arguments. | ||
CallPInvoke(string.Empty); | ||
} | ||
|
||
private static void CallPInvoke(string path) | ||
{ | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
Console.WriteLine($"Calling SetCurrentDirectoryW with path '{path}'"); | ||
Kernel32.SetCurrentDirectoryW(path); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Calling chdir with path '{path}'"); | ||
libc.chdir(path); | ||
} | ||
|
||
// Get the last p/invoke error and display it. | ||
int error = Marshal.GetLastPInvokeError(); | ||
Console.WriteLine($"Last p/invoke error: {error}"); | ||
} | ||
} | ||
//</snippet1> |
8 changes: 8 additions & 0 deletions
8
samples/snippets/csharp/System.Runtime.InteropServices/Marshal/Marshal.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters