New test.
[mono.git] / mcs / class / System / System.Diagnostics / Win32EventLog.cs
index 02e0f569af444dcdf9c17fdaf8194aef4c5ed21c..221464dc9f1954bf35def7564ae2b0c49c5cab1f 100644 (file)
@@ -82,6 +82,9 @@ namespace System.Diagnostics
                                try {
                                        logKey = eventLogKey.OpenSubKey (sourceData.LogName, true);
                                        if (logKey == null) {
+                                               ValidateCustomerLogName (sourceData.LogName, 
+                                                       sourceData.MachineName);
+
                                                logKey = eventLogKey.CreateSubKey (sourceData.LogName);
                                                logKey.SetValue ("Sources", new string [] { sourceData.LogName,
                                                        sourceData.Source });
@@ -353,28 +356,22 @@ namespace System.Diagnostics
                        }
                }
 
-               public override EventLog [] GetEventLogs (string machineName)
-               {
-                       using (RegistryKey eventLogKey = GetEventLogKey (machineName, false)) {
-                               if (eventLogKey == null) {
-                                       throw new InvalidOperationException ("TODO");
-                               }
-                               string [] logNames = eventLogKey.GetSubKeyNames ();
-                               EventLog [] eventLogs = new EventLog [logNames.Length];
-                               for (int i = 0; i < logNames.Length; i++) {
-                                       EventLog eventLog = new EventLog (logNames [i], machineName);
-                                       eventLogs [i] = eventLog;
-                               }
-                               return eventLogs;
-                       }
-               }
-
                [MonoTODO]
                protected override string GetLogDisplayName ()
                {
                        return CoreEventLog.Log;
                }
 
+               protected override string [] GetLogNames (string machineName)
+               {
+                       using (RegistryKey eventLogKey = GetEventLogKey (machineName, true)) {
+                               if (eventLogKey == null)
+                                       return new string [0];
+
+                               return eventLogKey.GetSubKeyNames ();
+                       }
+               }
+
                public override string LogNameFromSourceName (string source, string machineName)
                {
                        using (RegistryKey logKey = FindLogKeyBySource (source, machineName, false)) {
@@ -573,12 +570,9 @@ namespace System.Diagnostics
                        // http://www.pinvoke.net/default.aspx/advapi32/LookupAccountSid.html
                        // http://msdn.microsoft.com/library/en-us/secauthz/security/lookupaccountsid.asp
 
-                       // FIXME: StringBuilders should not have to be initialized with a
-                       // specific capacity => bug #79152
-
-                       StringBuilder name = new StringBuilder (16);
+                       StringBuilder name = new StringBuilder ();
                        uint cchName = (uint) name.Capacity;
-                       StringBuilder referencedDomainName = new StringBuilder (16);
+                       StringBuilder referencedDomainName = new StringBuilder ();
                        uint cchReferencedDomainName = (uint) referencedDomainName.Capacity;
                        SidNameUse sidUse;
 
@@ -611,9 +605,6 @@ namespace System.Diagnostics
                        // http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
                        // http://msdn.microsoft.com/netframework/programming/netcf/cffaq/default.aspx
 
-                       // FIXME: we should be using Marshal.StringToHGlobalAuto and 
-                       // Marshal.PtrToStringAuto => bug #79117
-
                        IntPtr msgDllHandle = PInvoke.LoadLibraryEx (msgDll, IntPtr.Zero,
                                LoadFlags.LibraryAsDataFile);
                        if (msgDllHandle == IntPtr.Zero)
@@ -625,8 +616,7 @@ namespace System.Diagnostics
 
                        try {
                                for (int i = 0; i < replacementStrings.Length; i++) {
-                                       // TODO: use StringToHGlobalAuto once bug #79117 is fixed
-                                       arguments [i] = Marshal.StringToHGlobalUni (
+                                       arguments [i] = Marshal.StringToHGlobalAuto (
                                                replacementStrings [i]);
                                }
 
@@ -634,8 +624,7 @@ namespace System.Diagnostics
                                        FormatMessageFlags.FromHModule | FormatMessageFlags.AllocateBuffer,
                                        msgDllHandle, messageID, 0, ref lpMsgBuf, 0, arguments);
                                if (ret != 0) {
-                                       // TODO: use PtrToStringAuto once bug #79117 is fixed
-                                       string sRet = Marshal.PtrToStringUni (lpMsgBuf);
+                                       string sRet = Marshal.PtrToStringAuto (lpMsgBuf);
                                        lpMsgBuf = PInvoke.LocalFree (lpMsgBuf);
                                        // remove trailing whitespace (CRLF)
                                        return sRet.TrimEnd (null);
@@ -652,6 +641,13 @@ namespace System.Diagnostics
                                        }
                                }
                        } finally {
+                               // release unmanaged memory allocated for replacement strings
+                               for (int i = 0; i < arguments.Length; i++) {
+                                       IntPtr argument = arguments [i];
+                                       if (argument != IntPtr.Zero)
+                                               Marshal.FreeHGlobal (argument);
+                               }
+
                                PInvoke.FreeLibrary (msgDllHandle);
                        }
                        return null;
@@ -682,19 +678,21 @@ namespace System.Diagnostics
                        if (hEventLog == IntPtr.Zero) {
                                throw new InvalidOperationException (string.Format (
                                        CultureInfo.InvariantCulture, "Event Log '{0}' on computer"
-                                       + " '{1}' cannot be opened."), new Win32Exception ());
+                                       + " '{1}' cannot be opened.", logName, CoreEventLog.MachineName),
+                                       new Win32Exception ());
                        }
                        return hEventLog;
                }
 
                private IntPtr RegisterEventSource ()
                {
-                       IntPtr hEventLog = PInvoke.OpenEventLog (CoreEventLog.MachineName,
-                               CoreEventLog.Source);
+                       IntPtr hEventLog = PInvoke.RegisterEventSource (
+                               CoreEventLog.MachineName, CoreEventLog.Source);
                        if (hEventLog == IntPtr.Zero) {
                                throw new InvalidOperationException (string.Format (
-                                       CultureInfo.InvariantCulture, "Event Log '{0}' on computer"
-                                       + " '{1}' cannot be opened."), new Win32Exception ());
+                                       CultureInfo.InvariantCulture, "Event source '{0}' on computer"
+                                       + " '{1}' cannot be opened.", CoreEventLog.Source,
+                                       CoreEventLog.MachineName), new Win32Exception ());
                        }
                        return hEventLog;
                }
