delphi - COM and Services -


i'm trying create service communicate com object controls on barcode scanner (model motorola symbol ls9208).

i created tlb unit ocx installed scanner driver application.

in delphi, i had create datamodule make work put start , stop code of scanner in serviceexecute procedure:

  1. initialize tscanner com object (declared in tlb)

  2. set scanner properties, "claim" control , set 1 tscanner event 1 of procedures in datamodule service fire when barcode readed

  3. after all, before close, release , free tscanner object

    procedure tintelipedcheckservice.serviceexecute(sender: tservice); begin    debuglog( 'initialization of service thread...' );    checkinitparams;    //datamodule_create;    startscanner;    //loopback   while (not terminated)   begin     sleep(100);     servicethread.processrequests(false);   end;    debuglog( 'end of service loop...' ); //only reached if stop service before read codbar    stopscanner;    //datamodule_free;    debuglog( 'finalization of service thread...' );  end;  procedure tintelipedcheckservice.startscanner; var hr : hresult; begin    //coinitialize( nil );   //coinitializeex( nil, coinit_multithreaded );   //coinitializeex( nil, coinit_multithreaded or coinit_disable_ole1dde );   //coinitializeex( nil, coinit_apartmentthreaded );   coinitializeex( nil, coinit_apartmentthreaded or coinit_disable_ole1dde );    // i've tried options above, without success    hr := coinitializesecurity(     nil, -1, nil, nil,     1, // rpc_c_authn_level_none     1, // rpc_c_imp_level_anonymous     nil,     0, // eoac_none     nil);    debuglog( 'hresult of coinitializesecurity: ' + inttostr( hr ) );   // log shows 0 hresult    scanner := tscanner.create( nil );    scanner.open( 'sti_usbscanner' );    if scanner.claim(0) = 0   begin      scanner.ondataevent := scannerdataevent;     //scanner.ondirectioevent := scannerdirectioevent;      scanner.deviceenabled    := true;     scanner.dataeventenabled := true;     scanner.freezeevents     := false;    end   else   begin     scanner.deviceenabled    := false;     scanner.dataeventenabled := false;     scanner.freezeevents     := false;   end;    //  caption := ifthen( scanner.claimed, 'ok', 'fail' );  end;  procedure tintelipedcheckservice.stopscanner; begin    if ( scanner <> nil )   begin      scanner.release;     scanner.free;      couninitialize();    end;  end;  procedure tintelipedcheckservice.scannerdataevent(asender: tobject; lstatus: integer); begin    debuglog( 'will fire...' ); //never reached    debuglog( 'fired: ' + scanner.scandata );    //scanner.deviceenabled    := false;   //sleep(1000);   scanner.deviceenabled    := true;   scanner.dataeventenabled := true;  end; 

when create datamodule run code exe application, works fine. i'm migrating funcionality windows service.

as first problem, error of "coinitialize not called" fired , nothing works.

so, tried coinitialize() in service's dpr, before application.initialize don't solve, , error keep raising.

the error gone when put coinitialize() before tscanner.create(nil), scanner starts , see laser ready read barcodes.

now scanner starts fine service start, , stop fine when service stops.

but if read barcode, , event ondataevent triggered, service halted, , no data received, (of course) lost control of scanner, needing restart service.

i read instructions of how coinitialize com objects (https://msdn.microsoft.com/en-us/library/windows/desktop/ff485844(v=vs.85).aspx) , found mention marshalling technique transport data between com objects , other threads. i'm not sure if problem... , don't know how that.

the service halt when scan barcode. if not, starts , stops without errors, , laser lights on when start, , light off when stop, demonstrating normal , correct command , operation.

please, 1 can me solve problem?

thanks in advance!

i once solved com trouble within nt-service calling coinitializesecurity right after coinitialize. wrote more here. apart important threading right. sure debugging 'normal' application version , using getcurrentthreadid incoming data scanner handled exact thread expect on.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -