Merge pull request #40 from soukoku/v4-support-bg-handling

Some more unverified tw_info reads ideas.
This commit is contained in:
Eugene Wang 2023-04-10 22:21:24 -04:00 committed by GitHub
commit f58bf3c620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -1040,8 +1040,9 @@ namespace NTwain.Data
/// </summary>
/// <param name="memMgr"></param>
/// <param name="index">If item is an array specify which string to read</param>
/// <param name="lengthHint">Pass a value if you know how long it should be.</param>
/// <returns></returns>
public unsafe string? ReadHandleString(IMemoryManager memMgr, int index = 0)
public unsafe string? ReadHandleString(IMemoryManager memMgr, int index = 0, int lengthHint = -1)
{
if (index < 0 || index >= NumItems || !IsDataAPointer) return default;
@ -1053,7 +1054,7 @@ namespace NTwain.Data
if (NumItems == 1)
{
// if 1, item is already the pointer to the string
value = LockAndReadNullTerminatedString(memMgr, itemAsPtr);
value = LockAndReadNullTerminatedString(memMgr, itemAsPtr, lengthHint);
}
else
{
@ -1062,17 +1063,17 @@ namespace NTwain.Data
lockPtr += (IntPtr.Size * index);
// is this even correct? I hope it is
var subItemPtr = Marshal.PtrToStructure<IntPtr>(lockPtr);
value = LockAndReadNullTerminatedString(memMgr, subItemPtr);
value = LockAndReadNullTerminatedString(memMgr, subItemPtr, lengthHint);
memMgr.Unlock(itemAsPtr);
}
return value;
}
private string? LockAndReadNullTerminatedString(IMemoryManager memMgr, IntPtr data)
private string? LockAndReadNullTerminatedString(IMemoryManager memMgr, IntPtr data, int lengthHint = -1)
{
var lockPtr = memMgr.Lock(data);
// yolo as ansi, should work in most cases
var value = Marshal.PtrToStringAnsi(lockPtr);
var value = lengthHint > 0 ? Marshal.PtrToStringAnsi(lockPtr, lengthHint) : Marshal.PtrToStringAnsi(lockPtr);
memMgr.Unlock(data);
return value;
}

View File

@ -439,6 +439,9 @@ namespace NTwain.Data
default:
throw new NotSupportedException($"Unsupported item type {type} for reading.");
// TODO: verify if needs to read int32 for small types
case TWTY.HANDLE:
intptr += IntPtr.Size * itemIndex;
return MarshalTo<TValue>(intptr);
case TWTY.INT8:
intptr += 1 * itemIndex;
if (isEnum)

View File

@ -548,6 +548,8 @@ namespace NTwain.Data
if (type == typeof(TW_STR128)) return TWTY.STR128;
if (type == typeof(TW_STR255)) return TWTY.STR255;
if (type == typeof(TW_FRAME)) return TWTY.FRAME;
if (type == typeof(IntPtr)) return TWTY.HANDLE;
if (type == typeof(UIntPtr)) return TWTY.HANDLE;
if (type.IsEnum)
{
@ -579,6 +581,10 @@ namespace NTwain.Data
default:
throw new NotSupportedException($"Unsupported item type {type} for writing.");
// TODO: for small types needs to fill whole int32 before writing?
case TWTY.HANDLE:
intptr += IntPtr.Size * itemIndex;
Marshal.StructureToPtr(value, intptr, false);
break;
case TWTY.INT8:
intptr += 1 * itemIndex;
//int intval = Convert.ToSByte(value);