0

私はc#.netでネイティブAPIを呼び出そうとしています。以下のコードをC#呼び出しに変換するのを手伝ってもらえますか?本当にありがたいです。

dwResult = ::MprAdminMIBServerConnect( pwcComputerName.GetText(), &hMibServer );

dwResult = ::MprAdminServerGetInfo( hMibServer, 0, (LPBYTE*)&pServerBuf );

// I want to read the below variables as string
pInObjectEntry->Put(L"rastotalportstoconnectto", pServerBuf->dwTotalPorts );
pInObjectEntry->Put(L"rasportsinuse", pServerBuf->dwPortsInUse );

これがサンプルコードです。dwTotalPortsとdwPortsInUseの値を読み取る方法を教えてください。

  class RASCollector
    {
        [DllImport("mprapi.dll", SetLastError = false)]
        public static extern UInt32 MprAdminMIBServerConnect([MarshalAs(UnmanagedType.LPWStr)] string lpwsServerName, out IntPtr phMibServer);

        [DllImport("mprapi.dll", SetLastError = false)]
        public static extern UInt32 MprAdminServerGetInfo(IntPtr phMprServer, UInt32 dwLevel, out byte[] pServerBuf);

        public void Run()
        {
            IntPtr hMibServer = new IntPtr();
            UInt32 result;

            result = MprAdminMIBServerConnect("localhost", out hMibServer);

            byte[] pServerBuf;

            result = MprAdminServerGetInfo(hMibServer, 0, out pServerBuf);
        }
    }
4

1 に答える 1

0

これは、InterOPを使用して行われます。次のように各関数をインポートする必要があります。

using System.Runtime.InteropServices;

[DllImport("mprapi.dll", SetLastError = false)]
public static extern UInt32 MprAdminMIBServerConnect([MarshalAs(UnmanagedType.LPWStr)] string lpwsServerName, out IntPtr phMibServer);

MSDNライブラリで定義されているデータ型は、対応するC#データ型に変換する必要があります。詳細については、次の記事を確認してください:http: //msdn.microsoft.com/en-us/library/ac7ay120.aspx

C#での複雑なデータ構造とポインターのマーシャリングの詳細については、http: //blogs.msdn.com/b/dsvc/archive/2009/02/18/marshalling-complicated-structures-using-pinvoke.aspxを参照してください。

于 2013-02-26T14:11:36.770 に答える