WAVEOUTCAPS problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y25pY2ts?=

    WAVEOUTCAPS problem

    Hi,

    I trying to get something that seems to be simple to work: I’m using VS
    2005/C#. I want to get the information of my WaveOut devices. The WAVEOUTCAPS
    structure is defined in C++ as:

    typedef struct {
    WORD wMid;
    WORD wPid;
    MMVERSION vDriverVersion;
    TCHAR szPname[MAXPNAMELEN];
    DWORD dwFormats;
    WORD wChannels;
    WORD wReserved1;
    DWORD dwSupport;
    } WAVEOUTCAPS;

    In C# I declare it as:

    public struct WAVEOUTCAPS
    {
    public ushort wMid;
    public ushort wPid;
    public ulong vDriverVersion;
    public char[] szPname;
    public ulong dwFormats;
    public ushort wChannels;
    public ushort wReserved1;
    public ulong dwSupport;
    }

    And later in the code:

    WAVEOUTCAPS waveinfo = new WAVEOUTCAPS();
    waveinfo.szPnam e = new char[32];

    the waveOutGetDevCa ps function is declared as

    [DllImport("winm m.dll")]
    public static extern int waveOutGetDevCa ps(int uDeviceID, ref WAVEOUTCAPS
    lpCaps, int uSize);

    everything up to here compiles and runs fine. As soon as I call

    waveOutGetDevCa ps(0, ref waveinfo, Marshal.SizeOf( waveinfo));

    my program crashes with the error:

    System.Runtime. InteropServices .SafeArrayTypeM ismatchExceptio n was unhandled
    Message="Specif ied array was not of the expected type."

    Any ideas what I’m doing wrong?

  • Peter Duniho

    #2
    Re: WAVEOUTCAPS problem

    On Sun, 08 Jul 2007 14:00:01 -0700, cnickl
    <cnickl@discuss ions.microsoft. comwrote:
    [...]
    my program crashes with the error:
    >
    System.Runtime. InteropServices .SafeArrayTypeM ismatchExceptio n was
    unhandled
    Message="Specif ied array was not of the expected type."
    >
    Any ideas what I’m doing wrong?
    Maybe you need to explicitly call the Unicode version? The error sure
    seems like something that would come up if you tried to pass a C# Unicode
    character array to a Win32 ANSI-version function.

    I think that the p/invoke stuff should have a way to specify that, but if
    not I suppose you could just use "waveOutGetDevC apsW" explicitly.

    Pete

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: WAVEOUTCAPS problem

      The problem is that the OP has an array declaration but doesn't specify
      that it should be an inline array like the original structure. The array
      declaration should be:

      [StructLayout(La youtKind.Sequen tial)]
      public struct WAVEOUTCAPS
      {
      public ushort wMid;
      public short wPid;
      public ulong vDriverVersion;
      [MarshalAs(Unman agedType.ByValT Str, SizeConst=MAXPN AMELEN)]
      public string szPname;

      Comment

      • Peter Duniho

        #4
        Re: WAVEOUTCAPS problem

        On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
        <mvp@spam.guard .caspershouse.c omwrote:
        The problem is that the OP has an array declaration but doesn't
        specify that it should be an inline array like the original structure.
        Ahh. I see that now.

        Is it safe to assume that p/invoke will always match up the correct TCHAR
        version of a Win32 function? Or are there situations in which that is
        actually an issue and needs to be addressed explicitly?

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: WAVEOUTCAPS problem

          It is dependent on the value set to the CharSet property of the
          StructLayout attribute assigned to the structure.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.tu56tae v8jd0ej@petes-computer.local. ..
          On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
          <mvp@spam.guard .caspershouse.c omwrote:
          >
          > The problem is that the OP has an array declaration but doesn't
          >specify that it should be an inline array like the original structure.
          >
          Ahh. I see that now.
          >
          Is it safe to assume that p/invoke will always match up the correct TCHAR
          version of a Win32 function? Or are there situations in which that is
          actually an issue and needs to be addressed explicitly?

          Comment

          • =?Utf-8?B?Y25pY2ts?=

            #6
            Re: WAVEOUTCAPS problem

            Thanks. Your post with the sample code was very helpful. I would have never
            thought about this.

            "Nicholas Paldino [.NET/C# MVP]" wrote:
            It is dependent on the value set to the CharSet property of the
            StructLayout attribute assigned to the structure.
            >
            >
            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m
            >
            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
            news:op.tu56tae v8jd0ej@petes-computer.local. ..
            On Sun, 08 Jul 2007 16:13:12 -0700, Nicholas Paldino [.NET/C# MVP]
            <mvp@spam.guard .caspershouse.c omwrote:
            The problem is that the OP has an array declaration but doesn't
            specify that it should be an inline array like the original structure.
            Ahh. I see that now.

            Is it safe to assume that p/invoke will always match up the correct TCHAR
            version of a Win32 function? Or are there situations in which that is
            actually an issue and needs to be addressed explicitly?
            >

            Comment

            • Peter Duniho

              #7
              Re: WAVEOUTCAPS problem

              On Sun, 08 Jul 2007 17:35:05 -0700, Nicholas Paldino [.NET/C# MVP]
              <mvp@spam.guard .caspershouse.c omwrote:
              It is dependent on the value set to the CharSet property of the
              StructLayout attribute assigned to the structure.
              I didn't see that attribute in the original post. That's my point. Can
              one rely on the default behavior? Is there a reliable default behavior?
              Or is a structure definition without that attribute a bug waiting to be
              found?

              Comment

              • Chris Dunaway

                #8
                Re: WAVEOUTCAPS problem

                On Jul 8, 10:18 pm, cnickl <cni...@discuss ions.microsoft. comwrote:
                Thanks. Your post with the sample code was very helpful. I would have never
                thought about this.
                >
                In the future, you might find the pinvoke.net website useful. They
                also have an add in to make it easy to get pinvoke signatures directly
                from VS.

                Chris

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: WAVEOUTCAPS problem

                  Well, I would say that it is a bug. Depending on what version of the
                  API the OP is going to make the call for, it is up to the OP to set that
                  flag. Generally, when using structures for A or W versions of APIS which
                  have embedded strings in them which need to be marshaled (using the
                  ByValTStr UnmanagedType), it would be erroneous to not include the CharSet
                  property on the attribute definition.


                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                  news:op.tu6hp8t i8jd0ej@petes-computer.local. ..
                  On Sun, 08 Jul 2007 17:35:05 -0700, Nicholas Paldino [.NET/C# MVP]
                  <mvp@spam.guard .caspershouse.c omwrote:
                  >
                  > It is dependent on the value set to the CharSet property of the
                  >StructLayout attribute assigned to the structure.
                  >
                  I didn't see that attribute in the original post. That's my point. Can
                  one rely on the default behavior? Is there a reliable default behavior?
                  Or is a structure definition without that attribute a bug waiting to be
                  found?

                  Comment

                  • Peter Duniho

                    #10
                    Re: WAVEOUTCAPS problem

                    On Mon, 09 Jul 2007 08:14:07 -0700, Nicholas Paldino [.NET/C# MVP]
                    <mvp@spam.guard .caspershouse.c omwrote:
                    Well, I would say that it is a bug. Depending on what version of the
                    API the OP is going to make the call for, it is up to the OP to set that
                    flag. Generally, when using structures for A or W versions of APIS which
                    have embedded strings in them which need to be marshaled (using the
                    ByValTStr UnmanagedType), it would be erroneous to not include the
                    CharSet
                    property on the attribute definition.
                    Okay...thanks! Good to know. :)

                    Comment

                    Working...