Custom URI ๋ง๋ค๊ธฐ
exe ํํ์ ์คํํ๋ก๊ทธ๋จ์ ํ ์คํธํ๊ธฐ ์ํด ํ๋ผ๋ฏธํฐ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ผ๋ก CustomURI๊ฐ ์๋ค.
Custom URI๋ http,ftp์ ๊ฐ์ URI Scheme ๋์
์์์ URI Scheme๋ฅผ ๋ ์ง์คํธ๋ฆฌ์ ๋ฑ๋กํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ ๋ปํ๋ค.
์๋ฅผ ๋ค์ด "test://"๋ก ์์ํ๋ URI Scheme๋ฅผ ๋ ์ง์คํธ๋ฆฌ์ ๋ฑ๋กํ๋ฉด
"test://testํ๋ก๊ทธ๋จ"์์ผ๋ก ํธ์ถํ ๊ฒฝ์ฐ "testํ๋ก๊ทธ๋จ"์ ํ๋ผ๋ฏธํฐ๋ก ์ฌ์ฉํ ์ ์๋ค.
์์
1. exe ํํ์ ์คํ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด
2. nsi ์คํฌ๋ฆฝํธ๋ฅผ ํตํด customURI๋ฅผ ๋ ์ง์คํธ๋ฆฌ์ ์ ์ฅํ ํ
3. ์ํ html์ ํตํด customURI๋ก exeํ๋ก๊ทธ๋จ์ด ์คํ๋๋์ง ํ์ธํ๋ค.
์ฌ๊ธฐ์ ์ฌ์ฉํ exe ํ๋ก๊ทธ๋จ์ ๊ฐ๋จํ๊ฒ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ผ๋ฒจ ํ์์ผ๋ก ๋์์ฃผ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
1. CustomURI.exe
static class Program
{
/// <summary>
/// ํด๋น ์์ฉ ํ๋ก๊ทธ๋จ์ ์ฃผ ์ง์
์ ์
๋๋ค.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// ๋งค๊ฐ๋ณ์ customURI ํํ๋ก ๋์ด์๋์ง ํ์ธ
if (args == null || args.Length < 1 || args[0].StartsWith("customuri://") == false) // uri ์๋ฌธ์์ฌ์ผํจ
{
MessageBox.Show("์คํ ํ๋ผ๋ฏธํฐ๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.", "์คํ ์ค๋ฅ");
return;
}
string param = args[0].Substring(args[0].IndexOf(':') + 3); // customURI ์ ๊ฑฐ
param = HttpUtility.UrlDecode(param, Encoding.UTF8); // Decoding
param = param.Substring(0, param.Length - 1); // ๋ง์ง๋ง ๋ฌธ์ '/' ์ ๊ฑฐ
if(param.Length < 1)
{
MessageBox.Show("์
๋ ฅ๊ฐ์ด ์์ต๋๋ค.");
return;
}
Form1 form = new Form1();
form.INPUT_TEXT = param;
Application.Run(form);
}
}
์ฐ์ Main์์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ ์ ์๋๋ก Main(string[] args)๋ก ์์ ํ๋ค.
๋งค๊ฐ๋ณ์๊ฐ ์๋์ง, ์ํ๋ CustomURI์ธ "customuri"//"๋ก ์์ํ๋์ง ํ์ธํ๋ค.
์ด๋ ๊ฒฝ๋ก ํํ๋ก ๋ค์ด์ค๋ ๋งค๊ฐ๋ณ์๋ ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ด๊ธฐ๋๋ฌธ์ CustomURI๋ฅผ ํ์ธํ ๋ ์๋ฌธ์๋ก ํ์ธํด์ผํ๋ค.
์ ๋๋ก ๋ค์ด์๋ค๋ฉด ํ๋ผ๋ฏธํฐ๋ง ๋จ๊ธฐ๊ธฐ ์ํด Substring์ ์ด์ฉํ์ฌ "customuri://"๋ฅผ ์ ์ธํ ๋ฌธ์์ด๋ง ๋จ๊ธฐ๊ณ
์ธ์ฝ๋ฉ๋ uri๋ฅผ ๋์ฝ๋ฉ ํ ๋ง์ง๋ง์ ๋ถ์ "/"๋ฅผ ์ ๊ฑฐํ์ฌ param์ ์ ์ฅํ๋ค.
param์ด ์๋์ง Length๋ฅผ ํตํด ํ์ธ ํ form์ ๋ณ์์ ๋ฃ์ด App์ ์คํํ๋ค.
2. CustomURI_Setup.nsi
; Custom URI
Section "RegistryWrite" SEC02
WriteRegStr HKCR "${Appname}" "URL protocol" ""
WriteRegStr HKCR "${Appname}\shell" "" "open"
WriteRegStr HKCR "${Appname}\shell\open" "" "command"
WriteRegStr HKCR "${Appname}\shell\open\command" "" '"$INSTDIR\${APPNAME}.exe" "%1"'
SectionEnd
์ ์ฝ๋ ์ธ์ nsi ์คํฌ๋ฆฝํธ๋ ๊ธฐ์กด๊ณผ ๋์ผํ๊ณ ํต์ฌ ๋ถ๋ถ์ customURI๋ฅผ ๋ ์ง์คํธ๋ฆฌ์ ๋ฑ๋กํ๋ ๊ฒ์ด๋ค.
HKEY_CLASSES_ROOT ์๋ customURI key๋ฅผ ์ถ๊ฐํ๊ณ URL protocol key๋ฅผ ์ถ๊ฐํ๋ค.
๊ทธ ์๋ ์ฐจ๋ก๋๋ก shell/open/command key๋ฅผ ์ถ๊ฐํ๊ณ
์คํํ์ผ์ด ์ค์น๋๋ ๊ฒฝ๋ก์ ๊ฒฝ๋ก ๋ค์ "%1"์ ์์ฑํ์ฌ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ ์ ์๋๋ก ํ๋ค.
์๋๋ ๋ ์ง์คํธ๋ฆฌ ํธ์ง๊ธฐ์ ๋ฑ๋ก๋ ๋ชจ์ต์ด๋ค.
3. Test_Sample.html
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>CustomURI Test</TITLE>
<script ID="clientEventHandlersJS" language="javascript">
function fn(fName)
{
var param = fName;
location.href = "CustomURI://" + encodeURI(param);
}
</script>
</HEAD>
<BODY>
<div>
<H1>CustomURI TEST</H1>
ํ๋ผ๋ฏธํฐ : <input type="text" id="txtParam" value="text" style="width:300px;">
<input type="button" value="exe ์คํ" OnClick="fn(txtParam.value)"> <br>
</div>
</BODY>
</HTML>
๋ง๋ exe๋ฅผ ํ ์คํธํ๊ธฐ ์ํด ์ํ html์ ์ ์ํ์๋ค.
text๋ฅผ ์ ๋ ฅํ ์ ์๋ input๊ณผ CustomURI๋ฅผ ํธ์ถํ๊ธฐ ์ํ button์ผ๋ก ์ด๋ฃจ์ด์ ธ์๋ค.
HEAD์ script์ CustomURI๋ฅผ ํธ์ถํ๊ธฐ ์ํด ํจ์๋ฅผ ํ๋ ์ ์ธํด์ค๋ค.
ํด๋น ํจ์๋ button์ OnClick์ผ๋ก ์์ฉํ์ฌ ์ ๋ ฅํ text๊ฐ CustomURI์ ๋ค์ ๋ถ์ ์ ์๋๋ก ํ๋ค.
์คํํด๋ณด๋ฉด ์์๊ฐ์ ํํ๋ก
์ ๋ ฅํ๋ ๋ฌธ์์ด์ด ์คํํ๋ก๊ทธ๋จ์ ํ๋ผ๋ฏธํฐ๋ก ๋์ด๊ฐ๋ฉด์
label ํํ๋ก ๋ณด์ฌ์ง๋ค.
'๐ป๊ฐ๋ฐ > WEB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
WebView2 runtime fixed version ์ ์ฉํ๊ธฐ (0) | 2022.08.02 |
---|---|
[.NET] ๋น๋(Build)์ ๋ค์๋น๋(ReBuild) ์ฐจ์ด (0) | 2022.07.25 |
[CSS] ์ ํ์ (0) | 2022.03.28 |
์ ์ ํ์ผ ๋ง๋ค๊ธฐ : ํญ ์์ด์ฝ ์ค์ ํ๊ธฐ (0) | 2022.03.27 |
๋ฒ๋ค๋ฌ (Bundler) : parcel, webpack, rollup (0) | 2022.03.27 |