www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you call GetVersionEx (Windows)?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
There's an example of a module constructor in TDPL. I don't think it's supossed
to compile as it is, but I wanted to try it out anyway.

I need a way to call the windows c function GetVersionEx. A grep through the
source files doesn't find it (except in the DMD cpp backend which it internally
uses), but there's one GetVersion function in core\sys\windows\windows.d. That
seems to work and returns a uint representation, but then I have to do
bitmasking to get all the info out of it. Yikes!

The TDPL example does actually use GetVersionEx() and calls it with a
"OSVERSIONINFOEX" structure argument, and it's all nicely documented on MSDN.
But I've no idea if I can call it from D. Anyone doing Windows programming in
D? :)
Aug 22 2010
next sibling parent "Yao G." <nospamyao gmail.com> writes:
On Sun, 22 Aug 2010 14:36:50 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 There's an example of a module constructor in TDPL. I don't think it's  
 supossed to compile as it is, but I wanted to try it out anyway.

 I need a way to call the windows c function GetVersionEx. A grep through  
 the source files doesn't find it (except in the DMD cpp backend which it  
 internally uses), but there's one GetVersion function in  
 core\sys\windows\windows.d. That seems to work and returns a uint  
 representation, but then I have to do bitmasking to get all the info out  
 of it. Yikes!

 The TDPL example does actually use GetVersionEx() and calls it with a  
 "OSVERSIONINFOEX" structure argument, and it's all nicely documented on  
 MSDN. But I've no idea if I can call it from D. Anyone doing Windows  
 programming in D? :)

This works on my XP machine: --------------------------------------- module example; import std.stdio; import core.sys.windows.windows; extern(Windows) { __gshared: BOOL GetVersionExW( LPOSVERSIONINFO lpVersionInfo ); // Change to GetVersionExA if you want the UTF-8 version alias GetVersionExW GetVersionEx; // Change to char if you want the UTF-8 version alias wchar TCHAR; struct OSVERSIONINFOEX { DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[128]; WORD wServicePackMajor; WORD wServicePackMinor; WORD wSuiteMask; BYTE wProductType; BYTE wReserved; } alias OSVERSIONINFOEX* LPOSVERSIONINFO; } void main( string[] args ) { OSVERSIONINFOEX info; info.dwOSVersionInfoSize = OSVERSIONINFOEX.sizeof; if( GetVersionEx(&info) != 0 ) { if( info.dwMajorVersion == 5 ) { switch( info.dwMinorVersion ) { case 0: writeln( "Windows 2000" ); break; case 1: writeln( "Windows XP" ); break; case 2: writeln( "Windows Server 2003" ); break; default: writeln( "I dunno lol" ); } } else { writeln( "Windows Vista or 7" ); } } } --------------------------------------- I have several modified lib files with more functions defined. The ones that come bundled with DMD are severely outdated. If you have some linking problems, try to use the aliases that I suggested on the code comments. -- Yao G.
Aug 22 2010
prev sibling next sibling parent reply "Yao G." <nospamyao gmail.com> writes:
On Sun, 22 Aug 2010 14:36:50 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

I did a mistake in the code.

If you want to use the UTF-8 version of GetVersionInfoEx, declare it like  
this:

 BOOL GetVersionExA( LPOSVERSIONINFO lpVersionInfo );

 alias GetVersionExA GetVersionEx;

And make the change to the TCHAR alias too. Remember that a lot of Windows functions, specially the ones that deal with strings and characters, have two versions: functionNameA for ASCI/UTF-8 and functionNameW for UTF-16. -- Yao G.
Aug 22 2010
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Thanks, that works.

Interesting how it's pretty much a 1:1 translation from the MSDN documentation
to D code. I'm going to give more of these functions a try.

Yao G. Wrote:

 

Aug 22 2010
parent reply Andrej Mitrovic <andrej.mitrovich name.com> writes:
I think it might be this one:

http://hp.vector.co.jp/authors/VA028375/d/windows.h.html

But those are old as well. Not a big deal, c linkage seems really easy from D.
I don't think I'll have any problems using MSDN samples with a few aliases
sprinkled here and there. :)

Yao G. Wrote:

 I grabbed the lib files I use from a link that somebody posted here  
 in the NG a few years ago. Sadly I can't find that link anymore.
 
 -- 
 Yao G.

Aug 23 2010
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Very cool! Thanks, Yao.

Yao G. Wrote:

 snip

Aug 23 2010
prev sibling next sibling parent "Yao G." <nospamyao gmail.com> writes:
On Sun, 22 Aug 2010 15:23:23 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Thanks, that works.

 Interesting how it's pretty much a 1:1 translation from the MSDN  
 documentation to D code.

