Skip to content

Commit 32a6047

Browse files
nxtnBillWagner
authored andcommitted
Migrate snippets to samples repo (#897)
1 parent ca658c8 commit 32a6047

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace ConsoleApplication1
5+
{
6+
public static class Program
7+
{
8+
// Define a delegate that corresponds to the unmanaged function.
9+
private delegate bool EnumWC(IntPtr hwnd, IntPtr lParam);
10+
11+
// Import user32.dll (containing the function we need) and define
12+
// the method corresponding to the native function.
13+
[DllImport("user32.dll")]
14+
private static extern int EnumWindows(EnumWC lpEnumFunc, IntPtr lParam);
15+
16+
// Define the implementation of the delegate; here, we simply output the window handle.
17+
private static bool OutputWindow(IntPtr hwnd, IntPtr lParam)
18+
{
19+
Console.WriteLine(hwnd.ToInt64());
20+
return true;
21+
}
22+
23+
public static void Main(string[] args)
24+
{
25+
// Invoke the method; note the delegate as a first parameter.
26+
EnumWindows(OutputWindow, IntPtr.Zero);
27+
}
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace PInvokeSamples
5+
{
6+
public static class Program
7+
{
8+
// Define a delegate that has the same signature as the native function.
9+
private delegate int DirClbk(string fName, StatClass stat, int typeFlag);
10+
11+
// Import the libc and define the method to represent the native function.
12+
[DllImport("libc.so.6")]
13+
private static extern int ftw(string dirpath, DirClbk cl, int descriptors);
14+
15+
// Implement the above DirClbk delegate;
16+
// this one just prints out the filename that is passed to it.
17+
private static int DisplayEntry(string fName, StatClass stat, int typeFlag)
18+
{
19+
Console.WriteLine(fName);
20+
return 0;
21+
}
22+
23+
public static void Main(string[] args)
24+
{
25+
// Call the native function.
26+
// Note the second parameter which represents the delegate (callback).
27+
ftw(".", DisplayEntry, 10);
28+
}
29+
}
30+
31+
// The native callback takes a pointer to a struct. The below class
32+
// represents that struct in managed code. You can find more information
33+
// about this in the section on marshalling below.
34+
[StructLayout(LayoutKind.Sequential)]
35+
public class StatClass
36+
{
37+
public uint DeviceID;
38+
public uint InodeNumber;
39+
public uint Mode;
40+
public uint HardLinks;
41+
public uint UserID;
42+
public uint GroupID;
43+
public uint SpecialDeviceID;
44+
public ulong Size;
45+
public ulong BlockSize;
46+
public uint Blocks;
47+
public long TimeLastAccess;
48+
public long TimeLastModification;
49+
public long TimeLastStatusChange;
50+
}
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace PInvokeSamples
5+
{
6+
public static class Program
7+
{
8+
// Define a delegate that has the same signature as the native function.
9+
private delegate int DirClbk(string fName, StatClass stat, int typeFlag);
10+
11+
// Import the libc and define the method to represent the native function.
12+
[DllImport("libSystem.dylib")]
13+
private static extern int ftw(string dirpath, DirClbk cl, int descriptors);
14+
15+
// Implement the above DirClbk delegate;
16+
// this one just prints out the filename that is passed to it.
17+
private static int DisplayEntry(string fName, StatClass stat, int typeFlag)
18+
{
19+
Console.WriteLine(fName);
20+
return 0;
21+
}
22+
23+
public static void Main(string[] args)
24+
{
25+
// Call the native function.
26+
// Note the second parameter which represents the delegate (callback).
27+
ftw(".", DisplayEntry, 10);
28+
}
29+
}
30+
31+
// The native callback takes a pointer to a struct. The below class
32+
// represents that struct in managed code.
33+
[StructLayout(LayoutKind.Sequential)]
34+
public class StatClass
35+
{
36+
public uint DeviceID;
37+
public uint InodeNumber;
38+
public uint Mode;
39+
public uint HardLinks;
40+
public uint UserID;
41+
public uint GroupID;
42+
public uint SpecialDeviceID;
43+
public ulong Size;
44+
public ulong BlockSize;
45+
public uint Blocks;
46+
public long TimeLastAccess;
47+
public long TimeLastModification;
48+
public long TimeLastStatusChange;
49+
}
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace PInvokeSamples
5+
{
6+
public static class Program
7+
{
8+
// Import the libc shared library and define the method
9+
// corresponding to the native function.
10+
[DllImport("libc.so.6")]
11+
private static extern int getpid();
12+
13+
public static void Main(string[] args)
14+
{
15+
// Invoke the function and get the process ID.
16+
int pid = getpid();
17+
Console.WriteLine(pid);
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace PInvokeSamples
5+
{
6+
public static class Program
7+
{
8+
// Import the libSystem shared library and define the method
9+
// corresponding to the native function.
10+
[DllImport("libSystem.dylib")]
11+
private static extern int getpid();
12+
13+
public static void Main(string[] args)
14+
{
15+
// Invoke the function and get the process ID.
16+
int pid = getpid();
17+
Console.WriteLine(pid);
18+
}
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
public class Program
5+
{
6+
// Import user32.dll (containing the function we need) and define
7+
// the method corresponding to the native function.
8+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
9+
private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
10+
11+
public static void Main(string[] args)
12+
{
13+
// Invoke the function as a regular managed method.
14+
MessageBox(IntPtr.Zero, "Command-line message box", "Attention!", 0);
15+
}
16+
}

0 commit comments

Comments
 (0)