It’s no secret that many of the .NET Framework functions are built on top of the Windows APIs. A quick visit to www.pinvoke.net provides a quick glance at many of those functions and more. While numerous of these functions are built-in to the .NET Framework, some of the more interesting ones are left aside. Here we examine two functions provided by the Kernel32.dll. Wikipedia defines the Kernel32.dll as:“Exposes to applications most of the Win32 base APIs, such as memory management, input/output operations, (process and thread) creation, and synchronization functions. Many of these are implemented within Kernel32.dll by calling corresponding functions in the native API, exposed by Ntdll.dll.” AllocConsoleIn short, the AllocConsole method allows you to allocate a standard Windows console to the calling process. One example would be to show output from a Windows forms applications to a console via the Console.Write() method. To expose the AllocConsole method you must first import the following namespace:
C# 1. using System.Runtime.InteropServices;Step 2 is to define the method:
C# 1. [DllImport("kernel32")]
2. static extern bool AllocConsole();Step 3 is to call the method:
C# 1. private void Form1_Load(object sender, EventArgs e)
2. {
3. AllocConsole();
4. Console.WriteLine("Hello vbCity!");
5. }
6.
7. [DllImport("kernel32")]
8. static extern bool AllocConsole(); CreateHardLinkThe CreateHardLink method allows you to create a link between two files. Once the link is created, any changes to the original file will be made to the secondary file. These changes are not bound within your application. In other words, if you programmatically create a link, editing the file with a text editor (one example) will also edit the other. CreateHardLink definition:
C# 1. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
2. static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
Read more: vbCity
C# 1. using System.Runtime.InteropServices;Step 2 is to define the method:
C# 1. [DllImport("kernel32")]
2. static extern bool AllocConsole();Step 3 is to call the method:
C# 1. private void Form1_Load(object sender, EventArgs e)
2. {
3. AllocConsole();
4. Console.WriteLine("Hello vbCity!");
5. }
6.
7. [DllImport("kernel32")]
8. static extern bool AllocConsole(); CreateHardLinkThe CreateHardLink method allows you to create a link between two files. Once the link is created, any changes to the original file will be made to the secondary file. These changes are not bound within your application. In other words, if you programmatically create a link, editing the file with a text editor (one example) will also edit the other. CreateHardLink definition:
C# 1. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
2. static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
Read more: vbCity