<refentry id="glib-Windows-Compatibility-Functions">
<refmeta>
<refentrytitle role="top_of_page" id="glib-Windows-Compatibility-Functions.top_of_page">Windows Compatibility Functions</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Windows Compatibility Functions</refname>
<refpurpose>UNIX emulation on Windows</refpurpose>
</refnamediv>

<refsynopsisdiv id="glib-Windows-Compatibility-Functions.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;

#define             <link linkend="MAXPATHLEN--CAPS">MAXPATHLEN</link>
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-error-message">g_win32_error_message</link>               (<link linkend="gint">gint</link> error);
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-getlocale">g_win32_getlocale</link>                   (void);
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-get-package-installation-directory">g_win32_get_package_installation_directory</link>
                                                        (const <link linkend="gchar">gchar</link> *package,
                                                         const <link linkend="gchar">gchar</link> *dll_name);
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-get-package-installation-directory-of-module">g_win32_get_package_installation_directory_of_module</link>
                                                        (<link linkend="gpointer">gpointer</link> hmodule);
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-get-package-installation-subdirectory">g_win32_get_package_installation_subdirectory</link>
                                                        (const <link linkend="gchar">gchar</link> *package,
                                                         const <link linkend="gchar">gchar</link> *dll_name,
                                                         const <link linkend="gchar">gchar</link> *subdir);
<link linkend="guint">guint</link>               <link linkend="g-win32-get-windows-version">g_win32_get_windows_version</link>         (void);
<link linkend="gchar">gchar</link>*              <link linkend="g-win32-locale-filename-from-utf8">g_win32_locale_filename_from_utf8</link>   (const <link linkend="gchar">gchar</link> *utf8filename);
#define             <link linkend="G-WIN32-DLLMAIN-FOR-DLL-NAME--CAPS">G_WIN32_DLLMAIN_FOR_DLL_NAME</link>        (static, dll_name)
#define             <link linkend="G-WIN32-HAVE-WIDECHAR-API--CAPS">G_WIN32_HAVE_WIDECHAR_API</link>           ()
#define             <link linkend="G-WIN32-IS-NT-BASED--CAPS">G_WIN32_IS_NT_BASED</link>                 ()
</synopsis>
</refsynopsisdiv>









<refsect1 id="glib-Windows-Compatibility-Functions.description" role="desc">
<title role="desc.title">Description</title>
<para>
These functions provide some level of UNIX emulation on the Windows platform.
If your application really needs the POSIX APIs, we suggest you try the Cygwin
project.
</para>
</refsect1>

<refsect1 id="glib-Windows-Compatibility-Functions.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="MAXPATHLEN--CAPS" role="macro">
<title>MAXPATHLEN</title>
<indexterm zone="MAXPATHLEN--CAPS"><primary>MAXPATHLEN</primary></indexterm><programlisting>#define MAXPATHLEN 1024
</programlisting>
<para>
Provided for UNIX emulation on Windows; equivalent to UNIX
macro <link linkend="MAXPATHLEN--CAPS"><literal>MAXPATHLEN</literal></link>, which is the maximum length of a filename
(including full path).
</para></refsect2>
<refsect2 id="g-win32-error-message" role="function">
<title>g_win32_error_message ()</title>
<indexterm zone="g-win32-error-message"><primary sortas="win32_error_message">g_win32_error_message</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_error_message               (<link linkend="gint">gint</link> error);</programlisting>
<para>
Translate a Win32 error code (as returned by <link linkend="GetLastError"><function>GetLastError()</function></link>) into
the corresponding message. The message is either language neutral,
or in the thread's language, or the user's language, the system's
language, or US English (see docs for <link linkend="FormatMessage"><function>FormatMessage()</function></link>). The
returned string is in UTF-8. It should be deallocated with
<link linkend="g-free"><function>g_free()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>error</parameter>&#160;:</term>
<listitem><simpara> error code.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> newly-allocated error message
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-win32-getlocale" role="function">
<title>g_win32_getlocale ()</title>
<indexterm zone="g-win32-getlocale"><primary sortas="win32_getlocale">g_win32_getlocale</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_getlocale                   (void);</programlisting>
<para>
The <link linkend="setlocale"><function>setlocale()</function></link> function in the Microsoft C library uses locale
names of the form "English_United States.1252" etc. We want the
UNIXish standard form "en_US", "zh_TW" etc. This function gets the
current thread locale from Windows - without any encoding info -
and returns it as a string of the above form for use in forming
file names etc. The returned string should be deallocated with
<link linkend="g-free"><function>g_free()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> newly-allocated locale name.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-win32-get-package-installation-directory" role="function" condition="deprecated:">
<title>g_win32_get_package_installation_directory ()</title>
<indexterm zone="g-win32-get-package-installation-directory" role="deprecated"><primary sortas="win32_get_package_installation_directory">g_win32_get_package_installation_directory</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_get_package_installation_directory
                                                        (const <link linkend="gchar">gchar</link> *package,
                                                         const <link linkend="gchar">gchar</link> *dll_name);</programlisting>
