728x90

C언어에서 sendMessage()함수를 사용하는 것처럼 C#에서도 구현하고 싶은데 이것이 또 그냥 되지는 않더라.


[DllImport("coredll.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

이런 식으로 함수원형을 선언했다.

이렇게 구현하여 보내는 Message는 WndProc함수를 이용하여 확인할 수 있는데 .net framework에서는 Form class에서 지원하지만  compactframework에서는 사용할 수 없다.

MessageWindow를 이용하여 우회적으로 사용할 수는 있다.

protected override void WndProc(ref Message msg)
        {
            switch (msg.Msg)
            {
                case (MSG.Message1):
                    Console.WriteLine("Message1");
                    Program.mainform.Function1();
                    break;
                case (MSG.Message2):
                    Console.WriteLine("in WndProc Message2");
                    switch (MSG.WParam.ToInt32())
                    {
                        case START:
                            if (Program.mainform.CLI_Flag == true)
                            {
                                Program.mainform.AbortThread();
                            }
                            Program.mainform.g_bStop = true;
                            break;
                        case PAUSE:
                            break;
                        case RESTART:
                            break;
                        case END:
                            Console.WriteLine("END");
                            if (Program.mainform.CLI_Flag == true)
                            {
                                Program.mainform.CreateThread();
                            }
                            break;
                        case ERROR:
                            break;
                    }                    
                    //Do something here
                    break;
            }

            // call the base class WndProc for default message handling
            base.WndProc(ref msg);
        }
    }

다음은 message를 보내는 부분이다
SendMessage(winproc.Hwnd, MSG.Message1, IntPtr.Zero, IntPtr.Zero);

핸들러를 IntPtr형식으로 주게되면 dll에서 메시지를 보내는 것도 동일하게 받아올 수 있다.

728x90
728x90

[DllImport("coredll.dll", CharSet = CharSet.Unicode)]
        private static extern uint RegOpenKeyEx(
            uint HKEY,
            string lpSubKey,
            int ulOptions,
            uint samDesired,
            out uint phkResult);

        [DllImport("coredll.dll", CharSet = CharSet.Unicode)]
        private static extern uint RegQueryValueEx(
            uint hKey,
            string lpValueName,
            int lpReserved,
            ref int lpType,
            StringBuilder lpData,
            ref int lpcbData);

        [DllImport("coredll.dll")]
        private static extern int RegSetValueEx(
            uint hKey,
            [MarshalAs(UnmanagedType.LPArray)] byte[] lpValueName,
            int Reserved,
            Microsoft.Win32.RegistryValueKind dwType,
            [MarshalAs(UnmanagedType.LPArray)] byte[] lpData,
            int cbData);


        [DllImport("coredll.dll")]
        private static extern int RegCloseKey(uint hkey);

그런데 바로 적용되는 것이 있는가하면 재부팅 후 적용이 되는 녀석들도 많아 의도하는데로 사용하진 못했다.

728x90
728x90

private const uint HKEY_LOCAL_MACHINE = 0x80000002;

        [DllImport("CoreDll.dll", SetLastError = true)]
        private static extern IntPtr GetModuleHandle(IntPtr ModuleName);

        [DllImport("CoreDll.dll", SetLastError = true)]
        private static extern Int32 GetModuleFileName(IntPtr hModule, StringBuilder ModuleName, Int32 cch);


        public static string GetEntryAssembly()
        {
            StringBuilder fn = null;
            IntPtr hModule = GetModuleHandle(IntPtr.Zero);
            if (IntPtr.Zero != hModule)
            {
                fn = new StringBuilder(255);
                if (0 == GetModuleFileName(hModule, fn, fn.Capacity))
                    return null;
            }
            return fn.ToString();
        }

        public static string GetEntryDirectory()
        {
            string assembly = GetEntryAssembly();

            if (assembly == null)
                return null;

            int crop = assembly.LastIndexOf("\\");

            if (crop > 0)
                assembly = assembly.Remove(crop, assembly.Length - crop);

            return assembly;
        }

==========================================================
compack framework에서는 getCurrentDirectory()같은 함수를 사용할 수 없다.
하지만 돌려서 사용하는 함수를 구현해서 쓸 수 있다.

728x90
728x90

일반적인 .net framework에 비해 compack framework는 기능 제한이 너무 심하다.

PDA, 모바일용 프로그램을 작성할 때 사용되는데 특히 UI를 이쁘게 꾸미는 부분이 많이 막혀있다. 임베디드용이기 때문에 리소스를 많이 사용하는 것을 막아 여러 프로그램이 실행되더라도 안정적으로 돌아가게 할 수 있도록 프로그래머를 길들인다는 정책이다.

하지만 예측이란 것은 그 시점의 환경을 중심으로 하는 것이기 때문에 지금과 같이 하드웨어 환경이 좋아진 임베디드라면 그러한 제약을 오히려 풀어놓는 것이 맞다고 생각한다.


6월엔 WindowsCE 환경의 compact framework에서 작성한 프로그램을 가지고 사용법을 정리해봐야 겠다.

728x90

+ Recent posts