Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem using a fixed length string in a structure used as a parameter to a Win32API call

Problem using a fixed length string in a structure used as a parameter to a Win32API call

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdata-structuresregexjson
5 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Imports System.Runtime.InteropServices Public Class Form1 Public Const MAXPNAMELEN = 32 ' max product name length (including NULL) _ Structure MIDIINCAPS 'Structure def copied from win32api.txt Dim wMid As Int16 Dim wPid As Int16 Dim vVersion As Integer _ Dim szPname As String 'win32api.txt def is String*MAXPNAMELEN End Structure Declare Function midiInGetNumDevs Lib "winmm.dll" () As Long Declare Function midiInGetDevCaps Lib "winmm.dll" Alias "midiInGetDevCapsA" _ (ByVal uDeviceID As Int32, ByVal lpCaps As MIDIINCAPS, ByVal uSize As Int32) As Int32 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim retval As Int32 Dim NumMidiInDevices As Int32 Dim DevCounter As Int32 Dim devicecaps As New MIDIINCAPS NumMidiInDevices = midiInGetNumDevs() For DevCounter = 0 To NumMidiInDevices - 1 retval = midiInGetDevCaps(DevCounter, devicecaps, Len(devicecaps)) ' Runtime error msg here '======================================================================= 'A call to PInvoke function 'MidiInCapTest!MidiInCapTest.Form1::midiInGetDevCaps' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. '======================================================================== Next DevCounter End Sub '============================================================================ 'I have tried instead of Marshall as, in the sructure plus countless tries at int16, int32 etc for the other parameters in the API call,in a fruitless attempt to outwit the known inconsistencies between winapi32.txt data types & .NET but to no avail. 'Can anyone help End Class

    D L 2 Replies Last reply
    0
    • L Lost User

      Imports System.Runtime.InteropServices Public Class Form1 Public Const MAXPNAMELEN = 32 ' max product name length (including NULL) _ Structure MIDIINCAPS 'Structure def copied from win32api.txt Dim wMid As Int16 Dim wPid As Int16 Dim vVersion As Integer _ Dim szPname As String 'win32api.txt def is String*MAXPNAMELEN End Structure Declare Function midiInGetNumDevs Lib "winmm.dll" () As Long Declare Function midiInGetDevCaps Lib "winmm.dll" Alias "midiInGetDevCapsA" _ (ByVal uDeviceID As Int32, ByVal lpCaps As MIDIINCAPS, ByVal uSize As Int32) As Int32 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim retval As Int32 Dim NumMidiInDevices As Int32 Dim DevCounter As Int32 Dim devicecaps As New MIDIINCAPS NumMidiInDevices = midiInGetNumDevs() For DevCounter = 0 To NumMidiInDevices - 1 retval = midiInGetDevCaps(DevCounter, devicecaps, Len(devicecaps)) ' Runtime error msg here '======================================================================= 'A call to PInvoke function 'MidiInCapTest!MidiInCapTest.Form1::midiInGetDevCaps' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. '======================================================================== Next DevCounter End Sub '============================================================================ 'I have tried instead of Marshall as, in the sructure plus countless tries at int16, int32 etc for the other parameters in the API call,in a fruitless attempt to outwit the known inconsistencies between winapi32.txt data types & .NET but to no avail. 'Can anyone help End Class

      D Offline
      D Offline
      Dave Doknjas
      wrote on last edited by
      #2

      The main thing is that the 2nd parameter should be 'ByRef':

      Declare Function midiInGetDevCaps Lib "winmm.dll" Alias "midiInGetDevCapsA" (ByVal uDeviceID As Integer, ByRef lpCaps As MIDIINCAPS, ByVal uSize As Integer) As Integer

      Public Structure MIDIINCAPS
      Dim ManufacturerID As Short
      Dim ProductID As Short
      Dim DriverVersion As Integer
      Dim Label As String
      Dim Support As Integer
      End Structure

      David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

      L 1 Reply Last reply
      0
      • L Lost User

        Imports System.Runtime.InteropServices Public Class Form1 Public Const MAXPNAMELEN = 32 ' max product name length (including NULL) _ Structure MIDIINCAPS 'Structure def copied from win32api.txt Dim wMid As Int16 Dim wPid As Int16 Dim vVersion As Integer _ Dim szPname As String 'win32api.txt def is String*MAXPNAMELEN End Structure Declare Function midiInGetNumDevs Lib "winmm.dll" () As Long Declare Function midiInGetDevCaps Lib "winmm.dll" Alias "midiInGetDevCapsA" _ (ByVal uDeviceID As Int32, ByVal lpCaps As MIDIINCAPS, ByVal uSize As Int32) As Int32 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim retval As Int32 Dim NumMidiInDevices As Int32 Dim DevCounter As Int32 Dim devicecaps As New MIDIINCAPS NumMidiInDevices = midiInGetNumDevs() For DevCounter = 0 To NumMidiInDevices - 1 retval = midiInGetDevCaps(DevCounter, devicecaps, Len(devicecaps)) ' Runtime error msg here '======================================================================= 'A call to PInvoke function 'MidiInCapTest!MidiInCapTest.Form1::midiInGetDevCaps' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. '======================================================================== Next DevCounter End Sub '============================================================================ 'I have tried instead of Marshall as, in the sructure plus countless tries at int16, int32 etc for the other parameters in the API call,in a fruitless attempt to outwit the known inconsistencies between winapi32.txt data types & .NET but to no avail. 'Can anyone help End Class

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, This article[^] of mine should help you out for most issues. Yes it may take some experimenting; however for optimum progress, you should fix issues in this order: 1. The stack problem should be solved through the CallingConvention attribute. 2. The numeric parameters or fields should pose no problem, just make sure you use the managed/unmanaged types with matching sizes. 3. The fixed-length string field takes a MarshalAs with SizeConst (not sure a symbolic constant is acceptable); I expect ByValTStr is right, the doc says: Used for in-line, fixed-length character arrays that appear within a structure. The character type used with ByValTStr is determined by the System.Runtime.InteropServices.CharSet argument of the System.Runtime.InteropServices.StructLayoutAttribute applied to the containing structure. Good luck :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        L 1 Reply Last reply
        0
        • D Dave Doknjas

          The main thing is that the 2nd parameter should be 'ByRef':

          Declare Function midiInGetDevCaps Lib "winmm.dll" Alias "midiInGetDevCapsA" (ByVal uDeviceID As Integer, ByRef lpCaps As MIDIINCAPS, ByVal uSize As Integer) As Integer

          Public Structure MIDIINCAPS
          Dim ManufacturerID As Short
          Dim ProductID As Short
          Dim DriverVersion As Integer
          Dim Label As String
          Dim Support As Integer
          End Structure

          David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You are right, the main thing was to use byref on the 2nd param. I usually copy Api calls directly from win32api.txt but in this case copied it from someone elses downloaded code :( Thanks for the assist Midigeek

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, This article[^] of mine should help you out for most issues. Yes it may take some experimenting; however for optimum progress, you should fix issues in this order: 1. The stack problem should be solved through the CallingConvention attribute. 2. The numeric parameters or fields should pose no problem, just make sure you use the managed/unmanaged types with matching sizes. 3. The fixed-length string field takes a MarshalAs with SizeConst (not sure a symbolic constant is acceptable); I expect ByValTStr is right, the doc says: Used for in-line, fixed-length character arrays that appear within a structure. The character type used with ByValTStr is determined by the System.Runtime.InteropServices.CharSet argument of the System.Runtime.InteropServices.StructLayoutAttribute applied to the containing structure. Good luck :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See my reply to David Anton's ByRef suggestio which was the only change I made. Thanks for the link to your article which also mentions delegates. Now that Phase one (identify connected MIDI devices) works I shall code the MidiIn phase....watch this space!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups