From d056362081a3d420d363648a71b9275e3a7cb0e9 Mon Sep 17 00:00:00 2001 From: Rich Date: Thu, 14 Nov 2013 16:20:08 +0000 Subject: [PATCH] Added logic to support reading a 64 bit registry from 32 bit code. Fixed logic to check to see if MSWord 2007 has the addin installed for PDF Export Added Unit RO support for MSWord Sections for U-Name, U-ID, U-Other Text, U-Other Number, U-Other Name, and U-OtherID Fixed code to handle Library Documents being remove from a list. Removed Debug Output --- PROMS/LBWordLibrary/LBObjectExtension.cs | 119 ++++++++++++++++-- PROMS/LBWordLibrary/RegHive.cs | 110 ++++++++++++++++ .../Extension/DocumentExt.cs | 6 + PROMS/Volian.Controls.Library/DSOTabPanel.cs | 2 +- .../Volian.Controls.Library/DisplayLibDocs.cs | 41 +++--- 5 files changed, 245 insertions(+), 33 deletions(-) create mode 100644 PROMS/LBWordLibrary/RegHive.cs diff --git a/PROMS/LBWordLibrary/LBObjectExtension.cs b/PROMS/LBWordLibrary/LBObjectExtension.cs index a9a94b59..c9ccba15 100644 --- a/PROMS/LBWordLibrary/LBObjectExtension.cs +++ b/PROMS/LBWordLibrary/LBObjectExtension.cs @@ -5,6 +5,7 @@ using System.Reflection; using Microsoft.Win32; using System.Windows.Forms; using System.Text.RegularExpressions; +using Read64bitRegistryFrom32bitApp; namespace LBWordLibrary { @@ -34,31 +35,118 @@ namespace LBWordLibrary return false; } } - public bool WordPDFExporterInstalled + private static bool? _WordPDFExporterInstalled = null; + public static bool WordPDFExporterInstalled { get { + if (_WordPDFExporterInstalled == null) + { // this is to determine if ExportAsFixedFormat was installed in MSOffice // This key only worked for Vista //RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"Installer\Components\12B306B24E250DD428FC7016B6FB4BD8"); // This key works for Windows Vista and Windows 7 try { - RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0E5C161912A2A6C4C93C76678926C56C"); - if (key != null) - { - key.Close(); - return true; - } - _MyLog.WarnFormat("MSWord2007 PDF Export Registry Key Missing"); - return false; + string keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0E5C161912A2A6C4C93C76678926C56C"; + bool validKey = RegistryWOW6432.CheckRegKey64Valid(RegHive.HKEY_LOCAL_MACHINE, keyPath); + _WordPDFExporterInstalled = validKey; + if(!validKey) + _MyLog.WarnFormat("MSWord2007 PDF Export Registry Key Missing"); + //RegistryKey key = Registry.LocalMachine.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree); + //if (key == null) + //{ + // RegistryKey localMachine64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); + // key = localMachine64.OpenSubKey(keystring, RegistryKeyPermissionCheck.ReadSubTree); + //} + //if (key != null) + //{ + // key.Close(); + // _WordPDFExporterInstalled = true; + // return (bool) _WordPDFExporterInstalled; + //} + //_WordPDFExporterInstalled = FindEXP_PDF(); + return (bool) _WordPDFExporterInstalled; } catch (Exception ex) { _MyLog.WarnFormat("MSWord2007 PDF Export Registry Key Failure {0} - {1}", ex.GetType().Name, ex.Message); + _WordPDFExporterInstalled = false; + return (bool) _WordPDFExporterInstalled; + } + + } + return (bool) _WordPDFExporterInstalled; + } + } + private static bool FindEXP_PDF() + { + bool retval = false; + DateTime dtStart = DateTime.Now; + try + { + //string keystr = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\0E5C161912A2A6C4C93C76678926C56C"; + RegistryKey key = null; + string keystr = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components"; + while (key == null && keystr != "") + { + key = Registry.LocalMachine.OpenSubKey(keystr, RegistryKeyPermissionCheck.ReadSubTree); + keystr = keystr.Substring(0, keystr.LastIndexOf("\\")); + } + if (FindKeys(key, "EXP_PDF.DLL")) + retval = true; + } + catch (Exception ex) + { + AddKeyFound(string.Format("{0} - {1}", ex.GetType().Name, ex.Message)); + } + finally + { + DateTime dtEnd = DateTime.Now; + TimeSpan ts = TimeSpan.FromTicks(dtEnd.Ticks - dtStart.Ticks); + AddKeyFound(string.Format("{0} Seconds", ts.TotalSeconds)); + } + return retval; + } + private static bool FindKeys(RegistryKey key, string find) + { + string[] keyNames = key.GetSubKeyNames(); + foreach (string keyName in keyNames) + { + using (RegistryKey subKey = key.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadSubTree)) + { + if (FindKeys(subKey, find)) return true; + } + } + if (FindNames(key, find)) return true; return false; } + private static bool FindNames(RegistryKey key, string find) + { + string[] names = key.GetValueNames(); + foreach (string name in names) + { + if (FindNames(key, name, find)) return true; } + return false; + } + private static bool FindNames(RegistryKey key, string name, string find) + { + RegistryValueKind kind = key.GetValueKind(name); + if (kind == RegistryValueKind.String) + { + string val = (string)key.GetValue(name); + if (val.Contains(find)) + { + AddKeyFound(string.Format("'{0}'\r\n Value {1} = '{2}'", key.ToString(), name, val)); + return true; + } + } + return false; + } + private static void AddKeyFound(string str) + { + _MyLog.WarnFormat("RegistryKeySearch: {0}",str); } private bool CanCreatePDFFile(string pdfFileName) { @@ -126,14 +214,21 @@ namespace LBWordLibrary if (((int)Convert.ToSingle(Version)) > 12) return CreatePDF2007(pdfFileName, DebugStatus); if (((int)Convert.ToSingle(Version)) == 12 && WordPDFExporterInstalled) + { + //_MyLog.WarnFormat("Word PDF Exporter Installed - MSWord Version = '{0}'" , Version); return CreatePDF2007(pdfFileName, DebugStatus); - else if (VolianPDFInstalled) + } + if (VolianPDFInstalled) { _MyLog.WarnFormat("Using VolianPDFWriter - MSWord Version = '{0}'", Version); return CreatePDF2003BG(pdfFileName); } - else - throw new Exception("No PDF Writer support installed for MS Word sections"); + //else // Force PDF Export + //{ + // _MyLog.WarnFormat("Force PDF Export for Michelle - MSWord Version = '{0}'", Version); + // return CreatePDF2007(pdfFileName, DebugStatus); + throw new Exception("No PDF Writer support installed for MS Word sections"); + //} } public string CreatePDF2003FG(string pdfFileName, bool openPDF) { diff --git a/PROMS/LBWordLibrary/RegHive.cs b/PROMS/LBWordLibrary/RegHive.cs new file mode 100644 index 00000000..f4bb27b0 --- /dev/null +++ b/PROMS/LBWordLibrary/RegHive.cs @@ -0,0 +1,110 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace Read64bitRegistryFrom32bitApp +{ + public enum RegSAM + { + QueryValue = 0x0001, + SetValue = 0x0002, + CreateSubKey = 0x0004, + EnumerateSubKeys = 0x0008, + Notify = 0x0010, + CreateLink = 0x0020, + WOW64_32Key = 0x0200, + WOW64_64Key = 0x0100, + WOW64_Res = 0x0300, + Read = 0x00020019, + Write = 0x00020006, + Execute = 0x00020019, + AllAccess = 0x000f003f + } + + public static class RegHive + { + public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u); + public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u); + } + + public static class RegistryWOW6432 + { + #region Member Variables + #region Read 64bit Reg from 32bit app + [DllImport("Advapi32.dll")] + static extern uint RegOpenKeyEx( + UIntPtr hKey, + string lpSubKey, + uint ulOptions, + int samDesired, + out int phkResult); + + [DllImport("Advapi32.dll")] + static extern uint RegCloseKey(int hKey); + + [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")] + public static extern int RegQueryValueEx( + int hKey, string lpValueName, + int lpReserved, + ref uint lpType, + System.Text.StringBuilder lpData, + ref uint lpcbData); + #endregion + #endregion + + #region Functions + static public string GetRegKey64Value(UIntPtr inHive, String inKeyName, String inPropertyName) + { + return GetRegKey64Value(inHive, inKeyName, RegSAM.WOW64_64Key, inPropertyName); + } + static public string GetRegKey32Value(UIntPtr inHive, String inKeyName, String inPropertyName) + { + return GetRegKey64Value(inHive, inKeyName, RegSAM.WOW64_32Key, inPropertyName); + } + static public string GetRegKey64Value(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName) + { + int hkey = 0; + try + { + uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey); + if (0 != lResult) return null; + uint lpType = 0; + uint lpcbData = 1024; + StringBuilder AgeBuffer = new StringBuilder(1024); + RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, AgeBuffer, ref lpcbData); + string Age = AgeBuffer.ToString(); + return Age; + } + finally + { + if (0 != hkey) RegCloseKey(hkey); + } + } + static public bool CheckRegKey64Valid(UIntPtr inHive, String inKeyName) + { + return CheckRegKey64Valid(inHive, inKeyName, RegSAM.WOW64_64Key); + } + static public bool GetRegKey32Valid(UIntPtr inHive, String inKeyName) + { + return CheckRegKey64Valid(inHive, inKeyName, RegSAM.WOW64_32Key); + } + static public bool CheckRegKey64Valid(UIntPtr inHive, String inKeyName, RegSAM in32or64key) + { + int hkey = 0; + try + { + uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey); + if (0 != lResult) return false; + return true; + } + finally + { + if (0 != hkey) RegCloseKey(hkey); + } + } + #endregion + + #region Enums + #endregion + } +} \ No newline at end of file diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs index a4747e4a..7c00871c 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/DocumentExt.cs @@ -810,6 +810,12 @@ namespace VEPROMS.CSLA.Library { if(sel.Text.ToUpper() == "") val =lookup.DocVersionInfo.DocVersionConfig.Unit_Text; else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Unit_Number; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Unit_Name; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Unit_ID; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Other_Unit_Text; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Other_Unit_Number; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Other_Unit_Name; + else if (sel.Text.ToUpper() == "") val = lookup.DocVersionInfo.DocVersionConfig.Other_Unit_ID; //val = "",MyDocVersion.DocVersionConfig.Unit_ID); //text = text.Replace(@"", _MyItemInfo.MyDocVersion.DocVersionConfig.Unit_ProcedureSetID); //text = text.Replace("", _MyItemInfo.MyDocVersion.DocVersionConfig.Unit_Number); diff --git a/PROMS/Volian.Controls.Library/DSOTabPanel.cs b/PROMS/Volian.Controls.Library/DSOTabPanel.cs index 5d59e8df..554a5cfd 100644 --- a/PROMS/Volian.Controls.Library/DSOTabPanel.cs +++ b/PROMS/Volian.Controls.Library/DSOTabPanel.cs @@ -187,7 +187,7 @@ namespace Volian.Controls.Library this._MyDSOFramer.Open(MyDSOFile.MyFile.FullName); } LBDocumentClass doc = new LBDocumentClass(_MyDSOFramer.ActiveDocument); - Console.WriteLine("Version {0}", doc.Application.Version); + //Console.WriteLine("Version {0}", doc.Application.Version); float ver; if (!float.TryParse(doc.Application.Version, out ver)) ver = 12.0F; diff --git a/PROMS/Volian.Controls.Library/DisplayLibDocs.cs b/PROMS/Volian.Controls.Library/DisplayLibDocs.cs index af7abc7f..61a020a3 100644 --- a/PROMS/Volian.Controls.Library/DisplayLibDocs.cs +++ b/PROMS/Volian.Controls.Library/DisplayLibDocs.cs @@ -268,26 +268,27 @@ namespace Volian.Controls.Library Application.DoEvents(); } } - private bool _ProcessingSelectedValueChanged = false; - private void listBoxUsages_SelectedValueChanged(object sender, EventArgs e) - { - if (!_InitializingLibDocList) - { - if (_ProcessingSelectedValueChanged) return; - _ProcessingSelectedValueChanged = true; - if (listBoxUsages.SelectedIndex == -1) return; - DocumentInfo di = LibDocList[listBoxLibDocs.SelectedIndex]; - // see if the library document actually has data - data migration may have created a 'null' content within the - // record for missing library documents. - if (di.DocContent == null) - { - MessageBox.Show("No Content in this document"); - return; - } - _DisplayTabControl.OpenItem(di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex]); - _ProcessingSelectedValueChanged = false; - } - } + private bool _ProcessingSelectedValueChanged = false; + private void listBoxUsages_SelectedValueChanged(object sender, EventArgs e) + { + if (!_InitializingLibDocList) + { + if (_ProcessingSelectedValueChanged) return; + _ProcessingSelectedValueChanged = true; + if (listBoxUsages.SelectedIndex == -1) return; + DocumentInfo di = LibDocList[listBoxLibDocs.SelectedIndex]; + // see if the library document actually has data - data migration may have created a 'null' content within the + // record for missing library documents. + if (di.DocContent == null) + { + MessageBox.Show("No Content in this document"); + return; + } + if (listBoxUsages.SelectedIndex < di.LibraryDocumentUsageList.Count && listBoxUsages.SelectedIndex >= 0) + _DisplayTabControl.OpenItem(di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex]); + _ProcessingSelectedValueChanged = false; + } + } #endregion private void btnPrint_Click(object sender, EventArgs e)