Added attempt on calling StatusUtf8. Doesn't seem to work.

This commit is contained in:
Eugene Wang
2018-11-16 22:29:57 -05:00
parent 61a2c907bd
commit a73df1320f
6 changed files with 79 additions and 91 deletions

View File

@@ -46,7 +46,7 @@ namespace ConsoleApp
if (targetSrc != null)
{
TestThisSource(targetSrc);
TestThisSource(session, targetSrc);
}
else
{
@@ -67,12 +67,16 @@ namespace ConsoleApp
Console.ReadLine();
}
private static void TestThisSource(DataSource targetSrc)
private static void TestThisSource(TwainSession session, DataSource targetSrc)
{
Console.WriteLine($"Testing data source {targetSrc}");
Console.WriteLine();
targetSrc.Open();
var testStatus = session.GetStatus();
var testMessage = session.GetLocalizedStatus(ref testStatus);
targetSrc.Close();
}

View File

@@ -513,12 +513,9 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2)]
partial struct TW_STATUSUTF8
{
// NOTE: rather than embedding the TWStatus directly I'm using its fields instead
// so the TWStatus could become a class object.
TW_UINT16 _conditionCode;
TW_UINT16 _data;
TW_UINT32 _size;
TW_HANDLE _uTF8string;
public TW_STATUS Status;
public TW_UINT32 Size;
public TW_HANDLE UTF8string;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]

View File

@@ -2215,89 +2215,6 @@ namespace NTwain.Data
public ushort Data { get { return _data; } }
}
// /// <summary>
// /// Translates the contents of Status into a localized UTF8string, with the total number of bytes
// /// in the string.
// /// </summary>
// public sealed partial struct TW_STATUSUTF8 : IDisposable
// {
// /// <summary>
// /// <see cref="TW_STATUS"/> data received from a previous call.
// /// </summary>
// public TW_STATUS Status
// {
// get { return new TW_STATUS(_conditionCode, _data); }
// }
// /// <summary>
// /// Total number of bytes in the UTF8string, plus the terminating NULL byte.
// /// This is not the same as the total number of characters in the string.
// /// </summary>
// public int Size { get { return (int)_size; } }
// /// <summary>
// /// TW_HANDLE to a UTF-8 encoded localized string (based on
// /// TW_IDENTITY.Language or CapLanguage). The Source allocates
// /// it, the Application frees it.
// /// </summary>
// public IntPtr UTF8StringPtr { get { return _uTF8string; } }
// /// <summary>
// /// Gets the actual string from the pointer. This may be incorrect.
// /// </summary>
// /// <returns></returns>
// public string TryGetString()
// {
// if (_uTF8string != IntPtr.Zero)
// {
// var sb = new StringBuilder(Size - 1);
// byte bt;
// while (sb.Length < _size &&
// (bt = Marshal.ReadByte(_uTF8string, sb.Length)) != 0)
// {
// sb.Append((char)bt);
// }
// return sb.ToString();
// }
// return null;
// }
// #region IDisposable Members
// /// <summary>
// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
// /// </summary>
// public void Dispose()
// {
// Dispose(true);
// GC.SuppressFinalize(this);
// }
// void Dispose(bool disposing)
// {
// if (disposing)
// {
// }
// if (_uTF8string != IntPtr.Zero)
// {
// PlatformInfo.Current.MemoryManager.Free(_uTF8string);
// _uTF8string = IntPtr.Zero;
// }
// }
// /// <summary>
// /// Finalizes an instance of the <see cref="TW_STATUSUTF8"/> class.
// /// </summary>
// ~TW_STATUSUTF8()
// {
// Dispose(false);
// }
// #endregion
// }
/// <summary>
/// Provides identification information about a TWAIN entity. Used to maintain consistent
/// communication between entities.

View File

@@ -0,0 +1,56 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using NTwain.Data;
using NTwain.Internals;
namespace NTwain.Triplets.Control
{
sealed class StatusUtf8 : BaseTriplet
{
internal StatusUtf8(TwainSession session) : base(session) { }
public ReturnCode Get(ref TW_STATUS status, out string message)
{
message = null;
var rc = ReturnCode.Failure;
TW_STATUSUTF8 real = new TW_STATUSUTF8 { Status = status };
if (Use32BitData)
{
rc = NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource?.Identity,
DataGroups.Control, DataArgumentType.StatusUtf8, Message.Get, ref real);
}
if (rc == ReturnCode.Success)
{
message = ReadUtf8String(ref real);
}
return rc;
}
private string ReadUtf8String(ref TW_STATUSUTF8 status)
{
if (status.UTF8string != IntPtr.Zero)
{
try
{
IntPtr lockedPtr = Session.Config.MemoryManager.Lock(status.UTF8string);
var buffer = new byte[status.Size - 1];
Marshal.Copy(lockedPtr, buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer);
}
finally
{
Session.Config.MemoryManager.Unlock(status.UTF8string);
Session.Config.MemoryManager.Free(status.UTF8string);
}
}
return null;
}
}
}

View File

@@ -33,6 +33,9 @@ namespace NTwain.Triplets
Status _status;
internal Status Status => _status ?? (_status = new Status(Session));
StatusUtf8 _statusUtf8;
internal StatusUtf8 StatusUtf8 => _statusUtf8 ?? (_statusUtf8 = new StatusUtf8(Session));
DeviceEvent _devEvent;
internal DeviceEvent DeviceEvent => _devEvent ?? (_devEvent = new DeviceEvent(Session));
}

View File

@@ -93,6 +93,17 @@ namespace NTwain
return stat;
}
/// <summary>
/// Gets the translated string for a <see cref="TW_STATUS"/>.
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public string GetLocalizedStatus(ref TW_STATUS status)
{
var rc = DGControl.StatusUtf8.Get(ref status, out string message);
return message;
}
internal void RegisterCallback()
{
var callbackPtr = Marshal.GetFunctionPointerForDelegate(_callbackDelegate);