5

I am trying to open a Posiflex USB cash drawer with C#. This the code (courtesy of someone else):

using System;
using System.Collections.Generic;
using Microsoft.PointOfService;

namespace MyNamespace
{
  public class CashDrawerClass
  {
    CashDrawer myCashDrawer;
    PosExplorer explorer;

    public CashDrawerClass()
    {
        explorer = new PosExplorer();
        DeviceInfo ObjDevicesInfo = explorer.GetDevice("CashDrawer", "RR-Drawer");
        myCashDrawer = explorer.CreateInstance(ObjDevicesInfo) as CashDrawer;
    }

    public void OpenCashDrawer()
    {
        myCashDrawer.Open();
        myCashDrawer.Claim(1000);
        myCashDrawer.DeviceEnabled = true;
        myCashDrawer.OpenDrawer();
        myCashDrawer.DeviceEnabled = false;
        myCashDrawer.Release();
        myCashDrawer.Close();
    }
  }
}

As soon as execution reaches the code:

explorer = new PosExplorer(), the following exception is thrown:

An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll.

If it helps, here is the stack trace:

 at Microsoft.PointOfService.Pos4NetTelemetry.IsCeipOptInEnabled()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Microsoft.PointOfService.Pos4NetTelemetry.get_Enabled()
   at Microsoft.PointOfService.Pos4NetTelemetry.SetCurrentProcessBitness()
   at Microsoft.PointOfService.PosExplorer.Initialize()
   at Microsoft.PointOfService.PosExplorer..ctor()
   at RunningRabbit.CashDrawerClass..ctor() in c:\C# Development\RunningRabbit\RunningRabbit\CashDrawerClass.cs:line 14
   at RunningRabbit.MainPOS.btnOpenDrawer_Click(Object sender, EventArgs e) in c:\C# Development\RunningRabbit\RunningRabbit\MainPOS.cs:line 261
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
   at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at RunningRabbit.Program.Main() in c:\C# Development\RunningRabbit\RunningRabbit\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Any help will be appreciated.

PeterJ
  • 364
  • 4
  • 17
  • [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Sep 03 '15 at 12:08
  • Soner, I understand null references in general, but I fail to figure out why, in this specific instance, I get the error. – PeterJ Sep 03 '15 at 12:11
  • Can we see your `PosExplorer` as well? – Soner Gönül Sep 03 '15 at 12:14
  • PosExplorer is part of Microsoft.PointOfService library – Pedro Isaaco Sep 03 '15 at 12:16
  • As far as I know, PosExplorer is a class in the PointOfService library, so it should be instantiated like in the code. – PeterJ Sep 03 '15 at 12:17
  • Which POS4.NET version do you use? We found that v1.14preview has some very odd behaviours with the PosExplorer (e.g. not discovering FiscalPrinters) – LInsoDeTeh Sep 03 '15 at 12:22
  • This is a library problem. The crash is in IsCeipOptInEnabled. Decompile that code. – usr Sep 03 '15 at 12:47
  • Maybe you could get a more relevant answer in the [POS.Net forum](https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=posfordotnet) – cleftheris Sep 03 '15 at 12:48
  • 1
    @PeterJ did you ever find why this would happen? I am having the same issue but it only happens on one specific computer - code works on a bunch of other machines. – James Reategui Jun 28 '16 at 17:20

1 Answers1

5

You see it in the stack trace:

PosExplorer on initilization checks weather you allowed it to send telemetry to Microsoft during instalation of POS for .NET. The code is trying to read this property:

RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\POSfor.Net\\Setup").GetValue("CeipOptIn", (object) 0);

So if you don't have POS for .NET instaled, this Subkey isn't in your registry and then exception is thrown.

Note:

Telemetry is contained only in 1.14 version of POS for .NET. I suggest to use version 1.12 which has same functions and is without telemetry.

Gh61
  • 9,222
  • 4
  • 28
  • 39