mabra
mabra

Reputation: 383

Need RPC call example written in C#

I am dealing with WMI connection errors and timeouts and will try to check the RPC stack first. I found the following C code snippet, but do not understand if and how it works and where I may find further informations or even a sample.

RPC_STATUS status;
unsigned short *StringBinding;
RPC_BINDING_HANDLE BindingHandle;
status = RpcStringBindingCompose
  (
    NULL,                // Object UUID
    L"ncacn_ip_tcp",      // Protocol sequence to use
    L"MyServer.MyCompany.com", // Server DNS or Netbios Name
    NULL,
    NULL,
    &StringBinding
  );
// Error checking ommitted. If no error, we proceed below
status = RpcBindingFromStringBinding(StringBinding, &BindingHandle);

// free string regardless of errors from RpcBindingFromStringBinding
RpcStringFree(&StringBinding);

Does this code really make a connection? Does someone has a interop declartion for C#?

Thanks so far.

br--mabra

Upvotes: 1

Views: 4239

Answers (1)

GalacticJello
GalacticJello

Reputation: 11445

Here's the interop code for your above example:

class Rpc
{
    [DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
    public static extern int RpcStringBindingCompose(
        string ObjUuid,
        string ProtSeq,
        string NetworkAddr,
        string EndPoint,
        string Options,
        out string StringBinding);

    [DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
    public static extern int RpcBindingFromStringBinding(
        string StringBinding,
        out IntPtr Binding);

    [DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
    public static extern int RpcBindingFree(
        ref IntPtr Binding);

    public Rpc()
    {
        string stringBinding = null;

        int retCode = RpcStringBindingCompose(
             null,                // Object UUID
             "ncacn_ip_tcp",      // Protocol sequence to use
             "MyServer.MyCompany.com", // Server DNS or Netbios Name
             null,
             null,
             out stringBinding );

        IntPtr bindingHandle = IntPtr.Zero;
        retCode = RpcBindingFromStringBinding(stringBinding, out bindingHandle);

        retCode = RpcBindingFree(ref bindingHandle);
    }
}

Upvotes: 4

Related Questions