简体   繁体   English

Win32 API waveInGetErrorText声明进入C#

[英]Win32 API waveInGetErrorText declaration into C#

Need to call 需要打电话

MMRESULT waveInGetErrorText(
   MMRESULT mmrError,
   LPTSTR   pszText,
   UINT     cchText
);

From C# code. 从C#代码开始。 Didn't find the declaration at PINVOKE.net. 在PINVOKE.net上找不到声明。 The description of the func is here 功能的描述在这里

MMRESULT is just an uint value. MMRESULT只是一个uint值。 0 - no error.. any other value up to 20 - are errors. 0-没有错误。任何其他值,直到20-都是错误。

Tried lot of patterns. 尝试了很多模式。 Always get back an empty string. 始终取回一个空字符串。

[DllImport("Winmm", EntryPoint = "waveInGetErrorText")]
public static extern uint waveInGetErrorText(uint mmrError, IntPtr pszText, uint cchText);

When call. 通话时。

uint r = 12 // for example return value is 12
uint szErrorMessage = 128;
string errorMessage = new string(' ', (int) szErrorMessage);
IntPtr strPtr = Marshal.StringToBSTR(errorMessage);

r = waveInGetErrorText(r, strPtr, szErrorMessage);

The return string is always empty reserved 128 bytes of spaces. 返回字符串始终为空,保留128个字节的空格。 Tried ref and out without success.. 尝试refout但没有成功。

Pls, any idea why? 请问为什么?

Your definiton of: 您的定义:

public static extern uint waveInGetErrorText(uint mmrError, 
                                             IntPtr pszText, 
                                             uint cchText);

...isn't enough to tell .NET that IntPtr pszText is outputting anything. ...还不足以告诉.NET IntPtr pszText正在输出任何内容。 By default all paramaters are decorated with [in] marshal attributes by the compiler. 默认情况下,编译器会使用[in] marshal属性修饰所有参数。 So your above definiton is equivalent to: 因此,您的上述定义等同于:

public static extern uint waveInGetErrorText([in] uint mmrError, 
                                             [in] IntPtr pszText, 
                                             [in] uint cchText);

Now if you had a definition like: 现在,如果您有如下定义:

int CountRecords (out int numErrors)

...the compiler turns that into: ...编译器将其转换为:

int CountRecords ([out] out int numErrors)

...I believe. ...我相信。 Regardless, the out (either form) tells the compiler to expect something from the p-invoke. 无论如何, out (两种形式)都告诉编译器从p调用中获得期望。 Because you did not specify out or [out] , nothing is passed back into the parameter after completion of the call. 由于您未指定out[out] ,因此在调用完成之后,不会将任何内容传递回参数中。

Now you could use out string but StringBuilder is easer. 现在您可以使用out string但是StringBuilder更容易。 Some types like StringBuilder are recogised by .NET as "hey, for this parameter I expect something in return" . .NET将某些类型(如StringBuilder识别为“嘿,对于这个参数,我希望有回报” The out is optional. out是可选的。

Change your definition to: 将您的定义更改为:

[DllImport("Winmm", EntryPoint = "waveInGetErrorText")]
public static extern uint waveInGetErrorText(uint mmrError, StringBuilder text, uint cchText);

I believe you should be calling it like so: 我相信您应该这样称呼它:

uint r = 12 // for example return value is 12
StringBuilder data = new StringBuilder(255);
r = waveInGetErrorText(r, data, data.Capacity);

Tell me more 告诉我更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM