Example #1
0
        static XenStoreFunctions()
        {
            // Find the XenStore dll location - default or registry based?
            string xenStorePath;

            if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
            {
                xenStorePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + DefaultXS2SubPath + "\\xs2_32.DLL";
            }
            else
            {
                xenStorePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + DefaultXS2SubPath + "\\xs2.DLL";
            }

            Microsoft.Win32.RegistryKey xentoolsRegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(XenToolsRegkey);
            if (xentoolsRegKey != null)
            {
                xenStorePath = xentoolsRegKey.GetValue(XenStoreDll, xenStorePath).ToString();
            }

            //Get a DLL pointer
            IntPtr pDll = LoadLibrary(xenStorePath);

            //Pointers to DLL functions
            IntPtr pAddressOfxs_open = IntPtr.Zero;
            IntPtr pAddressOfxs_close = IntPtr.Zero;
            IntPtr pAddressOfxs_read = IntPtr.Zero;
            IntPtr pAddressOfxs_write = IntPtr.Zero;
            IntPtr pAddressOfxs_write_bin = IntPtr.Zero;
            IntPtr pAddressOfxs_free = IntPtr.Zero;
            IntPtr pAddressOfxs_directory = IntPtr.Zero;
            IntPtr pAddressOfxs_remove = IntPtr.Zero;
            IntPtr pAddressOfxs_watch = IntPtr.Zero;
            IntPtr pAddressOfxs_unwatch = IntPtr.Zero;

            //Get the pointers to DLL functions based on process bitage
            if (IntPtr.Size == 4)
            {
                pAddressOfxs_open = GetProcAddress(pDll, "_xs2_open@0");
                pAddressOfxs_close = GetProcAddress(pDll, "_xs2_close@4");
                pAddressOfxs_read = GetProcAddress(pDll, "_xs2_read@12");
                pAddressOfxs_write = GetProcAddress(pDll, "_xs2_write@12");
            //                pAddressOfxs_write_bin = GetProcAddress(pDll, "_xs2_write_bin@16");
                pAddressOfxs_free = GetProcAddress(pDll, "_xs2_free@4");
                pAddressOfxs_directory = GetProcAddress(pDll, "_xs2_directory@12");
                pAddressOfxs_remove = GetProcAddress(pDll, "_xs2_remove@8");
                pAddressOfxs_watch = GetProcAddress(pDll, "_xs2_watch@12");
                pAddressOfxs_unwatch = GetProcAddress(pDll, "_xs2_unwatch@4");
            }
            else if (IntPtr.Size == 8)
            {
                pAddressOfxs_open = GetProcAddress(pDll, "xs2_open");
                pAddressOfxs_close = GetProcAddress(pDll, "xs2_close");
                pAddressOfxs_read = GetProcAddress(pDll, "xs2_read");
                pAddressOfxs_write = GetProcAddress(pDll, "xs2_write");
            //                pAddressOfxs_write_bin = GetProcAddress(pDll, "xs2_write_bin");
                pAddressOfxs_free = GetProcAddress(pDll, "xs2_free");
                pAddressOfxs_directory = GetProcAddress(pDll, "xs2_directory");
                pAddressOfxs_remove = GetProcAddress(pDll, "xs2_remove");
                pAddressOfxs_watch = GetProcAddress(pDll, "xs2_watch");
                pAddressOfxs_unwatch = GetProcAddress(pDll, "xs2_unwatch");
            }

            #region DLL delegations

            //Delegate the dll open pointer to the method xs2_open()
            xs2_open = (raw_xs2_open)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_open, typeof(raw_xs2_open));

            //Delegate the dll close pointer to the method xs2_close()
            xs2_close = (raw_xs2_close)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_close, typeof(raw_xs2_close));

            //Delegate the dll read pointer to the method xs2_read_impl()
            xs2_read_impl = (raw_xs2_read)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_read, typeof(raw_xs2_read));

            //Delegate the dll write pointer to the method xs2_write
            xs2_write = (raw_xs2_write)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_write, typeof(raw_xs2_write));

            //Write bin has been depricated. Delegate the dll write bin pointer to the method xs2_write_bin_impl()
            //            xs2_write_bin_impl = (raw_xs2_write_bin)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_write_bin, typeof(raw_xs2_write_bin));

            //Delegate the dll free pointer to the method xs2_free()
            xs2_free = (raw_xs2_free)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_free, typeof(raw_xs2_free));

            //Delegate the dll directory pointer to the method xs2_directory_impl()
            xs2_directory_impl = (raw_xs2_directory)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_directory, typeof(raw_xs2_directory));

            //Delegate the dll remove pointer to the method xs2_remove()
            xs2_remove = (raw_xs2_remove)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_remove, typeof(raw_xs2_remove));

            //Delegate the dll watch pointer to the method xs2_watch()
            xs2_watch = (raw_xs2_watch)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_watch, typeof(raw_xs2_watch));

            //Delegate the dll unwatch pointer to the method xs2_unwatch()
            xs2_unwatch = (raw_xs2_unwatch)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_unwatch, typeof(raw_xs2_unwatch));
            #endregion
        }
Example #2
0
        static XenStoreFunctions()
        {
            // Find the XenStore dll location - default or registry based?
            string xenStorePath;
            
            if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
            {
                xenStorePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + DefaultXS2SubPath + "\\xs2_32.DLL";
            }
            else
            {
                xenStorePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + DefaultXS2SubPath + "\\xs2.DLL";
            }

            Microsoft.Win32.RegistryKey xentoolsRegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(XenToolsRegkey);
            if (xentoolsRegKey != null)
            {
                xenStorePath = xentoolsRegKey.GetValue(XenStoreDll, xenStorePath).ToString();
            }

            //Get a DLL pointer
            IntPtr pDll = LoadLibrary(xenStorePath);

            //Pointers to DLL functions
            IntPtr pAddressOfxs_open = IntPtr.Zero;
            IntPtr pAddressOfxs_close = IntPtr.Zero;
            IntPtr pAddressOfxs_read = IntPtr.Zero;
            IntPtr pAddressOfxs_write = IntPtr.Zero;
            IntPtr pAddressOfxs_write_bin = IntPtr.Zero;
            IntPtr pAddressOfxs_free = IntPtr.Zero;
            IntPtr pAddressOfxs_directory = IntPtr.Zero;
            IntPtr pAddressOfxs_remove = IntPtr.Zero;
            IntPtr pAddressOfxs_watch = IntPtr.Zero;
            IntPtr pAddressOfxs_unwatch = IntPtr.Zero;

            //Get the pointers to DLL functions based on process bitage
            if (IntPtr.Size == 4)
            {
                pAddressOfxs_open = GetProcAddress(pDll, "_xs2_open@0");
                pAddressOfxs_close = GetProcAddress(pDll, "_xs2_close@4");
                pAddressOfxs_read = GetProcAddress(pDll, "_xs2_read@12");
                pAddressOfxs_write = GetProcAddress(pDll, "_xs2_write@12");
//                pAddressOfxs_write_bin = GetProcAddress(pDll, "_xs2_write_bin@16");
                pAddressOfxs_free = GetProcAddress(pDll, "_xs2_free@4");
                pAddressOfxs_directory = GetProcAddress(pDll, "_xs2_directory@12");
                pAddressOfxs_remove = GetProcAddress(pDll, "_xs2_remove@8");
                pAddressOfxs_watch = GetProcAddress(pDll, "_xs2_watch@12");
                pAddressOfxs_unwatch = GetProcAddress(pDll, "_xs2_unwatch@4");
            }
            else if (IntPtr.Size == 8)
            {
                pAddressOfxs_open = GetProcAddress(pDll, "xs2_open");
                pAddressOfxs_close = GetProcAddress(pDll, "xs2_close");
                pAddressOfxs_read = GetProcAddress(pDll, "xs2_read");
                pAddressOfxs_write = GetProcAddress(pDll, "xs2_write");
//                pAddressOfxs_write_bin = GetProcAddress(pDll, "xs2_write_bin");
                pAddressOfxs_free = GetProcAddress(pDll, "xs2_free");
                pAddressOfxs_directory = GetProcAddress(pDll, "xs2_directory");
                pAddressOfxs_remove = GetProcAddress(pDll, "xs2_remove");
                pAddressOfxs_watch = GetProcAddress(pDll, "xs2_watch");
                pAddressOfxs_unwatch = GetProcAddress(pDll, "xs2_unwatch");
            }

            #region DLL delegations

            //Delegate the dll open pointer to the method xs2_open()
            xs2_open = (raw_xs2_open)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_open, typeof(raw_xs2_open));

            //Delegate the dll close pointer to the method xs2_close()
            xs2_close = (raw_xs2_close)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_close, typeof(raw_xs2_close));

            //Delegate the dll read pointer to the method xs2_read_impl()
            xs2_read_impl = (raw_xs2_read)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_read, typeof(raw_xs2_read));

            //Delegate the dll write pointer to the method xs2_write
            xs2_write = (raw_xs2_write)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_write, typeof(raw_xs2_write));

            //Write bin has been depricated. Delegate the dll write bin pointer to the method xs2_write_bin_impl()
//            xs2_write_bin_impl = (raw_xs2_write_bin)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_write_bin, typeof(raw_xs2_write_bin));

            //Delegate the dll free pointer to the method xs2_free()
            xs2_free = (raw_xs2_free)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_free, typeof(raw_xs2_free));

            //Delegate the dll directory pointer to the method xs2_directory_impl()
            xs2_directory_impl = (raw_xs2_directory)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_directory, typeof(raw_xs2_directory));

            //Delegate the dll remove pointer to the method xs2_remove()
            xs2_remove = (raw_xs2_remove)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_remove, typeof(raw_xs2_remove));

            //Delegate the dll watch pointer to the method xs2_watch()
            xs2_watch = (raw_xs2_watch)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_watch, typeof(raw_xs2_watch));

            //Delegate the dll unwatch pointer to the method xs2_unwatch()
            xs2_unwatch = (raw_xs2_unwatch)Marshal.GetDelegateForFunctionPointer(pAddressOfxs_unwatch, typeof(raw_xs2_unwatch));
            #endregion
        }