首先。感谢您的任何建议!
我正在处理一个 OpenXML 项目,我需要将文件转换为 *.bin 格式。
一切正常,但是当我运行测试应用程序时,我注意到与应用程序关联的 GDI 对象的数量随着对 OleCreateFromFile(..) 的每次调用而增加
代码:
//init stuff
storage = null;
pOle = null;
var result = OLE32.StgCreateStorageEx(oleOutputFileName,
Convert.ToInt32(OLE32.STGM.STGM_READWRITE | OLE32.STGM.STGM_SHARE_EXCLUSIVE | OLE32.STGM.STGM_CREATE | OLE32.STGM.STGM_TRANSACTED),
Convert.ToInt32(OLE32.STGFMT.STGFMT_DOCFILE),
0,
IntPtr.Zero,
IntPtr.Zero,
ref OLE32.IID_IStorage,
out storage
);
if (!HandleResult("StgCreateStorageEx", result))
return false;
var CLSID_NULL = Guid.Empty;
msOleInterop.FORMATETC f = new msOleInterop.FORMATETC();
//Creates an embedded object from the contents of a named file.
string newInput = MultibyteToUnicodeNETOnly(_inputFileName, 1252);
result = OLE32.OleCreateFromFile(
ref CLSID_NULL,
newInput,
ref OLE32.IID_IOleObject,
(uint)msOleInterop.OLERENDER.OLERENDER_NONE,
ref f,
null,
storage,
out pOle
);
...并且以这种方式发布是不够的。GDI 对象仍在增加(每个 OleCreateFromFile 调用大约 +2。
//Object release:
Marshal.ReleaseComObject(storage);
Marshal.ReleaseComObject(pOle);
OleCreateFromFile 的定义
#region OleCreateFromFile
[DllImport("ole32.dll")]
public static extern int OleCreateFromFile([In] ref Guid rclsid,
[MarshalAs(UnmanagedType.LPWStr)] string lpszFileName,
[In] ref Guid riid,
uint renderopt,
[In] ref FORMATETC pFormatEtc/*IntPtr pFormatEtc*/,
IOleClientSite pClientSite,
Microsoft.VisualStudio.OLE.Interop.IStorage pStg,
out Microsoft.VisualStudio.OLE.Interop.IOleObject ppvObj);
#endregion