RPC NDR -> .Net

A

Ashutosh

Hi,
I have some code which was written around 8-10 years back and I really I
don't have much idea what it does (no one in my team has either) other
than that it connects to some other other service over the network. This
code is in a VC++ DLL which is being used by a VB Application. We are
planning to create a new application in C# which does all the work done
by them. I tried searching the web, it shows some RPC NDR related to DCOM.

Is it somewhere related to custom marshaling or in DCOM? Also, is it
possible to achieve the same result somehow in .Net?

A part of the code is

int getConfig(
/* [ref][out][in] */ struct rpcConfig __RPC_FAR *r)
{
RPC_BINDING_HANDLE _Handle = 0;
int _RetVal;
RPC_MESSAGE _RpcMessage;
MIDL_STUB_MESSAGE _StubMsg;
RpcTryFinally
{
NdrClientInitializeNew(
( PRPC_MESSAGE )&_RpcMessage,
( PMIDL_STUB_MESSAGE )&_StubMsg,
( PMIDL_STUB_DESC )&chrpc_StubDesc,
10);
_Handle = hCHrpc;
_StubMsg.BufferLength = 0U;
NdrSimpleStructBufferSize( (PMIDL_STUB_MESSAGE) &_StubMsg,
(unsigned char __RPC_FAR *)r,
(PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156] );

NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg,
_StubMsg.BufferLength, _Handle );

NdrSimpleStructMarshall( (PMIDL_STUB_MESSAGE)& _StubMsg,
(unsigned char __RPC_FAR *)r,
(PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156] );

NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char
__RPC_FAR *) _StubMsg.Buffer );

if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) !=
NDR_LOCAL_DATA_REPRESENTATION )
NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING)
&__MIDL_ProcFormatString.Format[38] );

NdrSimpleStructUnmarshall( (PMIDL_STUB_MESSAGE) &_StubMsg,
(unsigned char __RPC_FAR * __RPC_FAR
*)&r,
(PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156],
(unsigned char)0 );

_RetVal = *(( int __RPC_FAR * )_StubMsg.Buffer)++;
}
RpcFinally
{
NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
}
RpcEndFinally
return _RetVal;
}

Thanks & Regards,
Ashutosh
 
D

dertopper

Hi,
I have some code which was written around 8-10 years back and I really I
don't have much idea what it does (no one in my team has either) other
than that it connects to some other other service over the network. This
code is in a VC++ DLL which is being used by a VB Application. We are
planning to create a new application in C# which does all the work done
by them. I tried searching the web, it shows some RPC NDR related to DCOM..

Is it somewhere related to custom marshaling or in DCOM? Also, is it
possible to achieve the same result somehow in .Net?

A part of the  code is

int getConfig(
    /* [ref][out][in] */ struct rpcConfig __RPC_FAR *r)
{
    RPC_BINDING_HANDLE _Handle    =    0;
    int _RetVal;
    RPC_MESSAGE _RpcMessage;
    MIDL_STUB_MESSAGE _StubMsg;
    RpcTryFinally
        {
        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&chrpc_StubDesc,
                          10);
        _Handle = hCHrpc;
        _StubMsg.BufferLength = 0U;
        NdrSimpleStructBufferSize( (PMIDL_STUB_MESSAGE) &_StubMsg,
                                   (unsigned char __RPC_FAR *)r,
                                   (PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156] );

        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg,
_StubMsg.BufferLength, _Handle );

        NdrSimpleStructMarshall( (PMIDL_STUB_MESSAGE)& _StubMsg,
                                 (unsigned char __RPC_FAR *)r,
                                 (PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156] );

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsignedchar
__RPC_FAR *) _StubMsg.Buffer );

        if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) !=
NDR_LOCAL_DATA_REPRESENTATION )
            NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING)
&__MIDL_ProcFormatString.Format[38] );

        NdrSimpleStructUnmarshall( (PMIDL_STUB_MESSAGE) &_StubMsg,
                                   (unsigned char __RPC_FAR * __RPC_FAR
*)&r,
                                   (PFORMAT_STRING)
&__MIDL_TypeFormatString.Format[156],
                                   (unsigned char)0 );

        _RetVal = *(( int __RPC_FAR * )_StubMsg.Buffer)++;
        }
    RpcFinally
        {
        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
        }
    RpcEndFinally
    return _RetVal;

}

At first glance I simply thought that this is standard code generated
by the MIDL compiler for a proxy-stub Dll. However, the function
getConfig is not part of a COM interface, so this method is apparently
hand-written (the author has certainly copy-pasted most of it from
MIDL generated code). The struct rpcConfig is not part of the standard
COM headers (at least not with the Platform SDK 2003), so I guess you
should search for the declaration of this struct. You should also look
for any calls of the function getConfig in the Dll code.

Regards,
Stuart
 
A

Ashutosh

Hi,
I got little hint!! It's for the RPC and it was developed before
COM/DCOM came. Can someone please confirm?

Also, is there any support in .Net for direct RPC, not using
COM/DCOM??? The server application to which this application connects
can't be changed under any circumstances.

Thanks & Regards,
Ashutosh
 
S

SvenC

Hi Ashutosh
Also, is there any support in .Net for direct RPC, not using
COM/DCOM??? The server application to which this application connects
can't be changed under any circumstances.

I doubt that. .Net has Remoting, Sockets, COM-support and most
importantly WCF as *the* communication strategy.

But using RPC (if you really must) might be a good project for using
C++/CLI where you can use native APIs to implement and expose .Net
classes which than can be consumed from C# and VB.Net
 
A

Ashutosh

Hi,
Thanks for the reply...

Yes you are correct, there is no direct support of RPC in .Net

Thanks & Regards,
Ashutosh
 
Top