Reading from and writing to the Clipboard

Have you ever tried putting something in the clipboard? Well, aside from literally copying or pasting?
If you have ever tried something like this in .NET, you might remember, you need to call the functionality in a thread with STA mode. Try doing this in your NAV Development Environment…

Below, you will find a download for a Clipboard Handler in NAV. It allows you to set or read the clipboard. For achieving this, we compile a .NET DLL in memory and execute that one.

Please note that this Codeunit is provided “as is”.

OBJECT Codeunit 50000 Clipboard Handler
{
  OBJECT-PROPERTIES
  {
    Date=22/09/15;
    Time=13:57:38;
    Modified=Yes;
  }
  PROPERTIES
  {
    SingleInstance=Yes;
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Reflection.Assembly" RUNONCLIENT;

    LOCAL PROCEDURE [email protected]();
    BEGIN
      IF (NOT ISNULL(HandlerAssembly)) THEN
        EXIT;

      CreateAssembly(CreateAssemblyCode, HandlerAssembly);
    END;

    LOCAL PROCEDURE [email protected]([email protected] : Text;VAR [email protected] : DotNet 

"'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Reflection.Assembly" RUNONCLIENT);
    VAR
      [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.CodeDom.CodeSnippetCompileUnit" RUNONCLIENT;
      [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.Microsoft.CSharp.CSharpCodeProvider" RUNONCLIENT;
      [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.CodeDom.Compiler.CompilerParameters" RUNONCLIENT;
      [email protected] : DotNet "'System, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.CodeDom.Compiler.CompilerResults" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
    BEGIN

      CodeSnippetCompileUnit := CodeSnippetCompileUnit.CodeSnippetCompileUnit(CodeSnippet);

      CSharpCodeProvider := CSharpCodeProvider.CSharpCodeProvider;

      CompilerParameters := CompilerParameters.CompilerParameters;

      CompilerParameters.ReferencedAssemblies.Add('System.dll');
      CompilerParameters.ReferencedAssemblies.Add('System.Windows.Forms.dll');

      CompilerParameters.GenerateInMemory := TRUE;

      CodeSnippetCompileUnitArray := CodeSnippetCompileUnitArray.CreateInstance(GETDOTNETTYPE(CodeSnippetCompileUnit),1);
      CodeSnippetCompileUnitArray.SetValue(CodeSnippetCompileUnit, 0);

      CompilerResults := CSharpCodeProvider.CompileAssemblyFromDom(CompilerParameters, CodeSnippetCompileUnitArray);

      ResultAssembly := CompilerResults.CompiledAssembly;
    END;

    LOCAL PROCEDURE [email protected]() : Text;
    VAR
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Text.StringBuilder" RUNONCLIENT;
    BEGIN
      StringBuilder := StringBuilder.StringBuilder;
      StringBuilder.AppendLine('using System.Threading;');

      StringBuilder.AppendLine('namespace ClipboardHelper');
      StringBuilder.AppendLine('{');
      StringBuilder.AppendLine('    class ClipboardManager');
      StringBuilder.AppendLine('    {');
      StringBuilder.AppendLine('        private ClipboardHelper ch = new ClipboardHelper();');

      StringBuilder.AppendLine('        public string GetText()');
      StringBuilder.AppendLine('        {');
      StringBuilder.AppendLine('            Thread thr = new Thread(ch.GetText);');
      StringBuilder.AppendLine('            thr.SetApartmentState(ApartmentState.STA);');
      StringBuilder.AppendLine('            thr.IsBackground = false;');
      StringBuilder.AppendLine('            thr.Start();');
      StringBuilder.AppendLine('            thr.Join();');
      StringBuilder.AppendLine('            return ch.ClipboardText;');
      StringBuilder.AppendLine('        }');
      StringBuilder.AppendLine('        ');
      StringBuilder.AppendLine('        public void SetText(string ClipboardText)');
      StringBuilder.AppendLine('        {');
      StringBuilder.AppendLine('            ch.ClipboardText = ClipboardText;');
      StringBuilder.AppendLine('            Thread thr = new Thread(ch.SetText);');
      StringBuilder.AppendLine('            thr.SetApartmentState(ApartmentState.STA);');
      StringBuilder.AppendLine('            thr.IsBackground = false;');
      StringBuilder.AppendLine('            thr.Start();');
      StringBuilder.AppendLine('        }');
      StringBuilder.AppendLine('    }');

      StringBuilder.AppendLine('    class ClipboardHelper');
      StringBuilder.AppendLine('    {');
      StringBuilder.AppendLine('        public string ClipboardText { get; set; }');

      StringBuilder.AppendLine('        public void GetText()');
      StringBuilder.AppendLine('        {');
      StringBuilder.AppendLine('            ClipboardText = "";');
      StringBuilder.AppendLine('            if (System.Windows.Forms.Clipboard.ContainsText())');
      StringBuilder.AppendLine('                ClipboardText = System.Windows.Forms.Clipboard.GetText();');
      StringBuilder.AppendLine('        }');
      StringBuilder.AppendLine('        public void SetText()');
      StringBuilder.AppendLine('        {');
      StringBuilder.AppendLine('            System.Windows.Forms.Clipboard.SetText(ClipboardText);');
      StringBuilder.AppendLine('        }');
      StringBuilder.AppendLine('    }');
      StringBuilder.AppendLine('}');

      EXIT(StringBuilder.ToString);
    END;

    PROCEDURE [email protected]() : Text;
    VAR
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Activator" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type" 

RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Object" 

RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Reflection.MethodInfo" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
    BEGIN
      Initialize;

      Type := HandlerAssembly.GetType('ClipboardHelper.ClipboardManager');
      Object := Activator.CreateInstance(Type);
      MethodInfo := Type.GetMethod('GetText');

      ParamArray := ParamArray.CreateInstance(GETDOTNETTYPE(Object),0);

      Object := MethodInfo.Invoke(Object,ParamArray);

      EXIT(Object.ToString);
    END;

    PROCEDURE [email protected]([email protected] : Text);
    VAR
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Activator" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type" 

RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Object" 

RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Reflection.MethodInfo" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
    BEGIN
      Initialize;

      Type := HandlerAssembly.GetType('ClipboardHelper.ClipboardManager');
      Object := Activator.CreateInstance(Type);
      MethodInfo := Type.GetMethod('SetText');

      ParamArray := ParamArray.CreateInstance(GETDOTNETTYPE(Object),1);
      ParamArray.SetValue(NewClipboardText,0);

      MethodInfo.Invoke(Object, ParamArray);
    END;

    BEGIN
    END.
  }
}

By using Reflection, we build some .NET code inside NAV. Next we call the correct functions.

If you want to use this Codeunit, you merely call the SetClipboardText or GetClipboardText.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.