在 C# 中编组到本机库
我在从托管 C# 代码中调用本机库的函数时遇到问题。我正在开发 3.5 紧凑框架 (Windows Mobile 6.x),以防万一这会产生任何影响。
我正在使用 coredll.dll 中的 waveIn* 函数(我相信这些函数位于常规 Windows 的 winmm.dll 中)。这就是我想到的:
// namespace winmm; class winmm
[StructLayout(LayoutKind.Sequential)]
public struct WAVEFORMAT
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct WAVEHDR
{
public IntPtr lpData;
public uint dwBufferLength;
public uint dwBytesRecorded;
public IntPtr dwUser;
public uint dwFlags;
public uint dwLoops;
public IntPtr lpNext;
public IntPtr reserved;
}
public delegate void AudioRecordingDelegate(IntPtr deviceHandle, uint message, IntPtr instance, ref WAVEHDR wavehdr, IntPtr reserved2);
[DllImport("coredll.dll")]
public static extern int waveInAddBuffer(IntPtr hWaveIn, ref WAVEHDR lpWaveHdr, uint cWaveHdrSize);
[DllImport("coredll.dll")]
public static extern int waveInPrepareHeader(IntPtr hWaveIn, ref WAVEHDR lpWaveHdr, uint Size);
[DllImport("coredll.dll")]
public static extern int waveInStart(IntPtr hWaveIn);
// some other class
private WinMM.WinMM.AudioRecordingDelegate waveIn;
private IntPtr handle;
private uint bufferLength;
private void setupBuffer()
{
byte[] buffer = new byte[bufferLength];
GCHandle bufferPin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
WinMM.WinMM.WAVEHDR hdr = new WinMM.WinMM.WAVEHDR();
hdr.lpData = bufferPin.AddrOfPinnedObject();
hdr.dwBufferLength = this.bufferLength;
hdr.dwFlags = 0;
int i = WinMM.WinMM.waveInPrepareHeader(this.handle, ref hdr, Convert.ToUInt32(Marshal.SizeOf(hdr)));
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInPrepare";
return;
}
i = WinMM.WinMM.waveInAddBuffer(this.handle, ref hdr, Convert.ToUInt32(Marshal.SizeOf(hdr)));
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInAddrBuffer";
return;
}
}
private void setupWaveIn()
{
WinMM.WinMM.WAVEFORMAT format = new WinMM.WinMM.WAVEFORMAT();
format.wFormatTag = WinMM.WinMM.WAVE_FORMAT_PCM;
format.nChannels = 1;
format.nSamplesPerSec = 8000;
format.wBitsPerSample = 8;
format.nBlockAlign = Convert.ToUInt16(format.nChannels * format.wBitsPerSample);
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
this.bufferLength = format.nAvgBytesPerSec;
format.cbSize = 0;
int i = WinMM.WinMM.waveInOpen(out this.handle, WinMM.WinMM.WAVE_MAPPER, ref format, Marshal.GetFunctionPointerForDelegate(waveIn), 0, WinMM.WinMM.CALLBACK_FUNCTION);
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInOpen";
return;
}
setupBuffer();
WinMM.WinMM.waveInStart(this.handle);
}
过去几天我读了很多关于编组的内容,但我没有让这段代码工作。当缓冲区已满时调用我的回调函数(waveIn)时,wavehdr 中传回的 hdr 结构显然已损坏。这是此时结构的示例:
- wavehdr {WinMM.WinMM.WAVEHDR} WinMM.WinMM.WAVEHDR dwBufferLength 0x19904c00 uint dwBytesRecorded 0x0000fa00 uint dwFlags 0x00000003 uint dwLoops 0x1990f6a4 uint + dwUser 0x00000000 System.IntPtr + lpData 0x00000000 System.IntPtr + lpNext 0x00000000 System.IntPtr + reserved 0x7c07c9a0 System.IntPtr
这显然不是我期望通过的。我显然很关心视图中字段的顺序。我不知道 Visual Studio .NET 在“本地”视图中显示记录时是否关心实际内存顺序,但它们显然没有按照我在结构中指定的顺序显示。
然后没有数据指针,并且 bufferLength 字段太高。有趣的是,bytesRecorded 字段恰好是 64000 - bufferLength 和 bytesRecorded 我希望两者都是 64000。我不知道到底出了什么问题,也许有人可以帮助我解决这个问题。我对托管代码编程和编组绝对是菜鸟,所以请不要对我可能做过的所有愚蠢的事情太严厉。
哦,这是我在此处找到的 WAVEHDR 的 C 代码定义,我我相信我可能在 C# 结构体定义中做错了一些事情:
/* wave data block header */
typedef struct wavehdr_tag {
LPSTR lpData; /* pointer to locked data buffer */
DWORD dwBufferLength; /* length of data buffer */
DWORD dwBytesRecorded; /* used for input only */
DWORD_PTR dwUser; /* for client's use */
DWORD dwFlags; /* assorted flags (see defines) */
DWORD dwLoops; /* loop control counter */
struct wavehdr_tag FAR *lpNext; /* reserved for driver */
DWORD_PTR reserved; /* reserved for driver */
} WAVEHDR, *PWAVEHDR, NEAR *NPWAVEHDR, FAR *LPWAVEHDR;
如果您习惯于使用所有这些低级工具(例如指针算术、强制转换等),那么开始编写托管代码是一件很痛苦的事情。这就像双手被绑在背上试图学习如何游泳一样。 我尝试过一些事情(没有效果): .NET 紧凑框架似乎不支持 [StructLayout] 中的 Pack = 2^x 指令。 我尝试了 [StructLayout(LayoutKind.Explicit)] 并使用 4 字节和 8 字节对齐。 4 字节对齐给了我与上面代码相同的结果,而 8 字节对齐只会让事情变得更糟 - 但这正是我所期望的。 有趣的是,如果我将代码从 setupBuffer 移到 setupWaveIn 中,并且不在类的上下文中声明 GCHandle,但在 setupWaveIn 的本地上下文中,回调函数返回的结构似乎没有损坏。但我不确定为什么会出现这种情况,以及如何利用这些知识来修复我的代码。 忘了吧。我将一些东西与我使用的更旧的代码混合在一起。
我真的很感谢有关编组、从 C# 调用非托管代码等方面的任何好的链接。如果有人能指出我的错误,我会非常高兴。我做错了什么?为什么我没有得到我所期望的。
I'm having trouble calling functions of a native library from within managed C# code. I am developing for the 3.5 compact framework (Windows Mobile 6.x) just in case this would make any difference.
I am working with the waveIn* functions from coredll.dll (these are in winmm.dll in regular Windows I believe). This is what I came up with:
// namespace winmm; class winmm
[StructLayout(LayoutKind.Sequential)]
public struct WAVEFORMAT
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct WAVEHDR
{
public IntPtr lpData;
public uint dwBufferLength;
public uint dwBytesRecorded;
public IntPtr dwUser;
public uint dwFlags;
public uint dwLoops;
public IntPtr lpNext;
public IntPtr reserved;
}
public delegate void AudioRecordingDelegate(IntPtr deviceHandle, uint message, IntPtr instance, ref WAVEHDR wavehdr, IntPtr reserved2);
[DllImport("coredll.dll")]
public static extern int waveInAddBuffer(IntPtr hWaveIn, ref WAVEHDR lpWaveHdr, uint cWaveHdrSize);
[DllImport("coredll.dll")]
public static extern int waveInPrepareHeader(IntPtr hWaveIn, ref WAVEHDR lpWaveHdr, uint Size);
[DllImport("coredll.dll")]
public static extern int waveInStart(IntPtr hWaveIn);
// some other class
private WinMM.WinMM.AudioRecordingDelegate waveIn;
private IntPtr handle;
private uint bufferLength;
private void setupBuffer()
{
byte[] buffer = new byte[bufferLength];
GCHandle bufferPin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
WinMM.WinMM.WAVEHDR hdr = new WinMM.WinMM.WAVEHDR();
hdr.lpData = bufferPin.AddrOfPinnedObject();
hdr.dwBufferLength = this.bufferLength;
hdr.dwFlags = 0;
int i = WinMM.WinMM.waveInPrepareHeader(this.handle, ref hdr, Convert.ToUInt32(Marshal.SizeOf(hdr)));
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInPrepare";
return;
}
i = WinMM.WinMM.waveInAddBuffer(this.handle, ref hdr, Convert.ToUInt32(Marshal.SizeOf(hdr)));
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInAddrBuffer";
return;
}
}
private void setupWaveIn()
{
WinMM.WinMM.WAVEFORMAT format = new WinMM.WinMM.WAVEFORMAT();
format.wFormatTag = WinMM.WinMM.WAVE_FORMAT_PCM;
format.nChannels = 1;
format.nSamplesPerSec = 8000;
format.wBitsPerSample = 8;
format.nBlockAlign = Convert.ToUInt16(format.nChannels * format.wBitsPerSample);
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
this.bufferLength = format.nAvgBytesPerSec;
format.cbSize = 0;
int i = WinMM.WinMM.waveInOpen(out this.handle, WinMM.WinMM.WAVE_MAPPER, ref format, Marshal.GetFunctionPointerForDelegate(waveIn), 0, WinMM.WinMM.CALLBACK_FUNCTION);
if (i != WinMM.WinMM.MMSYSERR_NOERROR)
{
this.Text = "Error: waveInOpen";
return;
}
setupBuffer();
WinMM.WinMM.waveInStart(this.handle);
}
I read alot about marshalling the last few days, nevertheless I do not get this code working. When my callback function is called (waveIn) when the buffer is full, the hdr structure passed back in wavehdr is obviously corrupted. Here is an examlpe of how the structure looks like at that point:
- wavehdr {WinMM.WinMM.WAVEHDR} WinMM.WinMM.WAVEHDR dwBufferLength 0x19904c00 uint dwBytesRecorded 0x0000fa00 uint dwFlags 0x00000003 uint dwLoops 0x1990f6a4 uint + dwUser 0x00000000 System.IntPtr + lpData 0x00000000 System.IntPtr + lpNext 0x00000000 System.IntPtr + reserved 0x7c07c9a0 System.IntPtr
This obiously is not what I expected to get passed. I am clearly concerned about the order of the fields in the view. I do not know if Visual Studio .NET cares about actual memory order when displaying the record in the "local"-view, but they are obviously not displayed in the order I speciefied in the struct.
Then theres no data pointer and the bufferLength field is far to high. Interestingly the bytesRecorded field is exactly 64000 - bufferLength and bytesRecorded I'd expect both to be 64000 though. I do not know what exactly is going wrong, maybe someone can help me out on this. I'm an absolute noob to managed code programming and marshalling so please don't be too harsh to me for all the stupid things I've propably done.
Oh here's the C code definition for WAVEHDR which I found here, I believe I might have done something wrong in the C# struct definition:
/* wave data block header */
typedef struct wavehdr_tag {
LPSTR lpData; /* pointer to locked data buffer */
DWORD dwBufferLength; /* length of data buffer */
DWORD dwBytesRecorded; /* used for input only */
DWORD_PTR dwUser; /* for client's use */
DWORD dwFlags; /* assorted flags (see defines) */
DWORD dwLoops; /* loop control counter */
struct wavehdr_tag FAR *lpNext; /* reserved for driver */
DWORD_PTR reserved; /* reserved for driver */
} WAVEHDR, *PWAVEHDR, NEAR *NPWAVEHDR, FAR *LPWAVEHDR;
If you are used to work with all those low level tools like pointer-arithmetic, casts, etc starting writing managed code is a pain in the ass. It's like trying to learn how to swim with your hands tied on your back.
Some things I tried (to no effect):
.NET compact framework does not seem to support the Pack = 2^x directive in [StructLayout].
I tried [StructLayout(LayoutKind.Explicit)] and used 4 bytes and 8 bytes alignment. 4 bytes alignmentgave me the same result as the above code and 8 bytes alignment only made things worse - but that's what I expected.Interestingly if I move the code from setupBuffer into the setupWaveIn and do not declare the GCHandle in the context of the class but in a local context of setupWaveIn the struct returned by the callback function does not seem to be corrupted. I am not sure however why this is the case and how I can use this knowledge to fix my code. Forget that. I mixed up stuff with much older code I used.
I'd really appreciate any good links on marshalling, calling unmanaged code from C#, etc. Then I'd be very happy if someone could point out my mistakes. What am I doing wrong? Why do I not get what I'd expect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 P/Invoke 声明是无可挑剔的。然而,您发布的 WAVEHDR 转储有一些非常的奇怪之处。它缺少 lpData 字段。如果插入,所有数字都会正确排列(即 lpData = 0x19904c00、dwBufferLength = 0x0000fa00 等)。
不知道这是怎么发生的,但它不知何故使用了错误的 WAVEHDR 声明。 WinMM.WinMM 应该是一个提示。
Your P/Invoke declarations are impeccable. However, there's something very odd about the WAVEHDR dump you posted. It is missing the lpData field. If you insert that, all the numbers line up properly (i.e. lpData = 0x19904c00, dwBufferLength = 0x0000fa00, etc).
Not sure how this came about, but it somehow is using the wrong WAVEHDR declaration. WinMM.WinMM ought to be a hint.
PInvoke.Net 是查找 PInvoke 声明的地方。
本页介绍了waveInAddBuffer方法及其C#等效方法,以及链接到 WAVEHDR。
我查看了您使用的各种方法,但找不到任何对您的情况有帮助的方法。 PInvoke.net 版本和您的版本的区别在于 PInvoke 使用 StructLayout 的 CharSet 属性,但我猜它不相关。
关于互操作性主题的一本好书是: NET-and-COM
PInvoke.Net is THE place to go for finding PInvoke declarations.
This page describes the waveInAddBuffer method and its C# equivalent, as well as links to WAVEHDR.
I had a look at various methods that you use but could not find anything helpful in your case. A difference with PInvoke.net's version and yours is that PInvoke uses the CharSet propery of StructLayout, but I guess it is not relevant.
A good book on the interoperability subject is: NET-and-COM
好吧,我明白了。我的所有代码基本上都是正确的。然而,我搞砸了 WAVEHDR 结构。 waveIn* 函数不仅期望引用 WAVEHDR 结构,还期望该结构持续存在,直到调用它为止。因此,WAVEHDR 结构需要在全局或至少足够大的上下文中创建,以维持直到调用它的 waveInHeaderUnprepare 为止,并且可能还需要使用 GCHandle 进行固定,这样它就不会改变它在内存中的位置(据我所知)在托管代码中不保证)。
这是我更新和清理的代码:
谢谢您的帮助。我希望有人可以使用我能想出的代码。
Ok, I figured it out. All my code was basicly correct. However, I screwed up with the WAVEHDR structure. The waveIn* functions not only expect a reference to an WAVEHDR structure but also expect this structure to sustain until waveInHeaderUnprepare is called upon it. So the WAVEHDR structure will need to be created in an global or at least big enough context to sustain untl waveInHeaderUnprepare is called on it and propably needs to be pinned aswell using a GCHandle, so that it will not change it's position in memory (which afaik is not guaranteed in managed code).
Here sis my updated and cleaned up code:
Thank you for your help. I hope someone can use the code I was able to come up with.