這裏是對付它的辦法。它可能適用於您的情況,具體取決於您的回調參數是否與printf
函數系列一起使用。
首先,進口vsprintf
和_vscprintf
從msvcrt
:
[DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int vsprintf(
StringBuilder buffer,
string format,
IntPtr args);
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int _vscprintf(
string format,
IntPtr ptr);
接下來,IntPtr
ARGS指針聲明你的委託:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public unsafe delegate void LogCallback(
void* arg1,
int level,
[In][MarshalAs(UnmanagedType.LPStr)] string fmt,
IntPtr args);
現在,當您的代理是通過本地代碼調用,只需使用vsprintf
正確格式化消息:
private void LogCallback(void* data, int level, string fmt, IntPtr args)
{
var sb = new StringBuilder(_vscprintf(fmt, args) + 1);
vsprintf(sb, fmt, args);
//here formattedMessage has the value your are looking for
var formattedMessage = sb.ToString();
...
}
[This](http://www.ownedcore.com/forums/world-of-warcraft/world-of-warcraft-bots-programs/wow-memory-editing/301588-how-hooking-functions-wow- variable-arguments-c.html)可能會有用。 – IllidanS4