someone told me, don't do that. Framework should free the com reference for you. but...
if you are writing a AddIn for PowerPoint, you might have seen the same problem. That is, if you reference a element of GroupItems without release it immediately, next time, you group it into bigger group, and trying to reference the GroupItems of the biggger group, PowerPoint totally screwed up. Here is the common solution for such kind of situation.
The idea is using your own object to wrap RCW(raw com wrapper). In the constructor, you register yourself in the ComObjectsPool. When you want to clean things up, clean com objects from the pool.
public interface IComObject : IDisposable
{
bool Release();
}
public abstract class ComObject : IComObject
{
private bool shouldRelease = true;
public abstract void Dispose();
public ComObject()
{
ComObjectsPool.PutIn(this);
}
public bool Release()
{
if (shouldRelease)
{
Dispose();
return true;
}
return false;
}
public bool ShouldRelease
{
get { return shouldRelease; }
set { shouldRelease = value; }
}
}
public static class ComObjectsPool
{
private static readonly List<IComObject> comObjects = new List<IComObject>();
public static void PutIn(IComObject comObject)
{
comObjects.Add(comObject);
}
public static void ReleaseAll()
{
List<IComObject> unreleasedComObjects = new List<IComObject>();
foreach (IComObject comObject in comObjects)
{
if (!comObject.Release())
{
unreleasedComObjects.Add(comObject);
}
}
comObjects.Clear();
comObjects.AddRange(unreleasedComObjects);
}
}
public class AdaptedShape : ComObject, IShape
{
private readonly Shape shape;
public AdaptedShape(Shape shape)
{
this.shape = shape;
}
public override void Dispose()
{
Marshal.ReleaseComObject(shape);
}
}
分享到:
相关推荐
PtrToStructure,将数据从非托管内存块封送到新分配的指定类型的托管对象 应用:MFC发送消息到WPF界面上 public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) ...
1. 接口与委托互转:Marshal.GetFunctionPointerForDelegate和Marshal.CreateDelegate方法可以实现接口和委托之间的转换,这对于调用非托管DLL函数非常有用。 2. 引用类型转换:Marshal.IsTypeVisibleFromCom和...
代码如下:System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject...
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) {...
"strazegy-marshal.rar_The Network" 提供了一系列与神经网络控制相关的代码示例,是学习和研究神经网络控制策略的重要资源。以下是对这些文件的详细解读: 1. **tareadme.fst**:通常这是一个项目或软件的说明文件...
本主题主要关注`pyc-grammar`,这是一个与从`marshal.c`检索`.pyc`字节码的语法描述相关的项目。 `marshal.c`是Python标准库中的一个源文件,它实现了Python的数据序列化和反序列化的功能。在`.pyc`文件的生成过程...
在"marshal.rar"中,可能包含了一个简单的示例,演示了如何在C++或C#等编程语言中实现这个过程,包括注册服务器、创建代理、设置接口指针、进行数据封送和解封送等关键步骤。学习这个例子有助于深入理解COM组件跨...
Dim lParam As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cds)) Marshal.StructureToPtr(cds, lParam, False) SendMessage(hWndReceiver, &H4A, 0, lParam) ' WM_COPYDATA 消息码 Marshal.FreeHGlobal(lParam) ...
在这个方法中,`Marshal.PtrToStructure`用来将指针所指向的内存中的数据转换为对应的结构体实例。 ### 总结 通过以上介绍和示例代码,我们可以看到如何在C#中实现结构体与字节数组之间的相互转换。这种方法在TCP...
这里的`BytesToStruct`方法接受一个字节数组和结构体的`Type`对象,首先检查字节数组的长度是否足够,然后分配内存,将字节数组的内容拷贝到内存,接着使用`Marshal.PtrToStructure`将内存中的数据转换为指定类型的...
TR objStruct = (TR)Marshal.PtrToStructure(ipSturct, typeof(TR)); Marshal.FreeHGlobal(ipSturct); return objStruct; } ``` 这两个方法可以实现结构体与byte[]的互相转换,方便在C#与C++程序之间进行数据交换...
EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg)); if (wParam == 0x100)//WM_KEYDOWN=0x100 { barCode.VirtKey = msg.message & 0xff;//虚拟吗 barCode.ScanCode = msg.paramL & 0...
可以先将结构体实例转换为`IntPtr`,然后通过`Marshal.PtrToStructure`和`Marshal.Copy`来传递和接收数据。例如,创建一个新的`point`实例,将其值赋给`p1`,然后使用中间的`IntPtr`数组来处理`p2`: ```csharp ...
IntPtr structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WavHeader))); Marshal.Copy(bytes, 0, structPtr, Marshal.SizeOf(typeof(WavHeader))); WavHeader header = (WavHeader)Marshal....
IntPtr structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Operator))); Marshal.Copy(bytes, 0, structPtr, Marshal.SizeOf(typeof(Operator))); Operator operatorObj = (Operator)Marshal....
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(p)); try { // 将结构体数据复制到非托管内存 Marshal.StructureToPtr(p, ptr, false); // 读取非托管内存中的数据 PERSON pFromPtr = (PERSON)Marshal....
Console.WriteLine("设置系统时间失败,错误代码: " + Marshal.GetLastWin32Error()); } else { Console.WriteLine("系统时间已成功设置为: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } } ``` ...
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI)); // Converting structure to IntPtr Marshal.StructureToPtr(struct_IPI, intptrStruct, true); bool iReturn = ...
Haskell库用于解析使用Marshal.dump序列化的Ruby对象的子集。 支持的类型 NilClass TrueClass | FalseClass Array Fixnum Float Hash String Symbol 如果您想增加对其他类型的支持,请随时使用以下准则创建...
IntPtr intptr = Marshal.AllocHGlobal(Marshal.SizeOf(vga)); Marshal.StructureToPtr(vga, intptr, true); // 将结构体复制到非托管内存 // 在此发送 intptr 指针给目的方 Marshal.FreeHGlobal(intptr); // 释放...