convert WCHAR in byte[] to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • centrino

    convert WCHAR in byte[] to string

    hi,

    I import a funtion fromm dll file:

    dll function header :

    DWORD WINAPI
    Enumerate(
    HANDLE hWDMHandle,
    PNDIS_STATUS pNStatus,
    PWCHAR pBuffer,
    PUINT pBufferSize
    )

    in C# should be:


    [DllImport("MyAP Idll", SetLastError=tr ue)]
    private static extern unsafe ulong Enumerate (
    IntPtr g_hPCASIMHandle ,
    ulong* pNStatus,
    void* pBuffer,
    uint* pBufferSize);


    und aufgerufen:

    [CSHARP]
    byte[] buf = new byte[2024];
    uint iBytesRead = 0;
    ulong ioResult;
    ulong ndis_status;
    uint buffsize;

    unsafe
    {
    // create a void pointer to buf
    fixed (void* pBuffer = buf)
    {
    ioResult=Enumer ate(this.m_iHan dle,
    &ndis_status,pB uffer,&buffsize );
    }


    So how can i read buf or convert it to string ?
  • Willy Denoyette [MVP]

    #2
    Re: convert WCHAR in byte[] to string


    "centrino" <centrino@discu ssions.microsof t.com> wrote in message
    news:7E061AD3-9789-4598-8A38-C397E8CE3DC6@mi crosoft.com...[color=blue]
    > hi,
    >
    > I import a funtion fromm dll file:
    >
    > dll function header :
    >
    > DWORD WINAPI
    > Enumerate(
    > HANDLE hWDMHandle,
    > PNDIS_STATUS pNStatus,
    > PWCHAR pBuffer,
    > PUINT pBufferSize
    > )
    >
    > in C# should be:
    >
    >
    > [DllImport("MyAP Idll", SetLastError=tr ue)]
    > private static extern unsafe ulong Enumerate (
    > IntPtr g_hPCASIMHandle ,
    > ulong* pNStatus,
    > void* pBuffer,
    > uint* pBufferSize);
    >
    >
    > und aufgerufen:
    >
    > [CSHARP]
    > byte[] buf = new byte[2024];
    > uint iBytesRead = 0;
    > ulong ioResult;
    > ulong ndis_status;
    > uint buffsize;
    >
    > unsafe
    > {
    > // create a void pointer to buf
    > fixed (void* pBuffer = buf)
    > {
    > ioResult=Enumer ate(this.m_iHan dle,
    > &ndis_status,pB uffer,&buffsize );
    > }
    >
    >
    > So how can i read buf or convert it to string ?[/color]

    There is no need for this, but first some remarks.
    1. A long in C# is a 64 bit entity in C it's 32 bit.
    2. There is no need for unsafe in your declarations and your code.
    3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
    default, so you need to change this behavior.

    Change your function declaration into:

    [DllImport("MyAP Idll", SetLastError=tr ue)]
    private static extern uint Enumerate (
    IntPtr g_hPCASIMHandle ,
    ref uint pNStatus,
    [MarshalAs(Unman agedType.LPWStr )]
    StringBuilder pBuffer,
    ref uint pBufferSize);

    And use it as:

    StringBuilder buf new StringBuilder( 2048);
    uint iBytesRead = 0;
    uintioResult;
    uint ndis_status;
    uint buffsize;

    ....
    ioResult = Enumerate(this. m_iHandle, ref ndis_status, buf, ref buffsize);
    string s = buf.ToString();

    Willy.





    Comment

    • Willy Denoyette [MVP]

      #3
      Re: convert WCHAR in byte[] to string


      "centrino" <centrino@discu ssions.microsof t.com> wrote in message
      news:7E061AD3-9789-4598-8A38-C397E8CE3DC6@mi crosoft.com...[color=blue]
      > hi,
      >
      > I import a funtion fromm dll file:
      >
      > dll function header :
      >
      > DWORD WINAPI
      > Enumerate(
      > HANDLE hWDMHandle,
      > PNDIS_STATUS pNStatus,
      > PWCHAR pBuffer,
      > PUINT pBufferSize
      > )
      >
      > in C# should be:
      >
      >
      > [DllImport("MyAP Idll", SetLastError=tr ue)]
      > private static extern unsafe ulong Enumerate (
      > IntPtr g_hPCASIMHandle ,
      > ulong* pNStatus,
      > void* pBuffer,
      > uint* pBufferSize);
      >
      >
      > und aufgerufen:
      >
      > [CSHARP]
      > byte[] buf = new byte[2024];
      > uint iBytesRead = 0;
      > ulong ioResult;
      > ulong ndis_status;
      > uint buffsize;
      >
      > unsafe
      > {
      > // create a void pointer to buf
      > fixed (void* pBuffer = buf)
      > {
      > ioResult=Enumer ate(this.m_iHan dle,
      > &ndis_status,pB uffer,&buffsize );
      > }
      >
      >
      > So how can i read buf or convert it to string ?[/color]

      There is no need for this, but first some remarks.
      1. A long in C# is a 64 bit entity in C it's 32 bit.
      2. There is no need for unsafe in your declarations and your code.
      3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
      default, so you need to change this behavior.

      Change your function declaration into:

      [DllImport("MyAP Idll", SetLastError=tr ue)]
      private static extern uint Enumerate (
      IntPtr g_hPCASIMHandle ,
      ref uint pNStatus,
      [MarshalAs(Unman agedType.LPWStr )]
      StringBuilder pBuffer,
      ref uint pBufferSize);

      And use it as:

      StringBuilder buf new StringBuilder( 2048);
      uint iBytesRead = 0;
      uintioResult;
      uint ndis_status;
      uint buffsize;

      ....
      ioResult = Enumerate(this. m_iHandle, ref ndis_status, buf, ref buffsize);
      string s = buf.ToString();

      Willy.





      Comment

      • centrino

        #4
        Re: convert WCHAR in byte[] to string

        Thanks Willy !
        It works.
        But i think i get only first vallue of pBuffer.
        Maybe i need an array of;

        StringBuilder[] buf;

        How can i initializise it:
        StringBuilder[] buf = new StringBuilder ....????

        and:

        [DllImport("MyAP Idll", SetLastError=tr ue)]
        private static extern uint Enumerate (
        IntPtr g_hPCASIMHandle ,
        ref uint pNStatus,
        [MarshalAs(Unman agedType.LPWStr )]
        StringBuilder pBuffer, // ???????
        ref uint pBufferSize)


        "Willy Denoyette [MVP]" wrote:
        [color=blue]
        >
        > "centrino" <centrino@discu ssions.microsof t.com> wrote in message
        > news:7E061AD3-9789-4598-8A38-C397E8CE3DC6@mi crosoft.com...[color=green]
        > > hi,
        > >
        > > I import a funtion fromm dll file:
        > >
        > > dll function header :
        > >
        > > DWORD WINAPI
        > > Enumerate(
        > > HANDLE hWDMHandle,
        > > PNDIS_STATUS pNStatus,
        > > PWCHAR pBuffer,
        > > PUINT pBufferSize
        > > )
        > >
        > > in C# should be:
        > >
        > >
        > > [DllImport("MyAP Idll", SetLastError=tr ue)]
        > > private static extern unsafe ulong Enumerate (
        > > IntPtr g_hPCASIMHandle ,
        > > ulong* pNStatus,
        > > void* pBuffer,
        > > uint* pBufferSize);
        > >
        > >
        > > und aufgerufen:
        > >
        > > [CSHARP]
        > > byte[] buf = new byte[2024];
        > > uint iBytesRead = 0;
        > > ulong ioResult;
        > > ulong ndis_status;
        > > uint buffsize;
        > >
        > > unsafe
        > > {
        > > // create a void pointer to buf
        > > fixed (void* pBuffer = buf)
        > > {
        > > ioResult=Enumer ate(this.m_iHan dle,
        > > &ndis_status,pB uffer,&buffsize );
        > > }
        > >
        > >
        > > So how can i read buf or convert it to string ?[/color]
        >
        > There is no need for this, but first some remarks.
        > 1. A long in C# is a 64 bit entity in C it's 32 bit.
        > 2. There is no need for unsafe in your declarations and your code.
        > 3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
        > default, so you need to change this behavior.
        >
        > Change your function declaration into:
        >
        > [DllImport("MyAP Idll", SetLastError=tr ue)]
        > private static extern uint Enumerate (
        > IntPtr g_hPCASIMHandle ,
        > ref uint pNStatus,
        > [MarshalAs(Unman agedType.LPWStr )]
        > StringBuilder pBuffer,
        > ref uint pBufferSize);
        >
        > And use it as:
        >
        > StringBuilder buf new StringBuilder( 2048);
        > uint iBytesRead = 0;
        > uintioResult;
        > uint ndis_status;
        > uint buffsize;
        >
        > ....
        > ioResult = Enumerate(this. m_iHandle, ref ndis_status, buf, ref buffsize);
        > string s = buf.ToString();
        >
        > Willy.
        >
        >
        >
        >
        >
        >[/color]

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: convert WCHAR in byte[] to string


          "centrino" <centrino@discu ssions.microsof t.com> wrote in message
          news:27D43345-2898-46CE-81AE-1AE83519768A@mi crosoft.com...[color=blue]
          > Thanks Willy !
          > It works.
          > But i think i get only first vallue of pBuffer.
          > Maybe i need an array of;
          >
          > StringBuilder[] buf;
          >
          > How can i initializise it:
          > StringBuilder[] buf = new StringBuilder ....????
          >
          > and:
          >
          > [DllImport("MyAP Idll", SetLastError=tr ue)]
          > private static extern uint Enumerate (
          > IntPtr g_hPCASIMHandle ,
          > ref uint pNStatus,
          > [MarshalAs(Unman agedType.LPWStr )]
          > StringBuilder pBuffer, // ???????
          > ref uint pBufferSize)
          >
          >[/color]

          No, you don't need a StringBuilder[].
          What do you mean with the first value, do you mean the first character? How
          are you looking at it, don't say in the debugger.

          What is the length of the string in the StringBuilder?
          int len = buf.ToString(). Length;

          Did you check the value in pBufferSize on return?

          Willy.




          Comment

          • centrino

            #6
            Re: convert WCHAR in byte[] to string

            i get:


            pBufferSize: 772
            pBuffer: \Device\{91E405 CA-9D5E-4366-BB44-3B27E09C6C35}

            i think i would get more, i don't know !?

            regards


            "Willy Denoyette [MVP]" wrote:
            [color=blue]
            >
            > "centrino" <centrino@discu ssions.microsof t.com> wrote in message
            > news:27D43345-2898-46CE-81AE-1AE83519768A@mi crosoft.com...[color=green]
            > > Thanks Willy !
            > > It works.
            > > But i think i get only first vallue of pBuffer.
            > > Maybe i need an array of;
            > >
            > > StringBuilder[] buf;
            > >
            > > How can i initializise it:
            > > StringBuilder[] buf = new StringBuilder ....????
            > >
            > > and:
            > >
            > > [DllImport("MyAP Idll", SetLastError=tr ue)]
            > > private static extern uint Enumerate (
            > > IntPtr g_hPCASIMHandle ,
            > > ref uint pNStatus,
            > > [MarshalAs(Unman agedType.LPWStr )]
            > > StringBuilder pBuffer, // ???????
            > > ref uint pBufferSize)
            > >
            > >[/color]
            >
            > No, you don't need a StringBuilder[].
            > What do you mean with the first value, do you mean the first character? How
            > are you looking at it, don't say in the debugger.
            >
            > What is the length of the string in the StringBuilder?
            > int len = buf.ToString(). Length;
            >
            > Did you check the value in pBufferSize on return?
            >
            > Willy.
            >
            >
            >
            >
            >[/color]

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: convert WCHAR in byte[] to string


              "centrino" <centrino@discu ssions.microsof t.com> wrote in message
              news:D9D5C5E8-646E-4B01-8B22-6B551C82B7AC@mi crosoft.com...[color=blue]
              >i get:
              >
              >
              > pBufferSize: 772
              > pBuffer: \Device\{91E405 CA-9D5E-4366-BB44-3B27E09C6C35}
              >
              > i think i would get more, i don't know !?
              >
              > regards
              >
              >
              >[/color]

              I guess you do have more. Your pBufferSize is 772 ( don't know what exactly,
              chars or bytes), the string is shorter anyway.
              I guess the buffer returned contains strings delimitted with 'nulls', like
              this...
              \Device\{91E405 CA-9D5E-4366-BB44-3B27E09C6C35}\0 \0
              \Device\{...... ............... ............... ............... .......}\0\0

              You can check the stringbuilders Length after return.
              If I'm right, you can use ToString(int startindex, int length) to retrieve
              the individual strings from the StringBuilders buffer.


              Willy.


              Comment

              Working...