简体   繁体   English

C#运算符'+'不能应用于类型'IntPtr'和'int'的操作数

[英]C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int'

I'm not a big connoisseur of C #. 我不是C#的大鉴赏家。 I would be very grateful if anyone could help me with this: 如果有人可以帮助我,我将不胜感激:

I have the function of recovering the "log \\ password" from the Windows store. 我具有从Windows存储恢复“日志\\密码”的功能。

If I compile in a .Net 4.0 environment, then everything is fine and everything works. 如果我在.Net 4.0环境中进行编译,则一切正常,并且一切正常。

But I would like to make this function and the program as a whole for .Net 3.5 and here I get an error in these lines: 但是我想为.Net 3.5做这个功能和整个程序,在这里我在这些行中遇到错误:

var vaultIdPtr = vaultGuids + i * Marshal.SizeOf(typeof(Guid));

var itemPtr = items + i * Marshal.SizeOf(typeof(VAULT_ITEM_W8));

and so on 等等

CS0019 C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int' CS0019 C#运算符'+'不能应用于类型'IntPtr'和'int'的操作数

 public static List<RecoveredAccount> ReadVaultPasswords()
    {

        List<RecoveredAccount> ReadVault = new List<RecoveredAccount>();

        if (!(Environment.OSVersion.Version.Major > 6 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1)))
        {
            // continue;
        }

        // var Vault_WebCredential_ID = Guid.Parse("3CCD5499-87A8-4B10-A215-608888DD3B55");

        Guid Vault_WebCredential_ID = new Guid("3CCD5499-87A8-4B10-A215-608888DD3B55");

        const uint ERROR_SUCCESS = 0;
        const uint VAULT_ENUMERATE_ALL_ITEMS = 512;

        bool isWin8 = Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor > 1;

        int vaultCount;
        IntPtr vaultGuids = IntPtr.Zero;
        try
        {
            if (VaultEnumerateVaults(0, out vaultCount, out vaultGuids) == ERROR_SUCCESS)
            {
                for (var i = 0; i < vaultCount; ++i)
                {
                    IntPtr vault = IntPtr.Zero;
                    try
                    {
                        var vaultIdPtr = vaultGuids + i * Marshal.SizeOf(typeof(Guid));


                        if (VaultOpenVault(vaultIdPtr, 0, out vault) == ERROR_SUCCESS)
                        {
                            int itemCount;
                            IntPtr items = IntPtr.Zero;
                            try
                            {
                                if (VaultEnumerateItems(vault, VAULT_ENUMERATE_ALL_ITEMS, out itemCount, out items) == ERROR_SUCCESS)
                                {
                                    for (var j = 0; j < itemCount; ++j)
                                    {
                                        string host;
                                        string username;
                                        string password = string.Empty;
                                        DateTime date = DateTime.Now;
                                        if (isWin8)
                                        {
                                            var itemPtr = items + i * Marshal.SizeOf(typeof(VAULT_ITEM_W8));


                                            var item = (VAULT_ITEM_W8)Marshal.PtrToStructure(itemPtr, typeof(VAULT_ITEM_W8));
                                            if (!Vault_WebCredential_ID.Equals(new Guid(item.SchemaId)))
                                            {
                                                continue;
                                            } ...etc


    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall)]
    private static extern uint VaultEnumerateVaults(uint dwFlags, out int VaultsCount, out IntPtr ppVaultGuids);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall)]
    private static extern uint VaultEnumerateItems(IntPtr pVaultHandle, uint dwFlags, out int ItemsCount, out IntPtr ppItems);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall, EntryPoint = "VaultGetItem")]
    private static extern uint VaultGetItem7(IntPtr pVaultHandle, IntPtr pSchemaId, IntPtr pResource, IntPtr pIdentity, IntPtr hwndOwner, uint dwFlags, out IntPtr ppItems);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall, EntryPoint = "VaultGetItem")]
    private static extern uint VaultGetItem8(IntPtr pVaultHandle, IntPtr pSchemaId, IntPtr pResource, IntPtr pIdentity, IntPtr pPackageSid, IntPtr hwndOwner, uint dwFlags, out IntPtr ppItems);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall)]
    private static extern uint VaultOpenVault(IntPtr pVaultId, uint dwFlags, out IntPtr pVaultHandle);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall)]
    private static extern uint VaultCloseVault(IntPtr pVaultHandle);

    [DllImport(VAULTCLI_DLL, CallingConvention = CallingConvention.StdCall)]
    private static extern uint VaultFree(IntPtr pMemory);

