我正在尝试使用(C)第三方库,该库具有回调机制,缺少任何可能的方法来识别调用上下文.我的主要项目是C#,我的包装器是一个C++/CLI项目来调用C库API.
若要解决此问题,我正在尝试使用Marshal :: GetFUnctionPointerForDelegate.这是我的C#代码:
void Init(Handler Callback)
{
// Create a wrapper lambda in case Callback is a non-static method.
instance.CreateBuffers((x, y) => Callback(x, y));
}
Run Code Online (Sandbox Code Playgroud)
然后,在C++/CLI代码中:
void CreateBuffers(Handler^ Callback)
{
pinCallback = GCHandle::Alloc(Callback);
void (*callback)(int, int) = (void (__stdcall *)(int, int))Marshal::GetFunctionPointerForDelegate(Callback).ToPointer(),
// Use 'callback' in third party library...
}
Run Code Online (Sandbox Code Playgroud)
所有这些的问题是根据http://msdn.microsoft.com/en-us/library/367eeye0.aspx,来自Marshal :: GetFunctionPointerForDelegate的函数指针是一个stdcall函数,但我的C回调是cdecl.如何在这里获得cdecl兼容功能?
我正在开发一个应用程序,我想动态生成用于数值计算的代码(用于性能).将此计算作为数据驱动操作太慢.要描述我的要求,请考虑以下类:
class Simulation
{
Dictionary<string, double> nodes;
double t, dt;
private void ProcessOneSample()
{
t += dt;
// Expensive operation that computes the state of nodes at the current t.
}
public void Process(int N, IDictionary<string, double[]> Input, IDictionary<string, double[]> Output)
{
for (int i = 0; i < N; ++i)
{
foreach (KeyValuePair<string, double[]> j in Input)
nodes[j.Key] = j.Value[i];
ProcessOneSample();
foreach (KeyValuePair<string, double[]> j in Output)
j.Value[i] = nodes[j.Key];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是JIT编译一个在Process中实现外部循环的函数.定义此函数的代码将由当前用于实现ProcessOneSample的数据生成.为了澄清我的期望,我希望所有的字典查找都在编译过程中执行一次(即JIT编译将直接绑定到字典中的相关对象),这样当编译的代码实际上是执行后,好像所有查找都已经过硬编码.
我想弄清楚的是解决这个问题的最佳工具.我问这个问题的原因是因为有很多选择: