XSharp service error

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

XSharp service error

Post by Juraj »

Hi All,
in XSharp console app this command work ok

File.Copy(xPathFile,"LPT3")

in XSharp service return error: System.IO.FileNotFoundException: Could not find file 'LPT3'.

LPT 3 is shared USB Pos printer (using Loopback Adapter)

Juraj
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

XSharp service error

Post by lumberjack »

Hi Juraj,
hsc wrote: in XSharp console app this command work ok

Code: Select all

File.Copy(xPathFile,"LPT3")
in XSharp service return error: System.IO.FileNotFoundException: Could not find file 'LPT3'.
LPT 3 is shared USB Pos printer (using Loopback Adapter)
The closest answer I could get:
For local printers...
File.Copy("xxx.txt","LPT1")
For network printers
File.Copy("xxx.txt","serverprintername")
For network shared printers you need to specify the printer name.
Try also with the third parameter, TRUE/FALSE:

Code: Select all

File.Copy("xxx.txt","serverprintername",TRUE)
File.Copy("xxx.txt","serverprintername", FALSE)
Good luck!
______________________
Johan Nel
Boshof, South Africa
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

XSharp service error

Post by Juraj »

Hi Johan,
thanks for advice.
The printer is connected on the same computer in which running the service app. If I enter a network name,(loacalhostEpson) the service will not report an error, but the printer will not print anything.
I think the problem is that the file.copy command is in service

Juraj
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

XSharp service error

Post by lumberjack »

Hi Juraj,
Ok understand, hopefully Robert or Chris have a better solution. Otherwise you might have to convert some of the examples on MSDN, codeproject, csharpcorner etc. Just google, there are plenty of examples all indicating to use the MS recommendation.
Regards and sorry that I could not help,
______________________
Johan Nel
Boshof, South Africa
User avatar
robert
Posts: 4523
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

XSharp service error

Post by robert »

Juraj,
I think the link between LPT3 and the printer port is user specific. Another user (and the System user) will not inherit your settings in this area.
If you create the link from your service app (try to run NET USE LPT3 serverprinter from the app) then it might work.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

XSharp service error

Post by lumberjack »

Hi Juraj,
Found this, not tested, sorry c#:

Code: Select all

using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace YourNamespace
{
public static class Print2LPT
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess,uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

            public static bool Print()
            {
                string nl = Convert.ToChar(13).ToString() + Convert.ToChar(10).ToString();
                bool IsConnected= false;

                string sampleText ="Hello World!" + nl +
                "Enjoy Printing...";     
                try
                {
                    Byte[] buffer = new byte[sampleText.Length];
                    buffer = System.Text.Encoding.ASCII.GetBytes(sampleText);

                    SafeFileHandle fh = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
                    if (!fh.IsInvalid)
                    {
                        IsConnected= true;                    
                        FileStream lpt1 = new FileStream(fh,FileAccess.ReadWrite);
                        lpt1.Write(buffer, 0, buffer.Length);
                        lpt1.Close();
                    }

                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                }

                return IsConnected;
            }
        }
}
Should not be too difficult to convert into X#, or just compile it as is an consume in your service.
HTH,
______________________
Johan Nel
Boshof, South Africa
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

XSharp service error

Post by Juraj »

Hi Johan and Rober,

Robert's advice was right, when I run NET USE LPT3..... from service app, command File.copy work OK.

Similar problems also have a command Directory.Getrfiles command from a shared directory on another PC
Command: Directory.GetFiles("OnotherPCSharedDir","Tab*.TXT")
this command in console app work ok, in service app return:
Error: System.IO.IOException: The specified server cannot perform the requested

Juraj
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

XSharp service error

Post by Juraj »

this is complete error message:

Error: System.IO.IOException: The specified server cannot perform the requested operation.

Juraj
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

XSharp service error

Post by wriedmann »

Hi Juraj,

a service is a completely different beast - many things that work in a normal Windows application will not work in services.
Specially when it comes to network connections a service is very limited, mostly for security reasons.

That has nothing to do with VO or X#or whatever tool you are using, but with how your service is configured and with Microsofts security measures.
A service running under the system account cannot access networked ressources, for example. To have a service access remote ressources, you have to create a proper user with the correct network access. Mappings will also not work, only UNC paths.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply