Hello,
I am trying to access a out of proc DCOM server from a C# client. I need
help on the following:
1) How to get the out value from the out-of proc server?
2) How to get the HRESULT value returned by any interface call?
Below is a detail description about the problem:
I need to access a DCOM component using a C# client. This is possible by
adding a reference or by using Type and Activator. The DCOM server is
running on a different PC, so it can't be accesses by adding a reference.
Hence I am using Type and Activator as follow
static void TestMathServer()
{
try
{
Type objType =
Type.GetTypeFromProgID("MathServer.MathOperations", "PC_MathServer");
if (objType == null)
Console.WriteLine("Failed to get object");
object objMathServer = Activator.CreateInstance(objType);
int nTemp = -1;
pValue = (IntPtr)nTemp;
object[] objArguments = { 1, 1, pValue };
object objReturn = objType.InvokeMember("AddInt",
System.Reflection.BindingFlags.InvokeMethod, null, objMathServer,
objArguments);
Console.WriteLine("method AddInt called");
}
catch (COMException ex)
{
Console.WriteLine("COM Exception:" + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("General Exception:" + ex.Message);
}
}
The signature of the Add method exposed by DCOM server is as follows:
HRESULT AddInt([in]int nVal1,[in]int nVal2,[out]int *nSum);
Now with the above code, the AddInt method will be called.
But I am not able to retrieve the sum returned by the out parameter
(,[out]int *nSum) and am not able to get the HRESULT value also.
Any solution for the above problem will be greatly appreciated.
Thanks and regards,
Mahesh
Willy Denoyette [MVP] - 06 Mar 2008 11:06 GMT
> Hello,
> I am trying to access a out of proc DCOM server from a C# client. I need
[quoted text clipped - 72 lines]
>
> Mahesh
Do you get an exception throw after the call?
The COMexception contains the HResult property, so, ex.HResult should hold
HRESULT for a failing call.
Note also that you should use early binding, that means you should "import"
the COM servers typelib ( .tlb file) at the client side.
Willy.
Mahesh - 06 Mar 2008 14:49 GMT
I get a exception only in case of E_FAIL. But if the interface returns
S_FALSE I don’t get any exceptions.
On the other hand i can't use early binding as this client and the COM
server will be deployed on different Machine's at each installations.
Basically I get the server location during run time from the DB and must
connect to that server.
I guess this is not possible by using "import".
-Mahesh
>> Hello,
>> I am trying to access a out of proc DCOM server from a C# client. I need
[quoted text clipped - 80 lines]
>
> Willy.
Willy Denoyette [MVP] - 06 Mar 2008 15:08 GMT
>I get a exception only in case of E_FAIL. But if the interface returns
>S_FALSE I don’t get any exceptions.
S_FALSE is not a failure HRESULT, that means if you don't get an exception,
it means that the HRESULT is < 0 or S_FALSE.
> On the other hand i can't use early binding as this client and the COM
> server will be deployed on different Machine's at each installations.
> Basically I get the server location during run time from the DB and must
> connect to that server.
> I guess this is not possible by using "import".
Importing means translating the "typelib" info into assembly metadata, this
is done by running tlbimp.exe on the COM server's typelib like this:
tlbimp /out:myInteropAssembly.dll comserver.tlb
In you client code you can now set a reference to the myInteropAssembly.dll
and use early binding.
Willy.
> -Mahesh
>
[quoted text clipped - 82 lines]
>>
>> Willy.