Windows interface

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sun Jul 2 14:03:22 PDT 2006


Karl Bochert wrote:
> I have been attempting to access the windows api from D, and there seem to be
> api calls missing from std.windows.windows.
> 
> I have added to phobos:
> export   HANDLE GetStdHandle(DWORD dev);
> export   DWORD GetConsoleTitle(LPSTR buf, DWORD len);
> export   BOOL AllocConsole();
> 
> but when I compile a program using them, I get from the linker:
> winapp.obj(winapp)
> Error 42: Symbol Undefined _GetConsoleTitle at 8
> 
> Why does GetConsoleTitle() fail when the other 2 don't??
> 
> 

Simple.  Because there is no such function as GetConsoleTitle().  There are, however, both 
GetConsoleTitleA() for ANSI and GetConsoleTitleW() for Unicode.  This is a common 
implementation detail throughout the Win32 API.  In C/C++ there is a macro defined for 
each such function as the base name without the ANSI/Unicode suffix, as nothing more than 
a transparent wrap to one of the implementations.

Something like:

[code]
void SomeFuncA(HANDLE, LPSTR);
void SomeFuncW(HANDLE, LPWSTR);

#ifdef _UNICODE_
#define SomeFunc(_A, _B) SomeFuncW(_A, _B)
#else
#define SomeFunc(_A, _B) SomeFuncA(_A, _B)
#endif
[/code]

Simply change your additions to the following:
# export { extern (Windows) {
#   BOOL   AllocConsole     (            ) ;
#   DWORD  GetConsoleTitleA (LPSTR, DWORD) ;
#   HANDLE GetStdHandle     (DWORD       ) ;
# }}

Note that you don't neccessarily need the 'export' attribute -- so far as I am aware 
anyhow -- and also that you don't have to add these to Phobos.  Instead, you may declare 
them in an import module in your own project.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list