[英]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.