<warning><para><literal>g_win32_get_package_installation_directory</literal> is deprecated and should not be used in newly-written code.</para></warning>
<para>
Try to determine the installation directory for a software package.
</para>
<para>
This function is deprecated. Use
<link linkend="g-win32-get-package-installation-directory-of-module"><function>g_win32_get_package_installation_directory_of_module()</function></link> instead.
</para>
<para>
The use of <parameter>package</parameter> is deprecated. You should always pass <link linkend="NULL--CAPS"><literal>NULL</literal></link>. A
warning is printed if non-NULL is passed as <parameter>package</parameter>.
</para>
<para>
The original intended use of <parameter>package</parameter> was for a short identifier of
the package, typically the same identifier as used for
<literal>GETTEXT_PACKAGE</literal> in software configured using GNU
autotools. The function first looks in the Windows Registry for the
value <literal>&num;InstallationDirectory</literal> in the key
<literal>&num;HKLM\Software@package</literal>, and if that value
exists and is a string, returns that.
</para>
<para>
It is strongly recommended that packagers of GLib-using libraries
for Windows do not store installation paths in the Registry to be
used by this function as that interfers with having several
parallel installations of the library. Enabling multiple
installations of different versions of some GLib-using library, or
GLib itself, is desirable for various reasons.
</para>
<para>
For this reason it is recommeded to always pass <link linkend="NULL--CAPS"><literal>NULL</literal></link> as
<parameter>package</parameter> to this function, to avoid the temptation to use the
Registry. In version 2.20 of GLib the <parameter>package</parameter> parameter
will be ignored and this function won't look in the Registry at all.
</para>
<para>
If <parameter>package</parameter> is <link linkend="NULL--CAPS"><literal>NULL</literal></link>, or the above value isn't found in the
Registry, but <parameter>dll_name</parameter> is non-<link linkend="NULL--CAPS"><literal>NULL</literal></link>, it should name a DLL loaded
into the current process. Typically that would be the name of the
DLL calling this function, looking for its installation
directory. The function then asks Windows what directory that DLL
was loaded from. If that directory's last component is "bin" or
"lib", the parent directory is returned, otherwise the directory
itself. If that DLL isn't loaded, the function proceeds as if
<parameter>dll_name</parameter> was <link linkend="NULL--CAPS"><literal>NULL</literal></link>.
</para>
<para>
If both <parameter>package</parameter> and <parameter>dll_name</parameter> are <link linkend="NULL--CAPS"><literal>NULL</literal></link>, the directory from where
the main executable of the process was loaded is used instead in
the same way as above.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>package</parameter>&#160;:</term>
<listitem><simpara> You should pass <link linkend="NULL--CAPS"><literal>NULL</literal></link> for this.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>dll_name</parameter>&#160;:</term>
<listitem><simpara> The name of a DLL that a package provides in UTF-8, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> a string containing the installation directory for
<parameter>package</parameter>. The string is in the GLib file name encoding,
i.e. UTF-8. The return value should be freed with <link linkend="g-free"><function>g_free()</function></link> when not
needed any longer. If the function fails <link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.

<parameter>Deprecated</parameter>:2.18: Pass the HMODULE of a DLL or EXE to
<link linkend="g-win32-get-package-installation-directory-of-module"><function>g_win32_get_package_installation_directory_of_module()</function></link> instead.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-win32-get-package-installation-directory-of-module" role="function" condition="since:2.16">
<title>g_win32_get_package_installation_directory_of_module ()</title>
<indexterm zone="g-win32-get-package-installation-directory-of-module" role="2.16"><primary sortas="win32_get_package_installation_directory_of_module">g_win32_get_package_installation_directory_of_module</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_get_package_installation_directory_of_module
                                                        (<link linkend="gpointer">gpointer</link> hmodule);</programlisting>
