1. How do I convert this to Euphoria?

'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

new topic     » topic index » view message » categorize

2. Re: How do I convert this to Euphoria?

bugmagnet said...

'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

new topic     » goto parent     » topic index » view message » categorize

3. Re: How do I convert this to Euphoria?

bugmagnet said...

'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

new topic     » goto parent     » topic index » view message » categorize

4. Re: How do I convert this to Euphoria?

mattlewis said...

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.

new topic     » goto parent     » topic index » view message » categorize

5. Re: How do I convert this to Euphoria?

jimcbrown said...
mattlewis said...

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: How do I convert this to Euphoria?

mattlewis said...

simply running as a console app should work (as you previously noted).

Agreed.

mattlewis said...

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.)

new topic     » goto parent     » topic index » view message » categorize

7. Re: How do I convert this to Euphoria?

jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: How do I convert this to Euphoria?

jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

9. Re: How do I convert this to Euphoria?

mattlewis said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu