我正在从事 Amibroker C# 插件项目。 Amibroker SDK 是用 C++ 编写的,但我使用的是 C# 插件,它的功能与 Amibroker C++ 的功能完全相同 C# Plugin link
C# 插件中的所有东西都工作正常,除了一个用 c++ 编写的函数是这样的:
PLUGINAPI struct RecentInfo* GetRecentInfo(LPCTSTR ticker)
{
//Process & return RecentInfo* (RecentInfo is a structure)
}
在C#标准插件中是这样转换的
public RecentInfo GetRecentInfo(string ticker)
{
//Process & Return RecentInfo
}
不幸的是,Amibroker 应用程序因这种错误的转换而崩溃。所以我确实尝试将其转换为让 Amibroker 应用程序正常工作的方式,但多次失败 这是我到目前为止尝试过的:
尝试 1:
unsafe public RecentInfo* GetRecentInfo(string ticker)
{
//Process & Return RecentInfo* (RecentInfo structure is declared as unsafe)
}
影响:
Amibroker 应用程序未加载
尝试 2:
public IntPtr GetRecentInfo(string ticker)
{
//Process & Return Pointer using Marshal.StructureToPtr
}
影响:
Amibroker 应用程序未加载
尝试 3:
public void GetRecentInfo(string ticker)
{
//Useless becoz no return type
}
影响:
Amibroker 加载并正确调用函数,但如何返回结构指针
于是,我绞尽脑汁想找出C#中C++函数的准确转换
最佳答案
如果完全是用c#写的,这样就好了,觉得是实现上的问题,不是调用上的
public RecentInfo GetRecentInfo(string ticker)
{
RecentInfo rc;
//Process & Return RecentInfo
return rc;
}
或者这个,(你也可以使用 ref)
public void GetRecentInfo(string ticker,out RecentInfo rc )
{
rc=new RecentInfo();
....process
return ;
}
关于c# - 我如何在 C# 函数中转换 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439582/