If you look at the source for IntPtr for version 4.7.2( https://referencesource.microsoft.com/#mscorlib/system/intptr.cs ) you'll see that there's an implementation of the operator + 如果您查看版本4.7.2( https://referencesource.microsoft.com/#mscorlib/system/intptr.cs )的IntPtr的源,您会发现其中存在一个运算符+

public static IntPtr operator +(IntPtr pointer, int offset) 
    {
        #if WIN32
            return new IntPtr(pointer.ToInt32() + offset);
        #else
            return new IntPtr(pointer.ToInt64() + offset);
        #endif
    }

I can't find the implementation for the version 3.5, but I guess that this operator is not implemented in earlier versions. 我找不到版本3.5的实现,但是我猜想该运算符未在早期版本中实现。

You can make it work using the code for the operator +, like this: 您可以使用运算符+的代码使其工作,如下所示:

    static void Main(string[] args)
    {
        IntPtr vaultGuids = IntPtr.Zero;
        int i = 10;

// Works in .net 4.0
        var vauldIdPtr = vaultGuids + i * Marshal.SizeOf(typeof(Guid));
        Console.WriteLine(vauldIdPtr);

// works in .net 4.0 and 3.5
        var ptr2 = new IntPtr(vaultGuids.ToInt32() + i * Marshal.SizeOf(typeof(Guid)));
        Console.WriteLine(ptr2);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符+不能应用于IntPtr和int类型的操作数-.Net 3.5 - Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5 C#运算符&#39;/&#39;不能应用于&#39;方法组&#39;和&#39;int类型的操作数 - C# Operator '/' cannot be applied to operands of type 'method group' and 'int C#:operator&#39; - &#39;不能应用于&#39;string&#39;和&#39;int&#39;类型的操作数错误 - C#: operator '-' cannot be applied to operands of type 'string' and 'int' error CS0019 C# 运算符“||” 不能应用于“int”和“int”类型的操作数 - CS0019 C# Operator '||' cannot be applied to operands of type 'int' and 'int' 运算符“ &amp;&amp;”不能应用于类型为“ int”和“ int”的操作数 - Operator “&&” cannot be applied to operands of type 'int' and 'int' c#-Linq错误“运算符&#39;==&#39;无法应用于类型为&#39;int&#39;和&#39;System.Linq.IQueryable的操作数 <T> ”和RemoveRange - c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange C#:运算符&#39;&gt; =&#39;不能应用于类型&#39;System.DateTime?&#39;的操作数 和&#39;int&#39; - C#: Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int' 为什么 c# “&amp;&amp;” 运算符会给出“不能应用于'bool'和'int'类型的操作数”的错误 - Why does c# “&&” operator give this error of “cannot be applied to operands of type 'bool' and 'int'” C#Linq的使用IsNull()的位置运算符&#39;&amp;&amp;&#39;不能应用于&#39;bool&#39;和&#39;int?&#39;类型的操作数 - C# Linq use Where with IsNull() Operator '&&' cannot be applied to operands of type 'bool' and 'int?' ASP.NET C#错误:运算符*不能应用于类型为&#39;int&#39;和&#39;string&#39;的操作数 - ASP.NET C# error: Operator * cannot be applied to operands of type 'int' and 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM