Using COM objects in .NET

A

Anton Shepelev

Hello all,

What is the right way to work with COM objects in .NET? I
usually do it this way:

public object GetValue( string query ) // analog of ExecuteScalar()
{ object Result = null;
Recordset rs = null;
rs = ( Recordset )cmp.GetBusinessObject( BoObjectTypes.BoRecordset );
try
{ rs.DoQuery( query );
if( rs.RecordCount > 0 )
{ rs.MoveFirst();
Result = rs.Fields.Item(0).Value;
}
}
finally
{ Marshal.ReleaseComObject( rs ); }
return Result;
}

where cmp and rs are COM objects exposed by a library which
is added to the project as a "COMReference". Even with this
pattern, I still sometimes get the error:

The Server threw an exception from HRESULT: 0x80010105
(RPC_E_SERVER_FAULT)

at the moment of creation ( GetBusinessObject() ), and some
have suggested it may be caused by incorrect memory
handling.
 
A

Anders Eriksson

The Server threw an exception from HRESULT: 0x80010105
(RPC_E_SERVER_FAULT)

at the moment of creation ( GetBusinessObject() ), and some
have suggested it may be caused by incorrect memory
handling.

As the error says RPC_E_SERVER_FAULT, it's the server that throws the
exception. I do get this error from time to time and usually it occurs
when I have got a new version of the COM Server but forgot to update my
..NET project.

Strangely I need to remove the reference and add it again to make VS2010
to recognize that the server has changed...

// Anders
 
A

Anton Shepelev

Anders Eriksson:
As the error says RPC_E_SERVER_FAULT, it's the server that
throws the exception. I do get this error from time to
time and usually it occurs when I have got a new version
of the COM Server but forgot to update my .NET project.

A new version of COM Server with the same GUIDs?
Strangely I need to remove the reference and add it again
to make VS2010 to recognize that the server has changed...

Weird indeed -- if the GUIDs have not changed, a rebuild
should help, because it re-generates Interop.* assemplies.

In my case the error occurs on the client's machine, so I'll
check their version of the COM server. Thank for the
suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top