Passing a type to a DLL API

BlitzMax Forums/BlitzMax Programming/Passing a type to a DLL API

I'm sure Mark or Skid could produce a much shorter and easier version, but this at least seems to work:

BlitzMAX Code:
Import "midi.c"

Rem
typedef struct { 
    WORD      wMid; 
    WORD      wPid; 
    MMVERSION vDriverVersion; 
    CHAR      szPname[MAXPNAMELEN]; 
    WORD      wTechnology; 
    WORD      wVoices; 
    WORD      wNotes; 
    WORD      wChannelMask; 
    DWORD     dwSupport; 
} MIDIOUTCAPS; 
EndRem

Extern
	Function bbMidiOutCapsSize()
	Function bbMidiOutGetDevCaps(device:Int,buf:Byte Ptr)
End Extern

Type MIDICaps
	Field manufacturerId:Int
	Field productId:Int
	Field driverVersion:Int
	Field productName:String
	Field technology:String
	Field voices:Int
	Field notes:Int
	Field channelMask:Int
	Field support:Int
End Type

Local caps:MIDICaps=GetMidiDevCaps(0)

Print caps.productName

Function GetMidiDevCaps:MIDICaps(device:Int)
	Local caps:MIDICaps=New MIDICaps
	
	Local bank:TBank=CreateBank(bbMidiOutCapsSize())
	
	Local result:Int=bbMidiOutGetDevCaps(device,BankBuf(bank))
	
	caps.manufacturerId=PeekShort(bank,0)
	caps.productId=PeekShort(bank,2)
	caps.driverVersion=PeekInt(bank,4)
	
	Local out:String
	
	For Local k:Byte=0 Until 32
		out = out + Chr(PeekByte(bank,8+k))	
	Next
	
	caps.productName=out
	caps.technology=PeekShort(bank,40)
	caps.voices=PeekShort(bank,42)
	caps.notes=PeekShort(bank,44)
	caps.channelMask=PeekShort(bank,46)
	caps.support=PeekInt(bank,48)
	
	Return caps
	
End Function


midi.c code (MinGW required to compile under Windows):
#include <windows.h>

int bbMidiOutCapsSize()
{
	return sizeof(MIDIOUTCAPS);
}

int bbMidiOutGetDevCaps(int device, char* out)
{
	MIDIOUTCAPS caps;
	
	int result=midiOutGetDevCaps(device,&caps,sizeof(MIDIOUTCAPS));

	
	memcpy(out,&caps,sizeof(MIDIOUTCAPS));

	return result;
}