1. How do I convert this to Euphoria?
- Posted by bugmagnet Jan 14, 2013
- 1331 views
'http://vb.mvps.org/samples/project.asp?id=console Private Declare Function GetStdOutHandle Lib "kernel32" _ Alias "GetStdHandle" _ (Optional ByVal HandleType As Long = -11) As Long Private Declare Function WriteFile Lib "kernel32" _ (ByVal hFile As Long, _ ByVal lpBuffer As String, _ ByVal cToWrite As Long, _ ByRef cWritten As Long, _ Optional ByVal lpOverlapped As Long) As Long
I think I've figured out GetStdHandle, but WriteFile is still proving difficult. What's the declaration for this?
Kind regards,
Bugmagnet
2. Re: How do I convert this to Euphoria?
- Posted by jimcbrown (admin) Jan 14, 2013
- 1355 views
'http://vb.mvps.org/samples/project.asp?id=console Private Declare Function GetStdOutHandle Lib "kernel32" _ Alias "GetStdHandle" _ (Optional ByVal HandleType As Long = -11) As Long Private Declare Function WriteFile Lib "kernel32" _ (ByVal hFile As Long, _ ByVal lpBuffer As String, _ ByVal cToWrite As Long, _ ByRef cWritten As Long, _ Optional ByVal lpOverlapped As Long) As Long
I think I've figured out GetStdHandle, but WriteFile is still proving difficult. What's the declaration for this?
Kind regards,
Bugmagnet
Untested code below.
To define:
define_c_func(open_dll("kernel32.dll"), "GetStdOutHandle", {C_LONG}, C_LONG) define_c_func(open_dll("kernel32.dll"), "WriteFile", {C_LONG, C_POINTER, C_LONG, C_POINTER, C_LONG}, C_LONG)
To use:
atom handle = c_func(GetStdOutHandle_, {-11}) atom str = allocate_string(your_text) atom byref = allocate(4) -- or allocate(8) on 64bit if using pre-alpha 4.1.0 poke4(byref, 0) -- or poke8 if ... atom ret = c_func(WriteFile_, {handle, str, length(your_text), byref, 0}) free({str, byref})
EDIT: change empty string to str
3. Re: How do I convert this to Euphoria?
- Posted by mattlewis (admin) Jan 14, 2013
- 1395 views
'http://vb.mvps.org/samples/project.asp?id=console Private Declare Function GetStdOutHandle Lib "kernel32" _ Alias "GetStdHandle" _ (Optional ByVal HandleType As Long = -11) As Long Private Declare Function WriteFile Lib "kernel32" _ (ByVal hFile As Long, _ ByVal lpBuffer As String, _ ByVal cToWrite As Long, _ ByRef cWritten As Long, _ Optional ByVal lpOverlapped As Long) As Long
I think I've figured out GetStdHandle, but WriteFile is still proving difficult. What's the declaration for this?
I could tell you, but I think you'll prefer opening STDOUT as a file like so:
integer stdout = open("CONOUT$", "w" )
...and then using normal printf / puts with that file number.
PS: I think WriteFile would be wrapped like so:
WriteFile = define_c_func( KERNEL32, "WriteFileA", { C_POINTER, -- HANDLE C_POINTER, -- Pointer to allocated String C_INT, -- size of the buffer C_POINTER, -- pointer to an int that will tell you how many bytes were written C_POINTER }, -- pointer to an OVERLAPPED structure C_INT )
The last two parameters are optional, and you're probably fine just to pass a zero for both of them.
Matt
4. Re: How do I convert this to Euphoria?
- Posted by jimcbrown (admin) Jan 14, 2013
- 1306 views
I could tell you, but I think you'll prefer opening STDOUT as a file like so:
integer stdout = open("CONOUT$", "w" )
Doesn't this open the Console Output Window (ala euiw.exe behavior), rather than STDOUT?
E.g., it wouldn't work to write to STDOUT from a CGI app.
5. Re: How do I convert this to Euphoria?
- Posted by mattlewis (admin) Jan 14, 2013
- 1324 views
I could tell you, but I think you'll prefer opening STDOUT as a file like so:
integer stdout = open("CONOUT$", "w" )
Doesn't this open the Console Output Window (ala euiw.exe behavior), rather than STDOUT?
E.g., it wouldn't work to write to STDOUT from a CGI app.
Hmm...you might be right...still..simply running as a console app should work (as you previously noted). And if it doesn't, we need to fix it.
Matt
6. Re: How do I convert this to Euphoria?
- Posted by jimcbrown (admin) Jan 14, 2013
- 1293 views
simply running as a console app should work (as you previously noted).
Agreed.
And if it doesn't, we need to fix it.
Agreed, though I haven't seen enough evidence yet of a problem. I'm fairly confident that bugmagnet's issue was due to outputting that incorrect header. (Well, the cpu utilization issue turned out to be due to a "puts(1, gets(0))" call - that sounds like an issue.)
7. Re: How do I convert this to Euphoria?
- Posted by mattlewis (admin) Jan 14, 2013
- 1302 views
Agreed, though I haven't seen enough evidence yet of a problem. I'm fairly confident that bugmagnet's issue was due to outputting that incorrect header. (Well, the cpu utilization issue turned out to be due to a "puts(1, gets(0))" call - that sounds like an issue.)
I've had the header thing bite me before. I don't get any CPU utilization with a simple ? gets(0) on Linux or Windows, so I'm not sure what's going on there. Then again, that was within an actual console. Maybe running as CGI has something else going on.
Matt
8. Re: How do I convert this to Euphoria?
- Posted by bugmagnet Jan 14, 2013
- 1294 views
I'm fairly confident that bugmagnet's issue was due to outputting that incorrect header.
So it would seem. See my latest posting on that topic.
Bugmagnet
9. Re: How do I convert this to Euphoria?
- Posted by petelomax Jan 20, 2013
- 1170 views
I could tell you, but I think you'll prefer opening STDOUT as a file like so:
integer stdout = open("CONOUT$", "w" )
A belated thank you for that - implementing it in Phix lead to fixing a --long-- outstanding bug in file redirection.
Pete