I have a problem with COM interop from a C# application to an inproc COM server component.
I have reduced the problem to a simple c# test program. It instantiates the server component's interop class, sets the value of a string a property on the instance, and then sets another string property. I'm not doing anything unusual with marshalling. Just using the interop class generated when I added a reference to the COM component. Something like:
using MyLib; // Interop assy
// ...
MyComp comp = new MyComp();
comp.Prop1 = "abc";
comp.Prop2 = "xyz";
Outcomes:
If I run the test program outside VS then, when the second property is set, I consistently get a COMException with an HRESULT of 0x80010105
(RPC_E_SERVERFAULT
).
If I run the test program inside Visual Studio 2005 then it consistently works correctly.
I wrote equivalent code in unmanaged C++ (no atl, just plain interface pointers) and this works correctly both in and out of VS.
My question: what is different about the environment in which interop occurs when running inside the debugger that might account for what I'm seeing? I assume that the E_RPC_SERVERFAULT
is being generated by the interop marshaller, but why only in the debugger? Any suggestions for how to proceed with this?
[I haven't said much about the com component because my question is about the different behaviour I'm seeing in and out of the debugger. The com component is well tested and has been in production use for over 7 years (thousands of invocations per day in a server environment) so I'm quite confident in it. Its a 32 bit dll. I have the source, but don't currently have a suitable build environment for it. Even if I did, I'm not sure how to debug into it from managed code. Is this possible?]
UPDATE
No solution yet, but some more observations:
The last item points to an issue with my machine, but I'm not sure what.
[The environment is Framework 2.0 running on Win7 Pro 32 bit with Visual Studio 2005 Pro.]
Is it possible that you are running an AnyCPU build, and for some reason Visual Studio loads the assembly as 32bit, but when you run the program outside VS, it runs as x64 and thus cannot load a 32-bit COM dll?
I've got the same type of issue. In my case forcing .NET application to be 32-bit and making
[STAThread]
public static int Main(string [] args)
or (for threads)
thread = new Thread(DoWorkUsingCOM);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
solved the problem.
User contributions licensed under CC BY-SA 3.0