// Allocates a new string on the unmanaged heap, and stores the pointer // so we can free it later private IntPtr MakeNewString(string s, NativeMethods.TASKDIALOG_ELEMENTS element) { IntPtr newStringPtr = Marshal.StringToHGlobalUni(s); updatedStrings[(int)element] = newStringPtr; return(newStringPtr); }
// Checks to see if the given element already has an updated string, and if so, // frees it. This is done in preparation for a call to MakeNewString(), to prevent // leaks from multiple updates calls on the same element within a single native dialog lifetime. private void FreeOldString(NativeMethods.TASKDIALOG_ELEMENTS element) { int elementIndex = (int)element; if (updatedStrings[elementIndex] != IntPtr.Zero) { Marshal.FreeHGlobal(updatedStrings[elementIndex]); updatedStrings[elementIndex] = IntPtr.Zero; } }
private void UpdateTextCore(string s, NativeMethods.TASKDIALOG_ELEMENTS element) { AssertCurrentlyShowing(); FreeOldString(element); SendMessageHelper( NativeMethods.TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT, (int)element, (long)MakeNewString(s, element)); }