簡體   English   中英

Win32 API waveInGetErrorText聲明進入C#

[英]Win32 API waveInGetErrorText declaration into C#

需要打電話

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

從C#代碼開始。 在PINVOKE.net上找不到聲明。 功能的描述在這里

MMRESULT只是一個uint值。 0-沒有錯誤。任何其他值,直到20-都是錯誤。

嘗試了很多模式。 始終取回一個空字符串。

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

通話時。

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);

返回字符串始終為空,保留128個字節的空格。 嘗試refout但沒有成功。

請問為什么?

您的定義:

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

...還不足以告訴.NET IntPtr pszText正在輸出任何內容。 默認情況下,編譯器會使用[in] marshal屬性修飾所有參數。 因此,您的上述定義等同於:

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

現在,如果您有如下定義:

int CountRecords (out int numErrors)

...編譯器將其轉換為:

int CountRecords ([out] out int numErrors)

...我相信。 無論如何, out (兩種形式)都告訴編譯器從p調用中獲得期望。 由於您未指定out[out] ,因此在調用完成之后,不會將任何內容傳遞回參數中。

現在您可以使用out string但是StringBuilder更容易。 .NET將某些類型(如StringBuilder識別為“嘿,對於這個參數,我希望有回報” out是可選的。

將您的定義更改為:

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

我相信您應該這樣稱呼它:

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

告訴我更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM