com-interop

SetWindowsHookEx returns 0 when compiling for the .NET 4.0 framework in 32bit machines

送分小仙女□ 提交于 2019-11-29 04:26:51
I'm trying to set a low level windows keyboard hook to grab three keys pressed even if the application is not in focus. To do this I'm calling SetWindowsHookEx as // Create an instance of HookProc. KeyboardHookProcedure = new HookProc(KeyboardHookProc); //install hook hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. if (hKeyboardHook == 0) { //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute

Why would I use “Both” COM threading model instead of “Free”?

筅森魡賤 提交于 2019-11-29 01:52:44
According to this article if I register my COM object with either "Both" or "Free" threading model that object must be completely thread-safe. Specifically all accesses to global shared variables must be synchronized and all accesses to member variables must also be synchronized. That's a lot of effort. Now I understand that being able to register my object as using "Free" threading model is advantageous and might be worth paying the price of making it completely thread-safe. But why would I want to do all the same and register my object using "Both" threading model instead? What would be the

Is the 'Implemented Categories' key needed when registering a Managed COM Component?

一曲冷凌霜 提交于 2019-11-29 01:29:15
When registering a managed class for COM Interop by hand, certain registry keys are needed. For example HKEY_CLASSES_ROOT CLSID\[My Cls Id] InprocServer32 (Default) = "mscoree.dll" Assembly = [My assembly name] etc. I've noticed that when VS registers a library for COM Interop, it also adds a key HKEY_CLASSES_ROOT CLSID\[My Cls Id] Implemented Categories {62C8FE65-4EBB-45e7-B440-6E39B2CDBF29} What is this key for, and is it absolutely necessary? As far as I can tell, life goes on just fine without it, but maybe I'm not encountering the circumstances where it is needed. It is a CATID, a

Converting between 2 different libraries using the same COM interface in C#

女生的网名这么多〃 提交于 2019-11-29 00:10:34
I have a pair of libraries that both use the same COM interface. In one library I have a class that implements that interface. The other library requires an object that implements the interface. However both libraries have their own definition of the interface. Both are slightly different but essentially the same interface. So I try to case between them as follows: Library2.Interface intf = (Library2.Interface)impl; but this raises as an exception. If I do the following: Library1.Interface intf = (Library1.Interface)impl; Then it casts without problem but I am no longer able to pass the class

Registering COM referenced DLLs on a build server

拟墨画扇 提交于 2019-11-28 21:31:45
We're developing a C# application that references a few COM libraries (AutoIT for example). I am including all referenced components under source control, in a 3rd party "Libs" folder. The problem is that COM dll's don't have a HintPath property in the .csproj file, and i assume these must be manually registered using regsvr32 (or using a script of some sort). I am currently looking into creating an MSBuild script that will run before every build, however i couldn't figure out if i should be manually calling regsvr32.exe or use some predefined MSBuild task? Currently, this is what i've

An MTA Console application calling an STA COM object from multiple threads

旧城冷巷雨未停 提交于 2019-11-28 20:48:56
Although there are many questions about COM and STA/MTA (e.g. here ), most of them talk about applications which have a UI. I, however, have the following setup: A console application, which is by default Multi-Threaded Apartment (Main() explicitly has the [MTAThread] attribute). The main thread spawns some worker threads. The main thread instantiates a single-threaded COM object. The main thread calls Console.ReadLine() until the user hits 'q', after which the application terminates. A few questions: Numerous places mentions the need of a message pump for COM objects . Do I need to manually

Marshal.GetActiveObject() throws MK_E_UNAVAILABLE exception in C#

半腔热情 提交于 2019-11-28 20:42:14
The following vbscript code works prefectly fine: Dim App Set App = GetObject("","QuickTest.Application") App.Quit But when I translate it into C# code as below: class Program { [STAThread] static void Main(string[] args) { object qtApp = Marshal.GetActiveObject("QuickTest.Application"); (qtApp as QuickTest.Application).Quit(); } } I get the exception: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) I don't think the problem is related to ROT, because the

How to get type of COM object

99封情书 提交于 2019-11-28 17:31:50
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 to do what I want to do? Add reference to Microsoft.VisualBasic.dll and then: Microsoft.VisualBasic

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

做~自己de王妃 提交于 2019-11-28 14:15: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 pollutes registry. Changing visibility to internal break the assembly, so those entities need to remain

C# Excel interop, put a line break in the text of a cell in a Excel

女生的网名这么多〃 提交于 2019-11-28 13:35:39
I am writing an Excel sheet using interop. In the sheet I need to put a set of sentences in to a cell. The text should be in a line break manner. How can I achieve this? Thanks in advance. It was done by either entering "\r\n" or Environment.NewLine. And also must remember to make WrapText property true in order to visible the line brakes. you can add style to cells programically as bellow worksheet.Cells.Style.WrapText = true; New line within an Excel cell is the LF character, which is "\n" in C#. And do not forget to set the WrapText property of the cell to TRUE. In VB , or VBA , I used to