Tag: interop

IDispatchEx在哪里存在?

找不到包含IDispatchEx接口的库。 我想实现这个接口,但找不到它。 有谁知道它在哪里? 谢谢,保罗。

DisposableAction和Marshal.ReleaseComObject

做一些办公室Interop,代码是Type try { selection = getSelected(Return.Some.Office.InteropObject); for ( int i = 0 ; i < selection.count ; i++) yield return selection.item(i) } finally { Marshal.ReleaseComObject(selection); } 想知道用DisposableAction替换它是否是个好主意并改为 using ( var a = new DisposableAction(getSelected(Return.Some.Office.InteropObject)) ) { foreach(var b in a.Items) yield return b; }

来自System.Windows.Forms的AccessViolationException,在WPF中使用WinFormsHost

在开发WPF / WinForms互操作应用程序时,我遇到了一个令人讨厌的问题。 我一直试图解决这个问题三天,但我无法取得任何进展。 我怀疑我能提供足够的信息来获得解决方案,但我正在寻找能够解释这里究竟发生了什么的人吗? 我使用的组件是AxMapControl(ESRI ArcGIS Engine 9.3.1 SP2),据我所知,它是COM包装的本机代码,公开为WinForms控件。 该组件使用WPF WinFormsHost代理嵌入在我们的WPF(.NET 3.5)客户端软件中。 应用程序会定期使用AccessViolationException崩溃。 这总是发生在用户鼠标点击地图控件时,但似乎没有任何押韵或原因在什么具体输入。 堆栈跟踪始终相同: System.AccessViolationException:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。 在System.Windows.Forns.Forms.Forf.DefWndProc上的System.Windows.Forms.NativeWindow.DefWndProc(Message&m)处的System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,IntPtr hWnd,Int32 msg,IntPtr wParam,IntPtr lParam) (Message&m)System.Windows.Fornd.OndMessage(Message&m)位于System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)的System.Windows.Forms.AxHost.WndProc(Message&m)处的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 因为exception似乎抛出了我的代码启动的任何调用堆栈之外,我无法弄清楚如何捕获exception并以编程方式处理它。 此问题发生在调试模式以及发布版本中。 但它确实不会出现在所有计算机上,但我能够在Windows 7和XP以及.NET framework 3.5和4.0上复制此问题。 在崩溃时检查进程是什么,exception似乎是在GAC部署的DLL上似乎有多个CreateFileMapping操作失败,结果FILE LOCKED WITH ONLY READERS 。 此视图已被过滤以仅显示该类型的结果,但似乎每个DLL恰好发生两次。 这是什么意思吗? 现在,很明显我对发生的事情毫无头绪,以及如何解决这个问题。 如果你有一个线索,你能否善待并向我解释我正在处理的问题类型? 知道如何调试这个问题吗?

“P / Invoke入口点应该存在”,应该指出正确的入口点

我从Visual Studio 2012中的代码分析工具收到此警告。代码如下所示: using System; using System.Runtime.InteropServices; namespace MyProgramNamespace { class NativeMethods { [DllImport(“user32.dll”, EntryPoint = “GetWindowLongPtr”)] public static extern IntPtr GetWindowLongPtr(IntPtr handle, int flag); [DllImport(“user32.dll”, EntryPoint = “SetWindowLongPtr”)] public static extern IntPtr SetWindowLongPtr(IntPtr handle, int flag, IntPtr ownerHandle); } } 我正在编译x64,所以我不关心使用旧的GetWindowLong和SetWindowLong。 据我所知,这些入口点名称是正确的。 编辑:已解决。 原来问题是Visual Studio本身(因此代码分析工具)是32位。 当代码分析工具检查user32.dll以查看是否存在这些函数时,它会检查32位版本的user32.dll(在C:/ Windows / SysWOW64 /中)而不是程序实际使用的版本(64位版本)在C:/ Windows / System32中,这些函数只存在于64位版本中(32位版本使用GetWindowLong / […]

什么是String 以及如何投射它?

我目前处于不幸的位置,不得不通过COM Interop从C#调用VBA宏。 宏返回一个字符串数组,看起来像: Public Function Foo() As String() 然后我试图在C#中转换为一个字符串数组,如下所示: var result = (string[])xlApp.Run(“Foo”, … missing args …) 然后导致运行时错误: 无法将类型为’System.String [*]’的对象强制转换为’System.String []’。 有谁知道String [*]是什么,你是否知道如何将它转换为C#string []? 这个演员在我创建的一个简单的测试用例中工作。 我能看到的唯一区别是,在简单的情况下,我在VBA中的数组是字符串类型(0到5),而在更复杂的情况下,它是字符串类型(1到6)。 这可能是原因,如果是这样,我的VBA相当生疏 – 我该如何解决? 非常感谢, 乔恩

PInvoke:用C ++分配内存并在C#中释放它

我们正在使用PInvoke在C#和C ++之间进行交互。 我有一个如下的互操作结构,另一边有相同的布局C ++结构。 [StructLayout(LayoutKind.Sequential)] public struct MeshDataStruct : IDisposable { public MeshDataStruct(double[] vertices, int[] triangles , int[] surfaces) { _vertex_count = vertices.Length / 3; _vertices = Marshal.AllocHGlobal(_vertex_count*3*sizeof (double)); Marshal.Copy(vertices, 0, _vertices, _vertex_count); } // .. extract data methods to double[] etc. private IntPtr _vertices; private int _vertex_count; public void Dispose() { if (_vertices != […]

无法在C#中实例化COM组件 – 错误80070002

我正在尝试在我的机器上实例化Windows Media Player COM对象: Guid mediaPlayerClassId = new Guid(“47ac3c2f-7033-4d47-ae81-9c94e566c4cc”); Type mediaPlayerType = Type.GetTypeFromCLSID(mediaPlayerClassId); Activator.CreateInstance(mediaPlayerType); // <– this line throws 执行最后一行时,我收到以下错误: System.IO.FileNotFoundException was caught Message=”Retrieving the COM class factory for component with CLSID {47AC3C2F-7033-4D47-AE81-9C94E566C4CC} failed due to the following error: 80070002.” Source=”mscorlib” StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) […]

COMexception80040154创建Excel应用程序时

我正在尝试在没有安装Office的服务器上运行我的应用程序。 using EXCEL = Microsoft.Office.Interop.Excel; … EXCEL.Application app = new EXCEL.Application();//Exception thrown here 代码在我自己的系统上工作正常,但在服务器上它提供以下exception: Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {…} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 两个系统都是32位,我在应用程序的exe旁边复制了excel Interop dll。 我还安装了O2010PIA 。 任何铅?

无法转换“Microsoft.Office.Interop.PowerPoint.ApplicationClass”类型的COM对象

无法将“Microsoft.Office.Interop.PowerPoint.ApplicationClass”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.PowerPoint._Application” 。 此操作失败,因为对于具有IID“{91493442-5A91-11CF-8700-00AA0060263B}”的接口的COM组件的QueryInterface调用由于以下错误而失败:库未注册。 (HRESULT的例外情况:0x8002801D(TYPE_E_LIBNOTREGISTERED))。 我无法找到解决方案,请帮忙吗?

C#项目设置“允许不安全的代码”的实际function是什么

我想知道C#项目设置“允许不安全代码”是否仅适用于项目本身的不安全C#代码,还是在链接本机C ++ DLL时是否需要设置此选项? 如何链接自身链接到本机DLL的托管DLL? 这个选项在引擎盖下真正做了什么?