.net 如何使用strongNameSignatureFromImage引用?

.net 如何使用strongNameSignatureFromImage引用?,.net,winapi,.net,Winapi,这种方法虽然“记录在案”,但似乎对我不起作用。 我正在尝试验证当前运行的可执行文件的签名。使用GetModuleHandle作为第一个参数(基址),并使用GetModuleInformation查找大小。 我不断返回CORSEC\u E\u无效的\u图像\u格式。 我还认为该方法可能需要清单的地址,因此我使用FindResource等来获取该地址,但它也不起作用。 有人成功地使用过这种方法吗 示例代码: HMODULE modHandle = GetModuleHandle(NULL); MOD

这种方法虽然“记录在案”,但似乎对我不起作用。 我正在尝试验证当前运行的可执行文件的签名。使用
GetModuleHandle
作为第一个参数(基址),并使用
GetModuleInformation
查找大小。 我不断返回
CORSEC\u E\u无效的\u图像\u格式
。 我还认为该方法可能需要清单的地址,因此我使用
FindResource
等来获取该地址,但它也不起作用。 有人成功地使用过这种方法吗

示例代码:

HMODULE modHandle = GetModuleHandle(NULL);
MODULEINFO modInfo;
GetModuleInformation(GetCurrentProcess(), modHandle, &modInfo, sizeof(MODULEINFO));
DWORD temp;
BOOL valid = StrongNameSignatureVerificationFromImage((BYTE*)modHandle, modInfo.SizeOfImage, 0, &temp);
DWORD res = StrongNameErrorInfo(); // returns CORSEC_E_INVALID_IMAGE_FORMAT

下面是我使用这个答案()中的AutoPiner类得出的关于
组装的结论:

其中NativeMethods为:

internal static class NativeMethods
{
    /// <summary>
    /// https://msdn.microsoft.com/en-us/library/ff844053(v=vs.110).aspx
    /// ICLRStrongName::StrongNameSignatureVerificationFromImage
    /// </summary>

    //Forces verification even if it is necessary to override registry settings
    public const int SN_INFLAG_FORCE_VER = 0x1;

    //Specifies that this is the first verification performed on this image
    public const int SN_INFLAG_INSTALL = 0x2;

    //Specifies that the cache will allow access only to users who have administrative privileges
    public const int SN_INFLAG_ADMIN_ACCESS = 0x4;

    //Specifies that the assembly will be accessible only to the current user
    public const int SN_INFLAG_USER_ACCESS = 0x8;

    //Specifies that the cache will provide no guarantees of access restriction
    public const int SN_INFLAG_ALL_ACCESS = 0x10;

    //This value is set to false to specify that the verification succeeded due to registry settings
    public const int SN_OUTFLAG_WAS_VERIFIED = 0x1;
}
内部静态类NativeMethods
{
/// 
/// https://msdn.microsoft.com/en-us/library/ff844053(v=vs.110).aspx
///ICLRStrongName::strongNameSignatureFromImage
/// 
//强制验证,即使有必要覆盖注册表设置
公共建筑内部消防力量=0x1;
//指定这是对此映像执行的第一次验证
公共const int SN\u INFLAG\u INSTALL=0x2;
//指定缓存仅允许具有管理权限的用户访问
公共const int SN\u INFLAG\u ADMIN\u ACCESS=0x4;
//指定程序集仅可由当前用户访问
public const int SN\u INFLAG\u USER\u ACCESS=0x8;
//指定缓存不保证访问限制
公共const int SN\u INFLAG\u ALL\u ACCESS=0x10;
//此值设置为false以指定由于注册表设置而成功验证
已验证公共常数int SNU OUTPLAG=0x1;
}

通过更多的catch子句和一些异常消息解析,您可以得到除
bool
result之外的结果。

根据文档,此方法已被弃用。看见此外,我认为该方法需要将程序集映射到应用程序的内存中。你到底想做什么?我也尝试了这个方法的新版本。同样的结果。我试图验证的程序集是正在执行的进程的程序集。很明显,它被映射到了内存中。我正在编写一个库,该库需要验证它是否从具有签名程序集的进程执行。数字签名还是强名称?强名称。另请参阅本文:相同的想法,只是我希望使用内存映射映像,而不是从磁盘访问程序集。我没有试过,但它们似乎很有帮助
internal static class NativeMethods
{
    /// <summary>
    /// https://msdn.microsoft.com/en-us/library/ff844053(v=vs.110).aspx
    /// ICLRStrongName::StrongNameSignatureVerificationFromImage
    /// </summary>

    //Forces verification even if it is necessary to override registry settings
    public const int SN_INFLAG_FORCE_VER = 0x1;

    //Specifies that this is the first verification performed on this image
    public const int SN_INFLAG_INSTALL = 0x2;

    //Specifies that the cache will allow access only to users who have administrative privileges
    public const int SN_INFLAG_ADMIN_ACCESS = 0x4;

    //Specifies that the assembly will be accessible only to the current user
    public const int SN_INFLAG_USER_ACCESS = 0x8;

    //Specifies that the cache will provide no guarantees of access restriction
    public const int SN_INFLAG_ALL_ACCESS = 0x10;

    //This value is set to false to specify that the verification succeeded due to registry settings
    public const int SN_OUTFLAG_WAS_VERIFIED = 0x1;
}