C#/COM interop working only in debugger

1

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:

  1. I tried running the application as administrator, and also with UAC turned off. Same result.
  2. I tried attaching to the process before the error occurs but I saw no indication in the output window of a structured exception being thrown.
  3. I tried explicitly setting the apartment model for the test app's main thread to STA and then MTA, but this made no difference. The COM component is apartment threaded.
  4. I tried running the managed test application (outside visual studio) on a Win2003 machine and a different Win7 pro machine. It worked correctly on both.

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.]

c#
com
interop
asked on Stack Overflow Mar 2, 2011 by Andy Johnson • edited Nov 2, 2015 by Drew Gaynor

2 Answers

1

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?

answered on Stack Overflow Mar 3, 2011 by erikkallen
0

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.

answered on Stack Overflow Apr 29, 2011 by Petro

User contributions licensed under CC BY-SA 3.0