Yes. And in fact, that's what I did ;) I just copied/pasted the struct and function definition from the MSDN page. This is one of the few times that I'm glad of the C compatibility we enjoy with D.
 I'm going to give more of these functions a try.

Go ahead. Just remember that a lot of these functions and data structures are not present in the lib files bundled with DMD. Specially the newer ones. I grabbed the lib files I use from a link that somebody posted here in the NG a few years ago. Sadly I can't find that link anymore. -- Yao G.
Aug 22 2010
prev sibling next sibling parent Stanislav Blinov <blinov loniir.ru> writes:
  22.08.2010 23:36, Andrej Mitrovic wrote:
 There's an example of a module constructor in TDPL. I don't think it's
supossed to compile as it is, but I wanted to try it out anyway.

 I need a way to call the windows c function GetVersionEx. A grep through the
source files doesn't find it (except in the DMD cpp backend which it internally
uses), but there's one GetVersion function in core\sys\windows\windows.d. That
seems to work and returns a uint representation, but then I have to do
bitmasking to get all the info out of it. Yikes!

 The TDPL example does actually use GetVersionEx() and calls it with a
"OSVERSIONINFOEX" structure argument, and it's all nicely documented on MSDN.
But I've no idea if I can call it from D. Anyone doing Windows programming in
D? :)


 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Filtered-With-Copfilter: Version 0.84beta4 (ProxSMTP 1.8)
 Copfilter-Filtered-With: SpamAssassin 3.2.5
 Copfilter-Virus-Scanned: ClamAV 0.94.2
 by Markus Madlener   http://www.copfilter.org

Should be something like this: --- import core.sys.windows.windows; extern (System) { struct OSVERSIONINFOA { DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; char szCSDVersion[128]; } struct OSVERSIONINFOW { // same fields except for: wchar szCSDVersion[128]; } alias OSVERSIONINFOA* LPOSVERSIONINFOA; alias OSVERSIONINFOW* LPOSVERSIONINFOW; BOOL GetVersionExA( LPOSVERSIONINFOA /lpVersionInfo/ ); BOOL GetVersionExW( LPOSVERSIONINFOW /lpVersionInfo <about:blank>/ ); } --- With this declared, you should be able to call appropriate (Unicode/non-Unicode) version of this function. OSVERSIONINFOEX struct can be declared in a similar fashion.
Aug 23 2010
prev sibling next sibling parent Stanislav Blinov <blinov loniir.ru> writes:
  Oops, sorry, Thunderbird inserted second lpVersionInfo as a hyperlink :)
Aug 23 2010
prev sibling next sibling parent "Yao G." <yao.gomez spam.gmail.com> writes:
On Mon, 23 Aug 2010 18:47:02 -0500, Andrej Mitrovic
<andrej.mitrovich name.com> wrote:

 I think it might be this one:

 http://hp.vector.co.jp/authors/VA028375/d/windows.h.html

 But those are old as well. Not a big deal, c linkage seems really easy  
 from D. I don't think I'll have any problems using MSDN samples with a  
 few aliases sprinkled here and there. :)

I was referring to the *.lib files that are needed to link to the "extern" functions (gdi32.lib, kernel32.lib, user32.lib, etc.). The ones that come with DMD are very outdated (lib folder). I had a lot of linking issues with those files. But as the japanese guy wrote on that page, maybe you could create your own files using implib / coff2omf (sold separately). Also, there's a project on dsource with more updated Windows API headers. Look for the bindings projects. They use a combination of static ifs that control what type of structures and functions will be defined (ASCI or Unicode), and what Operating System will be targeted (almost like the original C files). For my projects, I rolled my own headers. They are based (like the ones on dsource) on the MinGW32 project. But mine target Windows 2000 as minimal O.S. and use exclusively UTF-32 (the ASCI versions are not defined). Also, some functions for newer O.S. are declared as function pointers (so you can link to them dynamically). They are incomplete, maybe buggy in some cases, but I think that could come handy in my Windows projects. -- Yao G.
Aug 24 2010
prev sibling next sibling parent "Yao G." <yao.gomez spam.gmail.com> writes:
Relevant links:

http://dsource.org/projects/bindings/wiki/WindowsApi
http://dsource.org/projects/bindings/browser/trunk/win32


-- 
Yao G.
Aug 24 2010
prev sibling parent "Yao G." <yao.gomez spam.gmail.com> writes:
 For my projects, I rolled my own headers. They are based (like the ones  
 on
 dsource) on the MinGW32 project. But mine target Windows 2000 as minimal
 O.S. and use exclusively UTF-32 (the ASCI versions are not defined).  
 Also,

Replace UTF-32 with UTF-16 :D -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 24 2010