0
我知道這已經討論過了,但3天后我還沒有想通了,爲什麼我把調用C函數包裹在DLL中的C#後得到一個空字符串:編組的char *到StringBuilder的在C#
[DllImport(DllName, CharSet = CharSet.Ansi)]
public static extern int QA_FormatExample(int viHandle, int viExample [MarshalAs(UnmanagedType.LPStr)]StringBuilder rsComment)
在主營:
StringBuilder rsComment = new StringBuilder(256);
apiWrap.QA_FormatExample(currentInstance, viExample, rsComment);
System.Diagnostics.Debug.WriteLine(rsComment);
函數的簽名:
int __stdcall QA_FormatExample(int, int, char*);
功能QA_FormatExample
初始化爲rsComment
曾經打過電話,但我一直收到rsComment
空白。任何建議將非常感激,謝謝。
它確實在C代碼中設置了嗎? – rhughes
C函數如何導出? C默認爲'cdecl'調用約定,C#默認爲'stdcall' - 如果你在C函數中沒有指定'__stdcall'作爲調用約定,那麼你必須在'DllImport'中指定'CallingConvention.Cdecl' 。 –
謝謝你的回答。 Rughes - 是的,C代碼明確地設置了'rsComment',它是我公司產品之一的API函數,因此它已經被測試。 J - C函數的完整簽名是:__declspec(dllimport)int __stdcall QA_FormatExample(int,int,char *),所以它已經被指定爲'stdcall'。 – user3215290