这是一段代码:
private static bool CreateDelegates ()
{
IntPtr ptr;
//--- SoundTouch: createInstance
ptr = Kernel32Methods.GetProcAddress (libHandle, "_soundtouch_createInstance@0");
if (ptr != IntPtr.Zero)
{
createInstance = (st_createInstance) Marshal.GetDelegateForFunctionPointer
(ptr, typeof (st_createInstance));
}
//--- SoundTouch: destroyInstance
ptr = Kernel32Methods.GetProcAddress (libHandle, "_soundtouch_destroyInstance@4");
if (ptr != IntPtr.Zero)
{
destroyInstance = (st_destroyInstance) Marshal.GetDelegateForFunctionPointer
(ptr, typeof (st_destroyInstance));
}
}
在这种方法中还有更多类似上面的分配。我想创建像 AssignProc (...) 这样的方法来减少代码量。
void AssignProc (string procName, Delegate d, Type??? )
{
IntPtr ptr;
ptr = Kernel32Methods.GetProcAddress (libHandle, procName);
if (ptr != IntPtr.Zero)
{
d = (Type???) Marshal.GetDelegateForFunctionPointer
(ptr, typeof (???));
}
}
在哪里:
private static st_createInstance createInstance;
[UnmanagedFunctionPointer (CallingConvention.StdCall)]
private delegate IntPtr st_createInstance ();
帮助?:)