Skip to content

Commit

Permalink
- Fixed releasing of unmanaged resources
Browse files Browse the repository at this point in the history
- Memory usage improvements
- Added more details to log file
  • Loading branch information
tonyfederer committed Jan 31, 2019
1 parent 553bdaf commit 54edd56
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 128 deletions.
178 changes: 85 additions & 93 deletions OutlookFileDrag/DataObjectHelper.cs

Large diffs are not rendered by default.

19 changes: 4 additions & 15 deletions OutlookFileDrag/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ static class NativeMethods
public const int S_FALSE = 1;

public const short CF_TEXT = 1;
public const short CF_HDROP = 15;
public const short CF_UNICODETEXT = 13;
public const short CF_HDROP = 15;

public const int DATA_S_SAMEFORMATETC = 0x00040130;

Expand All @@ -37,34 +37,23 @@ static class NativeMethods
public const int E_UNSPEC = E_FAIL;
public const int E_UNEXPECTED = unchecked((int)0x8000FFFF);

public const int MK_LBUTTON = 0x0001;
public const int MK_RBUTTON = 0x0002;

public const int OLE_E_NOTRUNNING = unchecked((int)0x80040005);
public const int OLE_S_USEREG = unchecked((int)0x80040005);

public const int STG_E_MEDIUMFULL = unchecked((int)0x80030070);

public const int MAX_PATH = 260;

[DllImport("ole32.dll")]
public static extern int DoDragDrop(NativeMethods.IDataObject pDataObj, IntPtr pDropSource, uint dwOKEffects, out uint pdwEffect);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GlobalLock(HandleRef handle);
public static extern IntPtr GlobalLock(IntPtr handle);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool GlobalUnlock(HandleRef handle);
public static extern bool GlobalUnlock(IntPtr handle);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GlobalSize(HandleRef handle);
public static extern int GlobalSize(IntPtr handle);

[DllImport("ole32.dll", PreserveSig = false)]
public static extern ILockBytes CreateILockBytesOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease);

[DllImport("ole32.dll", CharSet = CharSet.Auto, PreserveSig = false)]
public static extern IntPtr GetHGlobalFromILockBytes(ILockBytes pLockBytes);

[DllImport("ole32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern IStorage StgCreateDocfileOnILockBytes(ILockBytes plkbyt, uint grfMode, uint reserved);

Expand Down
9 changes: 7 additions & 2 deletions OutlookFileDrag/OutlookDataObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public OutlookDataObject(NativeMethods.IDataObject innerData)

public int EnumFormatEtc(DATADIR direction, out IEnumFORMATETC ppenumFormatEtc)
{
IEnumFORMATETC origEnum = null;
try
{
log.DebugFormat("IDataObject.EnumFormatEtc called -- direction {0}", direction);
switch (direction)
{
case DATADIR.DATADIR_GET:
//Get original enumerator
IEnumFORMATETC origEnum;
int result = innerData.EnumFormatEtc(direction, out origEnum);
if (result != NativeMethods.S_OK)
{
Expand All @@ -48,7 +48,6 @@ public int EnumFormatEtc(DATADIR direction, out IEnumFORMATETC ppenumFormatEtc)
if (cfFormat != NativeMethods.CF_TEXT && cfFormat != NativeMethods.CF_UNICODETEXT && cfFormat != (ushort)DataObjectHelper.GetClipboardFormat("Csv"))
formats.Add(buffer[0]);
}
Marshal.ReleaseComObject(origEnum);

//Add CF_HDROP format
FORMATETC format = new FORMATETC();
Expand Down Expand Up @@ -79,6 +78,12 @@ public int EnumFormatEtc(DATADIR direction, out IEnumFORMATETC ppenumFormatEtc)
ppenumFormatEtc = null;
return NativeMethods.E_UNEXPECTED;
}
finally
{
//Release all unmanaged objects
if (origEnum != null)
Marshal.ReleaseComObject(origEnum);
}
}

public int GetCanonicalFormatEtc(ref FORMATETC formatIn, out FORMATETC formatOut)
Expand Down
4 changes: 2 additions & 2 deletions OutlookFileDrag/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.0")]
[assembly: AssemblyFileVersion("1.0.5.0")]
[assembly: AssemblyVersion("1.0.8.0")]
[assembly: AssemblyFileVersion("1.0.8.0")]

12 changes: 8 additions & 4 deletions OutlookFileDrag/ThisAddIn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using log4net;

namespace OutlookFileDrag
Expand All @@ -7,7 +8,7 @@ public partial class ThisAddIn
{
private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private System.Threading.Timer cleanupTimer;
private DragDropHook hook { get; set; }
private DragDropHook hook;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Expand All @@ -17,9 +18,12 @@ private void ThisAddIn_Startup(object sender, System.EventArgs e)
try
{
log.Info("Add-in startup");
log.DebugFormat("OS: {0} {1}", Environment.OSVersion, Environment.Is64BitOperatingSystem ? "x64" : "x86");
log.DebugFormat("Outlook version: {0} {1}", this.Application.Version, Environment.Is64BitProcess ? "x64" : "x86");
log.DebugFormat("Language: {0}", Application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

//Log version, OS version, Outlook version, and language
log.InfoFormat("Version: {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());
log.InfoFormat("OS: {0} {1}", Environment.OSVersion, Environment.Is64BitOperatingSystem ? "x64" : "x86");
log.InfoFormat("Outlook version: {0} {1}", this.Application.Version, Environment.Is64BitProcess ? "x64" : "x86");
log.InfoFormat("Language: {0}", Application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

//Set up exception handlers
System.Windows.Forms.Application.ThreadException += Application_ThreadException;
Expand Down
12 changes: 6 additions & 6 deletions OutlookFileDragSetup/OutlookFileDragSetup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -690,23 +690,23 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:Outlook File Drag"
"ProductCode" = "8:{7EA6E17B-8802-4E1F-9669-248670B31BFB}"
"PackageCode" = "8:{F3D1EFE7-A3F0-4F4D-8A81-FDB5D70121D8}"
"ProductCode" = "8:{F198B620-5A06-4AE0-883E-B948E8DEEB3A}"
"PackageCode" = "8:{E5ACAFF7-42AA-4A04-93FB-EF1BA5CEF265}"
"UpgradeCode" = "8:{2F626147-8F83-4FC1-9190-32AA4F25D487}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:1.0.5"
"ProductVersion" = "8:1.0.8"
"Manufacturer" = "8:Tony Federer"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
"ARPHELPLINK" = "8:https://github.com/tonyfederer/OutlookFileDrag"
"Title" = "8:Outlook File Drag Setup"
"Subject" = "8:"
"ARPCONTACT" = "8:Tony Federer"
"Keywords" = "8:"
"ARPCOMMENTS" = "8:Drag Outlook items as files into any application"
"ARPCOMMENTS" = "8:Drag and drop Outlook items as files into any application"
"ARPURLINFOABOUT" = "8:"
"ARPPRODUCTICON" = "8:"
"ARPIconIndex" = "3:0"
Expand Down Expand Up @@ -1496,7 +1496,7 @@
{
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_54EF5F74A56C4AEE96D4330B540B4A3A"
{
"SourcePath" = "8:"
"SourcePath" = "8:..\\OutlookFileDrag\\obj\\Debug\\OutlookFileDrag.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_919F44B827474C528E0E9FF4CAE2B9AE"
Expand Down
12 changes: 6 additions & 6 deletions OutlookFileDragSetup_x64/OutlookFileDragSetup_x64.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -716,23 +716,23 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:Outlook File Drag"
"ProductCode" = "8:{CF5F9043-967C-400D-B6D5-F41AF6AD83AE}"
"PackageCode" = "8:{6FCB2E29-100A-48F9-85B5-36FB5D041034}"
"ProductCode" = "8:{4D17A106-6F0C-469A-BC97-9B71E79B3CA8}"
"PackageCode" = "8:{EBCA38DE-16CA-486D-AF62-B375FD13A4B5}"
"UpgradeCode" = "8:{65870D9B-6652-4150-830B-C5199F26E62C}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:1.0.5"
"ProductVersion" = "8:1.0.8"
"Manufacturer" = "8:Tony Federer"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
"ARPHELPLINK" = "8:https://github.com/tonyfederer/OutlookFileDrag"
"Title" = "8:Outlook File Drag Setup (x64)"
"Subject" = "8:"
"ARPCONTACT" = "8:Tony Federer"
"Keywords" = "8:"
"ARPCOMMENTS" = "8:Drag Outlook items as files into any application"
"ARPCOMMENTS" = "8:Drag and drop Outlook items as files into any application"
"ARPURLINFOABOUT" = "8:"
"ARPPRODUCTICON" = "8:"
"ARPIconIndex" = "3:0"
Expand Down Expand Up @@ -1522,7 +1522,7 @@
{
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_54EF5F74A56C4AEE96D4330B540B4A3A"
{
"SourcePath" = "8:..\\OutlookFileDrag\\obj\\Release\\OutlookFileDrag.dll"
"SourcePath" = "8:..\\OutlookFileDrag\\obj\\Debug\\OutlookFileDrag.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_919F44B827474C528E0E9FF4CAE2B9AE"
Expand Down

0 comments on commit 54edd56

Please sign in to comment.