[英]Late binding on CreateProcess with C# returns null value
我正在尝试对kernel32.dll中的CreateProcess函数使用后期绑定,但是,它与其他函数不同的是返回空值。
这是我用于后期绑定的代码
public abstract class LateBinding
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true), SuppressUnmanagedCodeSecurity()]
private static extern LBHandle LoadLibrary(string fileName);
[DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity()]
private static extern IntPtr GetProcAddress(LBHandle hModule, string procname);
private Delegate Result = default(Delegate);
public Delegate Call(string library, string method, Type type)
{
LBHandle Lib = LoadLibrary(library);
if (!Lib.IsInvalid && !Lib.IsClosed)
{
Result = Marshal.GetDelegateForFunctionPointer(GetProcAddress(Lib, method), type);
Lib.Close();
}
return Result;
}
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public sealed class LBHandle : SafeHandleZeroOrMinusOneIsInvalid
{
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeLibrary(IntPtr hModule);
private LBHandle() : base(true) { }
protected override bool ReleaseHandle()
{
return FreeLibrary(handle);
}
}
这就是我调用函数的方式
private delegate bool dCreateProcess(string applicationName, string commandLine, IntPtr processAttributes, IntPtr threadAttributes, bool inheritHandles, uint creationFlags, IntPtr environment, string currentDirectory, ref STARTUP_INFORMATION startupInfo, ref PROCESS_INFORMATION processInformation);
dCreateProcess CreateProcess = Call("kernel32.dll", "CreateProcess", typeof(dCreateProcess)) as dCreateProcess;
kernel32.dll
实际上并未导出名称为CreateProcess
的函数入口点-它是CreateProcessA
或您的情况下为Unicode(宽)参数的CreateProcessW
。
kernel32中没有名为CreateProcess
函数。 它有两个版本: CreateProcessA
(ANSI)和CreateProcessW
(Unicode)。 您可以在该按钮上看到MSDN上CreateProcess的文档 。
这不是CreateProcess
独有的,几乎每个使用字符串的Win32 API函数都将具有A
和W
版本。
以下是您想要的:
dCreateProcess CreateProcess = Call("kernel32.dll", "CreateProcessW", typeof(dCreateProcess)) as dCreateProcess;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.