@@ -710,8 +708,7 @@ namespace System.Diagnostics
                        [DllImport ("advapi32.dll", SetLastError=true)]
                        public static extern int DeregisterEventSource (IntPtr hEventLog);
 
-                       // FIXME: CharSet.Unicode can be removed once bug #79117 is fixed
-                       [DllImport ("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
+                       [DllImport ("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
                        public static extern int FormatMessage (FormatMessageFlags dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, ref IntPtr lpBuffer, int nSize, IntPtr [] arguments);
 
                        [DllImport ("kernel32.dll", SetLastError=true)]
@@ -729,8 +726,7 @@ namespace System.Diagnostics
                        [DllImport ("kernel32.dll", SetLastError=true)]
                        public static extern IntPtr LocalFree (IntPtr hMem);
 
-                       // FIXME: CharSet.Unicode can be removed once bug #79117 is fixed
-                       [DllImport ("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
+                       [DllImport ("advapi32.dll", SetLastError=true)]
                        public static extern bool LookupAccountSid (
                                string lpSystemName,
                                [MarshalAs (UnmanagedType.LPArray)] byte [] Sid,
@@ -746,7 +742,7 @@ namespace System.Diagnostics
                        [DllImport ("advapi32.dll", SetLastError=true)]
                        public static extern IntPtr RegisterEventSource (string machineName, string sourceName);
 
-                       [DllImport ("Advapi32.dll", SetLastError = true)]
+                       [DllImport ("Advapi32.dll", SetLastError=true)]
                        public static extern int ReportEvent (IntPtr hHandle, ushort wType,
                                ushort wCategory, uint dwEventID, IntPtr sid, ushort wNumStrings,
                                uint dwDataSize, string [] lpStrings, byte [] lpRawData);