com-interop

How to get type of COM object

亡梦爱人 提交于 2019-11-27 10:32:54
问题 I am referencing a COM library in Visual Studio, so it has automatically created the corresponding Interop assembly for me. I would like to do a GetType() on these com objects, but they always return System.__ComObject . Querying them for an interface works though: bool isOfType = someComeObject is ISomeComObject; //this works But what I really want is this to return the actual type of the com object: Type type = someComeObject.GetType(); //returns System.__ComObject :-( Does anyone know how

Handling events from out-of-proc COM server in managed STA application

江枫思渺然 提交于 2019-11-27 09:33:48
Apparently, managed handlers for events, sourced from an unmanaged out-of-process COM server, are called back on a random pool thread, rather than on the main STA thread (as I'd expect). I've discovered this while answering a question on Internet Explorer automation . In the code below, DocumentComplete is fired on a non-UI thread (so "Event thread" is not the same as "Main thread" in the debug output). Thus, I have to use this.Invoke to show a message box. To the best of my knowledge, this behavior is different from unmanaged COM clients, where events subscribed to from an STA thread are

How to make make a .NET COM object apartment-threaded?

拈花ヽ惹草 提交于 2019-11-27 08:55:46
.NET objects are free-threaded by default. If marshaled to another thread via COM, they always get marshaled to themselves, regardless of whether the creator thread was STA or not, and regardless of their ThreadingModel registry value. I suspect, they aggregate the Free Threaded Marshaler (more details about COM threading could be found here ). I want to make my .NET COM object use the standard COM marshaller proxy when marshaled to another thread. The problem: using System; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows

Why is a value copy of MainForm created when method is called or invoked cross thread?

北战南征 提交于 2019-11-27 08:33:35
问题 Update: I think it has something to do with lazy instantiation of the window handle for MainForm - but haven't been able to work out quite how that would result in the behavior seen here. The application requests data via 3rd party COM interface providing a callback to process the results. In the callback, the UI needs to be updated - but the update doesn't work as expected. It's as if a value copy of MainForm had been created, when MainForm.DataReady is called or invoked directly cross

Exposing Property as Variant in .NET for Interop

a 夏天 提交于 2019-11-27 08:29:36
I am creating a wrapper class in .NET (VB.NET as it happens but is equally related to C#) that is exposed to COM and one of the properties I am trying to wrap is a Variant. I thought I would just be able to use an Object, but I get an error: Public Property FieldValue([vFieldID As Object = -1]) As Object cannot be exposed to COM as a property 'Let'. You will not be able to assign non-object values (such as numbers or strings) to this property from Visual Basic 6.0 using a 'Let' statement.* My property declaration looks like this: Public Property FieldValue(Optional ByVal vFieldID As Object =

What are alternatives to generic collections for COM Interop?

青春壹個敷衍的年華 提交于 2019-11-27 08:28:19
I am attempting to return a collection of departments from a .NET assembly to be consumed by ASP via COM Interop. Using .NET I would just return a generic collection, e.g. List<Department> , but it seems that generics don't work well with COM Interop. So, what are my options? I would like to both iterate over the list and be able to access an item by index. Should I inherit from List<Department> , implement an IList , IList<Department> or another interface, or is there a better way? Ideally I would prefer not to have to implement a custom collection for every type of list I need. Also, will

Releasing temporary COM objects

谁都会走 提交于 2019-11-27 08:25:29
Consider the following C# code using a COM object. MyComObject o = new MyComObject; try { var baz = o.Foo.Bar.Baz; try { // do something with baz } finally { Marshal.ReleaseComObject(baz); } } finally { Marshal.ReleaseComObject(o); } This will release the COM objects o and baz , but not the temporary objects returnd by o.Foo and o.Foo.Bar . This can cause problems, when those objects hold a large amount of unmanaged memory or other resources. An obvious but ugly solution would be, to clutter the code even more with try-finally and Marshal.ReleaseComObject . See C# + COM Interop, deterministic

How to elegantly prevent a webservice proxy from being exposed to COM?

孤街醉人 提交于 2019-11-27 08:13:56
问题 I have a C# assembly that I use as an in-proc COM server consumed by an unmanaged C++ application. The assembly consumes a webservice that will not ever change so there's no need to ever update the webservice proxy classes. That's why the proxy classes are created once and Reference.cs files are simply put into the repository and only compiled from there. The problem is that by default the webservice proxy classes are public and are therefore exposed to COM. This inflates the typelib and

What does “Register for COM Interop” actually do?

心不动则不痛 提交于 2019-11-27 08:10:42
What exactly does the VS project option "Register for COM interop" actually do? Because when I build my library with this option enabled I can call CreateObject on my library from VBScript. But if I build without this and then run regasm manually CreateObject fails. So I'm wondering -- what does VS2010 do that I'm not doing? It does the same thing as running Regasm.exe with the /tlb and /codebase options. The /codebase option is probably the one you forgot. Regasm likes assuming you put the DLL in the GAC and generates a warning when you don't. The GAC is indeed a very good way to avoid DLL

Determining .Net method suffix number in VBScript (COM-interop)

孤街浪徒 提交于 2019-11-27 08:01:34
问题 You can use .NET methods through COM-interop in VBScript. You have to append a certain suffix number to the method since overloads don't cross the managed/unmanaged boundary. The suffix number doesn't seem to have a particular order...how is the suffix number determined? Example: Dim encoding, bytesthroughdotnet Set encoding = CreateObject("System.Text.UTF8Encoding") bytesthroughdotnet = encoding.GetBytes_4("你好Ğ") 'get bytes WScript.Echo LenB(bytesthroughdotnet) 'length Set encoding = Nothing