需要用C#编写的RPC调用示例

发布于 2024-08-15 14:17:03 字数 731 浏览 1 评论 0原文

我正在处理 WMI 连接错误和超时,并将首先尝试检查 RPC 堆栈。我找到了以下 C 代码片段,但不明白它是否以及如何工作以及在哪里可以找到更多信息甚至示例。

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);

这段代码真的能建立联系吗? 有人有 C# 的互操作声明吗?

到目前为止谢谢。

br--马布拉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2024-08-22 14:17:03

这是上面示例的互操作代码:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文