To make an external call in C#, you can use the DllImport
attribute from the System.Runtime.InteropServices
namespace. This allows you to call functions from unmanaged DLLs (Dynamic Link Libraries). Here's an example:
main.cs578 chars22 lines
In this example, we have a class called ExternalFunctions
that contains two external function declarations. The first function is MessageBeep
from user32.dll
, which causes the system to emit a beep sound. The second function is GetCurrentThreadId
from kernel32.dll
, which retrieves the current thread ID.
In the Main
method, we call MessageBeep
to emit a beep sound and then call GetCurrentThreadId
to retrieve the ID of the current thread. We then print the thread ID to the console.
Note that the DllImport
attribute is used to tell the compiler which DLL the function is located in. The attribute also specifies how the function should be called, such as the calling convention and whether the function is a Unicode or ANSI function.
gistlibby LogSnag