[英]How do I call a function defined in a C++ DLL that has a parameter of type int *, from inside C# code?
[英]C#: calling a DLL function that has 'int' as a parameter data type and 'string' as the return data type
這是我在這里的第一個問題。 我也是編程新手,所以我會盡力使其盡可能清晰。
有這個 DLL 最初是用 Delphi 編寫的。 它的一個函數需要一個int (Delphi 中的 Integer)作為其參數,然后返回一個字符串(Delphi 中的 PWideChar)。
DLL 沒有問題,因為我可以使用restype從 Python 成功調用此 function。 例如:
DLLName.FunctionName.restype = c_wchar_p
但是,當我從 C# (5.0) 調用下面的回調 function 時,每當我使用任何數字類型(例如 sbyte、int 或 long)時,我都能看到一些東西。 但是,當我嘗試使用字符串時出現錯誤,我認為這將是放置為其返回數據類型的適當數據類型:
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern sbyte GetNameByID(int nID);
這是我的第一次試驗:
public static string GetName()
{
string result = GetNameByID(1)
return result;
}
但是,正如我所提到的,當我使用數字數據類型而不是string時,我只會看到返回的內容。 我也(天真地)用.ToString()試用它或用(string)投射結果......
我接下來嘗試的是動態數據類型。
[DllImport(dll_path, CallingConvention = CallingConvention.StdCall)]
public static extern dynamic GetNameByID(int nID);
public static dynamic GetName()
{
dynamic result = GetNameByID(1);
return result;
}
我不確定我做錯了什么......如果有人可以提供幫助,我將不勝感激。
提前致謝。
我在使用 C++ DLL 時遇到了類似的問題。 也許這也適用於您的情況:
[DllImport(dll_path, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string getNameById(int nID);
public static string GetName()
{
string result = GetNameByID(1);
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.