Thread overview
Using Win32 console functions in D
May 26, 2005
Hasan Aljudy
May 26, 2005
Regan Heath
May 26, 2005
Hasan Aljudy
May 26, 2005
David L. Davis
May 26, 2005
Hasan Aljudy
May 26, 2005
I'm trying to use the function WriteConsoleOutput
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writeconsoleoutput.asp

I've read the "D for Win32" page
http://www.digitalmars.com/d/windows.html
and looked at
dmd\src\phobos\std\c\windows\windows.d

Then I wrote some code so that hopefully the compiler recognizes that
function as an external windows function.
###
extern(Windows)
{
	union _Char
	{
		WCHAR UnicodeChar;
		CHAR AsciiChar;
	}
	struct CHAR_INFO
	{
		_Char Char;
		WORD Attributes;
	}
	
	struct COORD
	{
		SHORT X;
		SHORT Y;
	}
	
	struct SMALL_RECT
	{
		SHORT Left;
		SHORT Top;
		SHORT Right;
		SHORT Bottom;
	}
	
	alias SMALL_RECT* PSMALL_RECT;
	
	export BOOL WriteConsoleOutput(
	  HANDLE hConsoleOutput,
	  CHAR_INFO* lpBuffer,
	  COORD dwBufferSize,
	  COORD dwBufferCoord,
	  PSMALL_RECT lpWriteRegion
	);		
}
###

so far it compiles with no error, but when I test it and call the function I get some linking error

###########
E:\dmd\dmd\bin\..\..\dm\bin\link.exe
test,d-test.exe,,user32+kernel32,test.def/noi;

OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

test.obj(test)
 Error 42: Symbol Undefined _WriteConsoleOutput@20

--- errorlevel 1
in32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

test.obj(test)
 Error 42: Symbol Undefined _WriteConsoleOutput@20
###########

my test code looks like this:

###
void main()
{
	COORD zero;
	zero.X = 0;
	zero.Y = 0;
	
	WriteConsoleOutput(
	cast(HANDLE)0,
	cast(CHAR_INFO*)0,
	zero,
	zero,
	cast(PSMALL_RECT)0);
}
###

Of course I am a n00b in these things ..
Is it somethign in D, the phobos library, or my code that is causing this?
Is it possible to overcome this error and be able to use WriteConsoleOutput?
May 26, 2005
Change "WriteConsoleOutput" to "WriteConsoleOutputA" or "WriteConsoleOutputW".

The former is the ASCII/ANSI version the latter the WIDE/UNICODE version.

If you use the former, you can typically get away with passing a char[] argument to it.
If you use the latter, you should pass a wchar[] to it.

You'll find the C function definitions you're actually linking to in dm\include\win32\wincon.h:

WINBASEAPI
BOOL
WINAPI
WriteConsoleOutputA(
    HANDLE hConsoleOutput,
    CONST CHAR_INFO *lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    PSMALL_RECT lpWriteRegion
    );
WINBASEAPI
BOOL
WINAPI
WriteConsoleOutputW(
    HANDLE hConsoleOutput,
    CONST CHAR_INFO *lpBuffer,
    COORD dwBufferSize,
    COORD dwBufferCoord,
    PSMALL_RECT lpWriteRegion
    );
#ifdef UNICODE
#define WriteConsoleOutput  WriteConsoleOutputW
#else
#define WriteConsoleOutput  WriteConsoleOutputA
#endif // !UNICODE

Regan


On Thu, 26 May 2005 00:43:21 -0600, Hasan Aljudy <hasan.aljudy@gmail.com> wrote:
> I'm trying to use the function WriteConsoleOutput
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writeconsoleoutput.asp
>
> I've read the "D for Win32" page
> http://www.digitalmars.com/d/windows.html
> and looked at
> dmd\src\phobos\std\c\windows\windows.d
>
> Then I wrote some code so that hopefully the compiler recognizes that
> function as an external windows function.
> ###
> extern(Windows)
> {
> 	union _Char
> 	{
> 		WCHAR UnicodeChar;
> 		CHAR AsciiChar;
> 	}
> 	struct CHAR_INFO
> 	{
> 		_Char Char;
> 		WORD Attributes;
> 	}
> 	
> 	struct COORD
> 	{
> 		SHORT X;
> 		SHORT Y;
> 	}
> 	
> 	struct SMALL_RECT
> 	{
> 		SHORT Left;
> 		SHORT Top;
> 		SHORT Right;
> 		SHORT Bottom;
> 	}
> 	
> 	alias SMALL_RECT* PSMALL_RECT;
> 	
> 	export BOOL WriteConsoleOutput(
> 	  HANDLE hConsoleOutput,
> 	  CHAR_INFO* lpBuffer,
> 	  COORD dwBufferSize,
> 	  COORD dwBufferCoord,
> 	  PSMALL_RECT lpWriteRegion
> 	);		
> }
> ###
>
> so far it compiles with no error, but when I test it and call the function I get some linking error
>
> ###########
> E:\dmd\dmd\bin\..\..\dm\bin\link.exe
> test,d-test.exe,,user32+kernel32,test.def/noi;
>
> OPTLINK (R) for Win32  Release 7.50B1
> Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved
>
> test.obj(test)
>   Error 42: Symbol Undefined _WriteConsoleOutput@20
>
> --- errorlevel 1
> in32  Release 7.50B1
> Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved
>
> test.obj(test)
>   Error 42: Symbol Undefined _WriteConsoleOutput@20
> ###########
>
> my test code looks like this:
>
> ###
> void main()
> {
> 	COORD zero;
> 	zero.X = 0;
> 	zero.Y = 0;
> 	
> 	WriteConsoleOutput(
> 	cast(HANDLE)0,
> 	cast(CHAR_INFO*)0,
> 	zero,
> 	zero,
> 	cast(PSMALL_RECT)0);
> }
> ###
>
> Of course I am a n00b in these things ..
> Is it somethign in D, the phobos library, or my code that is causing this?
> Is it possible to overcome this error and be able to use WriteConsoleOutput?
May 26, 2005
In article <d73r5n$1nst$1@digitaldaemon.com>, Hasan Aljudy says...
>
>I'm trying to use the function WriteConsoleOutput http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writeconsoleoutput.asp
>
>I've read the "D for Win32" page
>http://www.digitalmars.com/d/windows.html
>and looked at
>dmd\src\phobos\std\c\windows\windows.d
>
>...
>
>Of course I am a n00b in these things ..
>Is it somethign in D, the phobos library, or my code that is causing this?
>Is it possible to overcome this error and be able to use WriteConsoleOutput?

You can find a D converted version of wincon.h, renamed to wincon.d, off of my site...there's also two very useful examples included in the download. Just look for the "Fun with the NT / XP Console in D", which is the bottom entry of the following web page.

http://spottedtiger.tripod.com/D_Language/D_Hub_Adv_Tutor_XP.html

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
May 26, 2005
Regan Heath wrote:
> Change "WriteConsoleOutput" to "WriteConsoleOutputA" or  "WriteConsoleOutputW".
> 
> The former is the ASCII/ANSI version the latter the WIDE/UNICODE version.
> 
> If you use the former, you can typically get away with passing a char[]  argument to it.
> If you use the latter, you should pass a wchar[] to it.
> 
> You'll find the C function definitions you're actually linking to in  dm\include\win32\wincon.h:
> 
> WINBASEAPI
> BOOL
> WINAPI
> WriteConsoleOutputA(
>     HANDLE hConsoleOutput,
>     CONST CHAR_INFO *lpBuffer,
>     COORD dwBufferSize,
>     COORD dwBufferCoord,
>     PSMALL_RECT lpWriteRegion
>     );
> WINBASEAPI
> BOOL
> WINAPI
> WriteConsoleOutputW(
>     HANDLE hConsoleOutput,
>     CONST CHAR_INFO *lpBuffer,
>     COORD dwBufferSize,
>     COORD dwBufferCoord,
>     PSMALL_RECT lpWriteRegion
>     );
> #ifdef UNICODE
> #define WriteConsoleOutput  WriteConsoleOutputW
> #else
> #define WriteConsoleOutput  WriteConsoleOutputA
> #endif // !UNICODE
> 
> Regan
> 
> 

Ah! right!
lol, mybad .. WriteConsoleOutput is just an alias! no such function really exists.
May 26, 2005
David L. Davis wrote:
> In article <d73r5n$1nst$1@digitaldaemon.com>, Hasan Aljudy says...
> 
>>I'm trying to use the function WriteConsoleOutput
>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writeconsoleoutput.asp
>>
>>I've read the "D for Win32" page
>>http://www.digitalmars.com/d/windows.html
>>and looked at
>>dmd\src\phobos\std\c\windows\windows.d
>>
>>...
>>
>>Of course I am a n00b in these things ..
>>Is it somethign in D, the phobos library, or my code that is causing this?
>>Is it possible to overcome this error and be able to use WriteConsoleOutput?
> 
> 
> You can find a D converted version of wincon.h, renamed to wincon.d, off of my
> site...there's also two very useful examples included in the download. Just look
> for the "Fun with the NT / XP Console in D", which is the bottom entry of the
> following web page.
> 
> http://spottedtiger.tripod.com/D_Language/D_Hub_Adv_Tutor_XP.html
> 
> David L.
> 
> -------------------------------------------------------------------
> "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
> -------------------------------------------------------------------
> 
> MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html

That's nice. I think this should become part of phobos, after all, it IS defined in <windows.h>, so why isn't it in std.c.windows.windows?

walter? Do you read this?