Custom URI ๋งŒ๋“ค๊ธฐ

2022. 7. 12. 13:46

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;">&nbsp;
<input type="button" value="exe ์‹คํ–‰" OnClick="fn(txtParam.value)">&nbsp;<br>
</div>
</BODY>
</HTML>

๋งŒ๋“  exe๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ์ƒ˜ํ”Œ html์„ ์ œ์ž‘ํ•˜์˜€๋‹ค.

text๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” input๊ณผ CustomURI๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ button์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.

 

HEAD์˜ script์— CustomURI๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์„ ์–ธํ•ด์ค€๋‹ค.

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” button์˜ OnClick์œผ๋กœ ์ž‘์šฉํ•˜์—ฌ ์ž…๋ ฅํ•œ text๊ฐ€ CustomURI์˜ ๋’ค์— ๋ถ™์„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.


์‹คํ–‰ํ•ด๋ณด๋ฉด ์œ„์™€๊ฐ™์€ ํ˜•ํƒœ๋กœ 

์ž…๋ ฅํ–ˆ๋˜ ๋ฌธ์ž์—ด์ด ์‹คํ–‰ํ”„๋กœ๊ทธ๋žจ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ

label ํ˜•ํƒœ๋กœ ๋ณด์—ฌ์ง„๋‹ค.

728x90

BELATED ARTICLES

more