KO CSharp Keyword extern - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
C#: extern
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
C# ์ฝ๋ ์ธ๋ถ์์ ๊ตฌํ๋ ๋ฉค๋ฒ๋ฅผ ์ ์ธํฉ๋๋ค. DllImport์ ํตํ ๊ธฐ๋ณธ ์ํธ ์ด์ฉ์ฑ์ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ ๋๋ค. ์ธ๋ถ ๋ณ์นญ์ ์ธ๋ถ ์ด์ ๋ธ๋ฆฌ ๋ณ์นญ์ ์ง์ ํฉ๋๋ค.
๊ฐ๋จํ ์์ (Example.cs)
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
class NativeMethods
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentProcessId();
}
์ ์ธ๋ง ํฌํจํฉ๋๋ค. ์ด ์ฝ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ปดํ์ผ๋๋ฉฐ ์คํํ๋ ค๋ฉด Main์์ ํธ์ถํด์ผ ํฉ๋๋ค.
ํต์ฌ๋ก
[DllImport(...)] public static extern ReturnType Method(...);
์ด ๋ ๊ฐ์ง ๊ธฐ๋ณธ ์์ ๋ Windows์ฉ์ ๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด๋ฆ, ABI ๋ฐ ๊ธฐ๋ฅ ๊ฐ์ฉ์ฑ์ ํ๋ซํผ์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค. ์ปดํ์ผ ์์ฒด์์๋ ํด๋น ํญ๋ชฉ์ ์กด์ฌ๋ฅผ ํ์ธํ์ง ์์ต๋๋ค.
์์ 1: ๊ธฐ๋ณธ ํํ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine(Native.GetCurrentProcessId());
}
}
public static class Native { [DllImport("kernel32.dll")] public static extern uint GetCurrentProcessId(); }
์ 2: ๋ ๋ค๋ฅธ ์ค์ ํ์
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine(Native.GetTickCount64());
}
}
public static class Native { [DllImport("kernel32.dll")] public static extern ulong GetTickCount64(); }
๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.