Skip to content

Commit

Permalink
Move P/Invokes to NativeMethods class (dotnet#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtn authored and BillWagner committed Sep 16, 2019
1 parent 6eb2349 commit 59b9d33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ using namespace System::Text;
using namespace System::Runtime::InteropServices;

//<snippet2>
public ref class LibWrap
private ref class NativeMethods
{
public:
[DllImport("Kernel32.dll", CharSet=CharSet::Auto)]
static int GetSystemDirectory(StringBuilder^
sysDirBuffer, int size);
[DllImport("Kernel32.dll", CharSet = CharSet::Auto)]
static int GetSystemDirectory(StringBuilder^ sysDirBuffer, int size);

[DllImport("Kernel32.dll", CharSet=CharSet::Auto)]
[DllImport("Kernel32.dll", CharSet = CharSet::Auto)]
static IntPtr GetCommandLine();
};
//</snippet2>
Expand All @@ -24,10 +23,10 @@ public ref class App
{
// Call GetSystemDirectory.
StringBuilder^ sysDirBuffer = gcnew StringBuilder(256);
LibWrap::GetSystemDirectory(sysDirBuffer, sysDirBuffer->Capacity);
NativeMethods::GetSystemDirectory(sysDirBuffer, sysDirBuffer->Capacity);
// ...
// Call GetCommandLine.
IntPtr cmdLineStr = LibWrap::GetCommandLine();
IntPtr cmdLineStr = NativeMethods::GetCommandLine();
String^ commandLine = Marshal::PtrToStringAuto(cmdLineStr);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using System.Runtime.InteropServices;

//<snippet2>
public class LibWrap
internal static class NativeMethods
{
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetSystemDirectory(
internal static extern int GetSystemDirectory(
StringBuilder sysDirBuffer, int size);

[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetCommandLine();
internal static extern IntPtr GetCommandLine();
}
//</snippet2>

Expand All @@ -22,10 +22,10 @@ public static void Main()
{
// Call GetSystemDirectory.
StringBuilder sysDirBuffer = new StringBuilder(256);
LibWrap.GetSystemDirectory(sysDirBuffer, sysDirBuffer.Capacity);
NativeMethods.GetSystemDirectory(sysDirBuffer, sysDirBuffer.Capacity);
// ...
// Call GetCommandLine.
IntPtr cmdLineStr = LibWrap.GetCommandLine();
IntPtr cmdLineStr = NativeMethods.GetCommandLine();
string commandLine = Marshal.PtrToStringAuto(cmdLineStr);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Imports System.Text
Imports System.Runtime.InteropServices

'<snippet2>
Public Class LibWrap
Declare Auto Sub GetSystemDirectory Lib "Kernel32.dll" _
Friend Class NativeMethods
Friend Declare Auto Sub GetSystemDirectory Lib "Kernel32.dll" _
(ByVal sysDirBuffer As StringBuilder, ByVal buffSize As Integer)

Declare Auto Function GetCommandLine Lib "Kernel32.dll" () As IntPtr
Expand All @@ -16,10 +16,10 @@ Public Class App
Public Shared Sub Main()
' Call GetSystemDirectory.
Dim sysDirBuffer As New StringBuilder(256)
LibWrap.GetSystemDirectory(sysDirBuffer, sysDirBuffer.Capacity)
NativeMethods.GetSystemDirectory(sysDirBuffer, sysDirBuffer.Capacity)
' ...
' Call GetCommandLine.
Dim cmdLineStr As IntPtr = LibWrap.GetCommandLine()
Dim cmdLineStr As IntPtr = NativeMethods.GetCommandLine()
Dim commandLine As String = Marshal.PtrToStringAuto(cmdLineStr)
End Sub
End Class
Expand Down

0 comments on commit 59b9d33

Please sign in to comment.