<para>
This function tries to determine the installation directory of a
software package based on the location of a DLL of the software
package.
</para>
<para>
<parameter>hmodule</parameter> should be the handle of a loaded DLL or <link linkend="NULL--CAPS"><literal>NULL</literal></link>. The
function looks up the directory that DLL was loaded from. If
<parameter>hmodule</parameter> is NULL, the directory the main executable of the current
process is looked up. If that directory's last component is "bin"
or "lib", its parent directory is returned, otherwise the directory
itself.
</para>
<para>
It thus makes sense to pass only the handle to a "public" DLL of a
software package to this function, as such DLLs typically are known
to be installed in a "bin" or occasionally "lib" subfolder of the
installation folder. DLLs that are of the dynamically loaded module
or plugin variety are often located in more private locations
deeper down in the tree, from which it is impossible for GLib to
deduce the root of the package installation.
</para>
<para>
The typical use case for this function is to have a <link linkend="DllMain"><function>DllMain()</function></link> that
saves the handle for the DLL. Then when code in the DLL needs to
construct names of files in the installation tree it calls this
function passing the DLL handle.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>hmodule</parameter>&#160;:</term>
<listitem><simpara> The Win32 handle for a DLL loaded into the current process, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> a string containing the guessed installation directory for
the software package <parameter>hmodule</parameter> is from. The string is in the GLib
file name encoding, i.e. UTF-8. The return value should be freed
with <link linkend="g-free"><function>g_free()</function></link> when not needed any longer. If the function fails
<link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.16</para></refsect2>
<refsect2 id="g-win32-get-package-installation-subdirectory" role="function" condition="deprecated:">
<title>g_win32_get_package_installation_subdirectory ()</title>
<indexterm zone="g-win32-get-package-installation-subdirectory" role="deprecated"><primary sortas="win32_get_package_installation_subdirectory">g_win32_get_package_installation_subdirectory</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_get_package_installation_subdirectory
                                                        (const <link linkend="gchar">gchar</link> *package,
                                                         const <link linkend="gchar">gchar</link> *dll_name,
                                                         const <link linkend="gchar">gchar</link> *subdir);</programlisting>
<warning><para><literal>g_win32_get_package_installation_subdirectory</literal> is deprecated and should not be used in newly-written code.</para></warning>
<para>
This function is deprecated. Use
<link linkend="g-win32-get-package-installation-directory-of-module"><function>g_win32_get_package_installation_directory_of_module()</function></link> and
<link linkend="g-build-filename"><function>g_build_filename()</function></link> instead.
</para>
<para>
Returns a newly-allocated string containing the path of the
subdirectory <parameter>subdir</parameter> in the return value from calling
<link linkend="g-win32-get-package-installation-directory"><function>g_win32_get_package_installation_directory()</function></link> with the <parameter>package</parameter> and
<parameter>dll_name</parameter> parameters. See the documentation for
<link linkend="g-win32-get-package-installation-directory"><function>g_win32_get_package_installation_directory()</function></link> for more details. In
particular, note that it is deprecated to pass anything except NULL
as <parameter>package</parameter>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>package</parameter>&#160;:</term>
<listitem><simpara> You should pass <link linkend="NULL--CAPS"><literal>NULL</literal></link> for this.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>dll_name</parameter>&#160;:</term>
<listitem><simpara> The name of a DLL that a package provides, in UTF-8, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>subdir</parameter>&#160;:</term>
<listitem><simpara> A subdirectory of the package installation directory, also in UTF-8
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> a string containing the complete path to <parameter>subdir</parameter> inside
the installation directory of <parameter>package</parameter>. The returned string is in
the GLib file name encoding, i.e. UTF-8. The return value should be
freed with <link linkend="g-free"><function>g_free()</function></link> when no longer needed. If something goes wrong,
<link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.

<parameter>Deprecated</parameter>:2.18: Pass the HMODULE of a DLL or EXE to
<link linkend="g-win32-get-package-installation-directory-of-module"><function>g_win32_get_package_installation_directory_of_module()</function></link> instead, and
then construct a subdirectory pathname with <link linkend="g-build-filename"><function>g_build_filename()</function></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-win32-get-windows-version" role="function" condition="since:2.6">
<title>g_win32_get_windows_version ()</title>
<indexterm zone="g-win32-get-windows-version" role="2.6"><primary sortas="win32_get_windows_version">g_win32_get_windows_version</primary></indexterm><programlisting><link linkend="guint">guint</link>               g_win32_get_windows_version         (void);</programlisting>
<para>
Returns version information for the Windows operating system the
code is running on. See MSDN documentation for the <link linkend="GetVersion"><function>GetVersion()</function></link>
function. To summarize, the most significant bit is one on Win9x,
and zero on NT-based systems. Since version 2.14, GLib works only
on NT-based systems, so checking whether your are running on Win9x
in your own software is moot. The least significant byte is 4 on
Windows NT 4, and 5 on Windows XP. Software that needs really
detailled version and feature information should use Win32 API like
<link linkend="GetVersionEx"><function>GetVersionEx()</function></link> and <link linkend="VerifyVersionInfo"><function>VerifyVersionInfo()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> The version information.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.6</para></refsect2>
<refsect2 id="g-win32-locale-filename-from-utf8" role="function" condition="since:2.8">
<title>g_win32_locale_filename_from_utf8 ()</title>
<indexterm zone="g-win32-locale-filename-from-utf8" role="2.8"><primary sortas="win32_locale_filename_from_utf8">g_win32_locale_filename_from_utf8</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_win32_locale_filename_from_utf8   (const <link linkend="gchar">gchar</link> *utf8filename);</programlisting>
<para>
Converts a filename from UTF-8 to the system codepage.
</para>
<para>
On NT-based Windows, on NTFS file systems, file names are in
Unicode. It is quite possible that Unicode file names contain
characters not representable in the system codepage. (For instance,
Greek or Cyrillic characters on Western European or US Windows
installations, or various less common CJK characters on CJK Windows
installations.)
</para>
<para>
In such a case, and if the filename refers to an existing file, and
the file system stores alternate short (8.3) names for directory
entries, the short form of the filename is returned. Note that the
"short" name might in fact be longer than the Unicode name if the
Unicode name has very short pathname components containing
non-ASCII characters. If no system codepage name for the file is
possible, <link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.
</para>
<para>
The return value is dynamically allocated and should be freed with
<link linkend="g-free"><function>g_free()</function></link> when no longer needed.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>utf8filename</parameter>&#160;:</term>
<listitem><simpara> a UTF-8 encoded filename.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> The converted filename, or <link linkend="NULL--CAPS"><literal>NULL</literal></link> on conversion
failure and lack of short names.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.8</para></refsect2>
<refsect2 id="G-WIN32-DLLMAIN-FOR-DLL-NAME--CAPS" role="macro" condition="deprecated:">
<title>G_WIN32_DLLMAIN_FOR_DLL_NAME()</title>
<indexterm zone="G-WIN32-DLLMAIN-FOR-DLL-NAME--CAPS" role="deprecated"><primary sortas="WIN32_DLLMAIN_FOR_DLL_NAME">G_WIN32_DLLMAIN_FOR_DLL_NAME</primary></indexterm><programlisting>#define             G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)</programlisting>
<warning><para><literal>G_WIN32_DLLMAIN_FOR_DLL_NAME</literal> is deprecated and should not be used in newly-written code.</para></warning>
<para>
On Windows, this macro defines a <link linkend="DllMain"><function>DllMain()</function></link> function that stores the actual
DLL name that the code being compiled will be included in.
</para>
<para>
On non-Windows platforms, expands to nothing.
</para><variablelist role="params">
<varlistentry><term><parameter>static</parameter>&#160;:</term>
<listitem><simpara>empty or "static".
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>dll_name</parameter>&#160;:</term>
<listitem><simpara>the name of the (pointer to the) char array where the DLL name 
   will be stored. If this is used, you must also include
   <filename>windows.h</filename>. If you need a more complex DLL entry
   point function, you cannot use this.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="G-WIN32-HAVE-WIDECHAR-API--CAPS" role="macro" condition="since:2.6">
<title>G_WIN32_HAVE_WIDECHAR_API()</title>
<indexterm zone="G-WIN32-HAVE-WIDECHAR-API--CAPS" role="2.6"><primary sortas="WIN32_HAVE_WIDECHAR_API">G_WIN32_HAVE_WIDECHAR_API</primary></indexterm><programlisting>#define G_WIN32_HAVE_WIDECHAR_API() TRUE
</programlisting>
<para>
On Windows, this macro defines an expression which evaluates to <link linkend="TRUE--CAPS"><literal>TRUE</literal></link>
if the code is running on a version of Windows where the wide
character versions of the Win32 API functions, and the wide chaacter
versions of the C library functions work. (They are always present in
the DLLs, but don't work on Windows 9x and Me.)
</para>
<para>
On non-Windows platforms, it is not defined.
</para><para role="since">Since 2.6</para></refsect2>
<refsect2 id="G-WIN32-IS-NT-BASED--CAPS" role="macro" condition="since:2.6">
<title>G_WIN32_IS_NT_BASED()</title>
<indexterm zone="G-WIN32-IS-NT-BASED--CAPS" role="2.6"><primary sortas="WIN32_IS_NT_BASED">G_WIN32_IS_NT_BASED</primary></indexterm><programlisting>#define G_WIN32_IS_NT_BASED() TRUE
</programlisting>
<para>
On Windows, this macro defines an expression which evaluates to <link linkend="TRUE--CAPS"><literal>TRUE</literal></link>
if the code is running on an NT-based Windows operating system.
</para>
<para>
On non-Windows platforms, it is not defined.
</para><para role="since">Since 2.6</para></refsect2>

</refsect1>




</refentry>
