Problem with class VO.Console

This forum is meant for questions and discussions about the X# language and tools
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

Problem with class VO.Console

Post by leon-ts »

Hi,

Faced a problem in the work of the VO.Console class. The Write() method does not print text to the console. With ILSpy I investigated this method, and apparently the problem is in the FileStream.Write() method (FWrite -> XSharp.IO.File.writeBuf), with the console handle argument passed to it. The handle of console in the VO.Console class is obtained as: hConOut := GetStdHandle(STD_OUTPUT_HANDLE).

There are two problems that I noticed (but maybe I overlooked or misunderstood something):

1. The XSharp.IO.File.writeBuf method receives a FileStream object from the cache (streams dictionary) by handle (findStream() method), but does not add this key to streams anywhere. But maybe I just did not understand the code.
2. The FileStream class does not work with the console handle obtained using GetStdHandle(STD_OUTPUT_HANDLE). Throws an exception. If you pass it a handle obtained, for example, from CreateFile(), then FileStream works fine.

The WriteFile() function works with the console handle without any problems:

Code: Select all

hConOut := GetStdHandle(STD_OUTPUT_HANDLE)
WriteFile(hConOut, String2Psz("abc"), 3, @nWritten, NULL)
Best regards,
Leonid
Best regards,
Leonid
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Problem with class VO.Console

Post by robert »

Leonid,
The Console class uses FWrite() and uses a native Win32 file handle for the fWrite(), That is indeed an error.
A similar problem can be found in Console.Read()
We will change that in the next build.
I am not sure what the best solution is in this case. We can use the StandardInput, StandardError and StandardOutput properties from the Process class or use the Win32 functions to read/write to the native handles.
What would you prefer ?
Or should we rewrite the whole class on top of System.Console ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3759
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Problem with class VO.Console

Post by wriedmann »

Hi Robert,
Or should we rewrite the whole class on top of System.Console ?
IMHO that would be the best thing.
But for non-console applications there is a special API call to enable the console.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

Problem with class VO.Console

Post by leon-ts »

Robert,

On the one hand, using the System.Console class inside the VO.Console wrapper class will allow you to create pure .NET code for any platform. On the other hand, if I need pure .NET, I will just initially use the System.Console class in my code instead of VO.Console.

Given the above, as well as the fact that the VO.Console class is located in VOConsoleClasses.dll (a VO legacy for Win32), I would use the WriteFile() function in VO.Console.

Best regards,
Leonid
Best regards,
Leonid
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

Problem with class VO.Console

Post by leon-ts »

addition to my previous post:

VO.Console is very small class.
Therefore, rewriting it as a wrapper over System.Console class is also a good idea.
Best regards,
Leonid
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

Problem with class VO.Console

Post by leon-ts »

Hi, devteam

Is there any news to fix this error?

Best regards,
Leonid
Best regards,
Leonid
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Problem with class VO.Console

Post by robert »

Leonid,
leon_ts wrote:Hi, devteam

Is there any news to fix this error?

Best regards,
Leonid
I am sorry, but not yet.
I'll try to work on this later this week.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Problem with class VO.Console

Post by robert »

Leonid,

I have adjusted the VO Console classes to use System.Console internally.
You can find the new code here:
https://github.com/X-Sharp/XSharpPublic ... lasses_SDK

Can you check if this does what you expect ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

Problem with class VO.Console

Post by leon-ts »

Robert,
Many thanks!

A quick look at the code says this should work. But there is a problem with the TextAttribute property. In the current implementation it is incompatible with the VO version (Win32 parameters). In VO, the wNewAttr parameter represents the wAttributes parameter for the Win32 function SetConsoleTextAttribute(). Values are combinations that represent both the foreground color and the background color (defines FOREGROUND_ * + BACKGROUND_ *). I think the TextAttribute SET property in the updated class needs to be improved: select the colors from wNewAttr by mask and assign them through the System.ForegroundColor and System.BackgoundColor properties. Similar to GET.

Best regards,
Leonid
Best regards,
Leonid
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Problem with class VO.Console

Post by robert »

Leonid,

Do you want to contribute by writing this code for us ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply