-1

相互運用機能を使用して C++ COM コンポーネントを呼び出していますが、マーシャリングにはパラメーターの 1 つをref Byteとして渡す必要があります。引数は実際には文字列です。文字列 (または char 配列) を Byte に変換してこのメ​​ソッドに渡すにはどうすればよいですか?

メソッド IDL

 [helpstring("method Read")]
    HRESULT _stdcall Read(
                    [in] unsigned char* path, 
                    [in] unsigned char command, 
                    [in] unsigned char nShortAddr, 
                    [out] short* pnDataSize, 
                    [out] unsigned char** ppbyData, 
                    [out] unsigned long* pnError);

IL

.method public hidebysig newslot virtual 
        instance void  Read([in] uint8& path,
                            [in] uint8 command,
                            [in] uint8 nShortAddr,
                            [out] int16& pnDataSize,
                            [out] native int ppbyData,
                            [out] uint32& pnError) runtime managed internalcall

Visual Studio に表示されるラッパーのメソッド

    public virtual void Read(ref byte path, byte command, byte nShortAddr,  
out short pnDataSize, System.IntPtr ppbyData, out uint pnError)
4

2 に答える 2

-1

Stringに変換することはできませんByte

ただし、ASCIIエンコーディングを使用することで、への変換がString可能ですByte[]


String is .Net is - 文字列は、テキストを表すために使用される Unicode 文字の連続したコレクションです。System.CharString オブジェクトは、文字列を表すオブジェクトの連続コレクションです。

Byte[]を使用して文字列を取得できますSystem.Text.Encoding.ASCII.GetBytes(my_string)

byte[] bytes = System.Text.Encoding.ASCII.GetBytes(my_string)
于 2013-04-05T02:20:23.703 に答える