[英]Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5
[英]C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int'
我不是C#的大鑒賞家。 如果有人可以幫助我,我將不勝感激:
我具有從Windows存儲恢復“日志\\密碼”的功能。
如果我在.Net 4.0環境中進行編譯,則一切正常,並且一切正常。
但是我想為.Net 3.5做這個功能和整個程序,在這里我在這些行中遇到錯誤:
var vaultIdPtr = vaultGuids + i * Marshal.SizeOf(typeof(Guid));
var itemPtr = items + i * Marshal.SizeOf(typeof(VAULT_ITEM_W8));
等等
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);
如果您查看版本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
}
我找不到版本3.5的實現,但是我猜想該運算符未在早期版本中實現。
您可以使用運算符+的代碼使其工作,如下所示:
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.