Back Programming

Articles Index

Rotate your screen to landscape mode to view this page.
The HP_MIDIFILE.dll file file from Heiko Plate - originally written for MIDI manipulation in C++ coded software programs - can be utilized in C# and VB.Net software programs as well. Two code samples are shown below.

For more information about the functions etc. in HP_MIDIFILE.dll please read the documentation from Heiko Plate.

The method is explained with a sample program that converts a MIDI type 1 file (c:\temp\test1.mid) to a MIDI type 0 file (c:\temp\new1.mid).

The sample programs consists of one form that has one command button called button1. The file HP_midifile.dll is located in the same folder as the programs.

The method used here is to declare the names of dll functions that will be needed in the programs using the internal identifications of the library. These "decorated names" are listed in the map-file HP_midifile.map.

The functions often refer to arguments such as HP_SMF0. The values expected by the dll are defined in the header file HP_midifile.h. For example the line

#define HP_SMF0	   0	/* Standard MIDI file-format 0 */

means passing a "0" signifies a type 0 MIDI file.

C# sample code for Form1
// all code for imports, UI etc. 
// generated by the designer is omitted

using System.Runtime.InteropServices;

namespace HP_test
{
 public class Form1 : System.Windows.Forms.Form
 {

  [DllImport("HP_midifile.dll", 
    EntryPoint="?HP_Init@@YAPAVMIDIFile@@XZ")]
    public static extern int HP_Init();

  [DllImport("HP_midifile.dll", 
    EntryPoint="?HP_Load@@YAIPAVMIDIFile@@PBD@Z")]
    public static extern int HP_Load(int i, String s);

  [DllImport("HP_midifile.dll", 
    EntryPoint="?HP_Save@@YAIPAVMIDIFile@@PBDH@Z")]
    public static extern int HP_Save(int i, String s, int x);

  [DllImport("HP_midifile.dll", 
    EntryPoint="?HP_Free@@YAIPAVMIDIFile@@@Z")]
    public static extern int HP_Free(int i);

  private void button1_Click(object sender, System.EventArgs e)
  {
   int smf0 = 0;
   int hp_err_none = 0;
   int mf = HP_Init();
   if (mf == 0)
   {
	    // do some error handling here
   }
   int result = HP_Load(mf, "c:\\temp\\test1.mid");
   if (result != hp_err_none)
   {
	    // do some error handling here
   }
   result = HP_Save(mf, "c:\\temp\\new1.mid", smf0);
   if (result != hp_err_none)
   {
	    // do some error handling here
   }
   result = HP_Free(mf);
   if (result != hp_err_none)
   {
	    // do some error handling here
   }
  }
 }
}

Due to a 64 bit vs. 32 bit conflict open "Projects" -> "Properties" window in Visual Studio and set the Target CPU as x86. You will get some warnings while compiling!

C# x86 Target CPU

VB.Net sample code for Form1
' all code for imports, UI etc. 
' generated by the designer is omitted
 
Imports System.Runtime.InteropServices

Public Class Form1 Inherits System.Windows.Forms.Form

  Private Declare Function HP_Init Lib _
  "HP_midifile.dll" _
  Alias "?HP_Init@@YAPAVMIDIFile@@XZ" () As Integer

  Private Declare Function HP_Load Lib _
  "HP_midifile.dll" _
  Alias "?HP_Load@@YAIPAVMIDIFile@@PBD@Z" (ByVal i As _
  Integer, ByVal s As String) As Integer

  Private Declare Function HP_Save Lib _
  "HP_midifile.dll" _
  Alias "?HP_Save@@YAIPAVMIDIFile@@PBDH@Z" (ByVal i As _
  Integer, ByVal s As String, ByVal x As Integer) As Integer

  Private Declare Function HP_Free Lib _
  "HP_midifile.dll" _
  Alias "?HP_Free@@YAIPAVMIDIFile@@@Z" (ByVal i As Integer) _
  As Integer

  Private Sub Button1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button1.Click

    Dim smf0 As Integer = 0
    Dim HP_ERR_NONE As Integer = 0
    Dim mf As Integer = HP_Init()
    If mf = 0 Then
            ' do some error handling here
    End If
    Dim result As Integer = HP_Load(mf, "c:\temp\test1.mid")
    If Not result = HP_ERR_NONE Then
            ' do some error handling here
    End If
    result = HP_Save(mf, "c:\temp\new1.mid", smf0)
    If Not result = HP_ERR_NONE Then
            ' do some error handling here
    End If
    result = HP_Free(mf)
    If Not result = HP_ERR_NONE Then
            ' do some error handling here
    End If
  End Sub

End Class

Due to a 64 bit vs. 32 bit conflict open "Projects" -> "Properties" window in Visual Studio and set the Target CPU as x86. You will get some warnings while compiling!

VB.Net x86 Target CPU