Using lame_enc.dll in .NET - Signature mismatch
-
I'm working on a program that will convert a wav-file to mp3, tag it and then upload it via FTP. I'm done with the FTP-part and am now working on the converting to MP3 part (the only thing left). The plan is to use LAME (lame_enc.dll) to do the encoding/converting (unless you have any other suggestion). But I've hit a snag. I couldn't find any good examples for Lame & .NET. I found this here on TCP: C# MP3 Compressor[^] It wouldn't compile due to missing form components, but I copied the the classes that's relevant. The project compiles but I get an exception on the call to beInitStream (in the constructor of Mp3Writer). The error states:
PInvokeStackImbalance was detected
and that "the managed PInvoke signature didn't match the unmanaged target signature" (translated from Swedish). Is there anyone that has an idea what's wrong?! Is this code written for an older version of lame_enc.dll (I've looked at the documentation, but can't see the problem)? I also tried to use IntPtr etc (found in another example for another DLL), but same result:
Marshal.StructureToPtr(Marshal.SizeOf(m_Mp3Config), Marshal.AllocHGlobal(pConfig), false);
uint LameResult = Lame_encDll.beInitStream(ref pConfig, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
Code for DLL import:
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);or
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref IntPtr pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);I could just call lame.exe, but I don't want to do it, mainly because I want to report the progress. Glad for any help/ideas!
-
I'm working on a program that will convert a wav-file to mp3, tag it and then upload it via FTP. I'm done with the FTP-part and am now working on the converting to MP3 part (the only thing left). The plan is to use LAME (lame_enc.dll) to do the encoding/converting (unless you have any other suggestion). But I've hit a snag. I couldn't find any good examples for Lame & .NET. I found this here on TCP: C# MP3 Compressor[^] It wouldn't compile due to missing form components, but I copied the the classes that's relevant. The project compiles but I get an exception on the call to beInitStream (in the constructor of Mp3Writer). The error states:
PInvokeStackImbalance was detected
and that "the managed PInvoke signature didn't match the unmanaged target signature" (translated from Swedish). Is there anyone that has an idea what's wrong?! Is this code written for an older version of lame_enc.dll (I've looked at the documentation, but can't see the problem)? I also tried to use IntPtr etc (found in another example for another DLL), but same result:
Marshal.StructureToPtr(Marshal.SizeOf(m_Mp3Config), Marshal.AllocHGlobal(pConfig), false);
uint LameResult = Lame_encDll.beInitStream(ref pConfig, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
Code for DLL import:
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);or
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref IntPtr pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);I could just call lame.exe, but I don't want to do it, mainly because I want to report the progress. Glad for any help/ideas!
Hi, P/Invoke stack problems are caused by a calling convention mismatch. This[^] may help you. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I'm working on a program that will convert a wav-file to mp3, tag it and then upload it via FTP. I'm done with the FTP-part and am now working on the converting to MP3 part (the only thing left). The plan is to use LAME (lame_enc.dll) to do the encoding/converting (unless you have any other suggestion). But I've hit a snag. I couldn't find any good examples for Lame & .NET. I found this here on TCP: C# MP3 Compressor[^] It wouldn't compile due to missing form components, but I copied the the classes that's relevant. The project compiles but I get an exception on the call to beInitStream (in the constructor of Mp3Writer). The error states:
PInvokeStackImbalance was detected
and that "the managed PInvoke signature didn't match the unmanaged target signature" (translated from Swedish). Is there anyone that has an idea what's wrong?! Is this code written for an older version of lame_enc.dll (I've looked at the documentation, but can't see the problem)? I also tried to use IntPtr etc (found in another example for another DLL), but same result:
Marshal.StructureToPtr(Marshal.SizeOf(m_Mp3Config), Marshal.AllocHGlobal(pConfig), false);
uint LameResult = Lame_encDll.beInitStream(ref pConfig, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
Code for DLL import:
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);or
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref IntPtr pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);I could just call lame.exe, but I don't want to do it, mainly because I want to report the progress. Glad for any help/ideas!
-
I'm working on a program that will convert a wav-file to mp3, tag it and then upload it via FTP. I'm done with the FTP-part and am now working on the converting to MP3 part (the only thing left). The plan is to use LAME (lame_enc.dll) to do the encoding/converting (unless you have any other suggestion). But I've hit a snag. I couldn't find any good examples for Lame & .NET. I found this here on TCP: C# MP3 Compressor[^] It wouldn't compile due to missing form components, but I copied the the classes that's relevant. The project compiles but I get an exception on the call to beInitStream (in the constructor of Mp3Writer). The error states:
PInvokeStackImbalance was detected
and that "the managed PInvoke signature didn't match the unmanaged target signature" (translated from Swedish). Is there anyone that has an idea what's wrong?! Is this code written for an older version of lame_enc.dll (I've looked at the documentation, but can't see the problem)? I also tried to use IntPtr etc (found in another example for another DLL), but same result:
Marshal.StructureToPtr(Marshal.SizeOf(m_Mp3Config), Marshal.AllocHGlobal(pConfig), false);
uint LameResult = Lame_encDll.beInitStream(ref pConfig, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream);
Code for DLL import:
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);or
[DllImport("Lame_enc.dll")]
public static extern uint beInitStream(ref IntPtr pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);I could just call lame.exe, but I don't want to do it, mainly because I want to report the progress. Glad for any help/ideas!
I can't see anything obviously wrong from what you've posted. I've had a quick look online at the C definition for the function and I would declare it like:
[DllImport("Lame_enc.dll")]
public static extern int beInitStream(ref BE_CONFIG config, out int samples, out int bufferSize, out IntPtr streamHandle);I've used
out
rather thanref
for the last 3 parameters as this[^] suggests that these values are returned by the function so no need to pass a reference in. Edit: Be sure to check the return value from the function. It will be one of these constants:public const int BE_ERR_SUCCESSFUL = 0x00000000;
public const int BE_ERR_INVALID_FORMAT = 0x00000001;
public const int BE_ERR_INVALID_FORMAT_PARAMETERS = 0x00000002;
public const int BE_ERR_NO_MORE_HANDLES = 0x00000003;
public const int BE_ERR_INVALID_HANDLE = 0x00000004;
public const int BE_ERR_BUFFER_TOO_SMALL = 0x00000005;Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)