I guess you want a generic method:
T CreateDelegate<T>(string procName) where T : class
{
IntPtr ptr;
ptr = Kernel32Methods.GetProcAddress (libHandle, procName);
if (ptr != IntPtr.Zero)
{
return (T)(object)Marshal.GetDelegateForFunctionPointer(ptr, typeof (T));
}
return null;
}
Unfortunately, you can't constrain T
to a delegate, so you first have to cast the result of GetDelegateForFunctionPointer
to object
before casting it to T
.
Usage:
createInstance = CreateDelegate<st_createInstance>("_soundtouch_createInstance@0");
destroyInstance = CreateDelegate<st_destroyInstance>("_soundtouch_destroyInstance@4");