<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sixserv blog &#187; Windows</title>
	<atom:link href="http://sixserv.org/category/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://sixserv.org</link>
	<description>A Blog about Linux, Networking, Development and Security.</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:45:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dynamic Loading of SDL</title>
		<link>http://sixserv.org/2010/05/21/dynamic-loading-of-sdl/</link>
		<comments>http://sixserv.org/2010/05/21/dynamic-loading-of-sdl/#comments</comments>
		<pubDate>Fri, 21 May 2010 05:58:15 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=605</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ccpp.png" width="50" height="51" alt="" title="C/C++" /><br/>If you want to use the SDL library (or any other library for that matter) in your programs, you normally would just link with the library, for instance by -lSDL (GCC). Sometimes however, it would be more flexible to dynamically load the library at runtime. With dynamic loading, if your application supports alternative libraries (e.g.: [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ccpp.png" width="50" height="51" alt="" title="C/C++" /><br/><p>If you want to use the <a href="http://www.libsdl.org/">SDL library</a> (or any other library for that matter) in your programs, you normally would just link with the library, for instance by <code>-lSDL</code> (GCC). Sometimes however, it would be more flexible to dynamically load the library at runtime. With dynamic loading, if your application supports alternative libraries (e.g.: <strike><a href="http://www.opengl.org/resources/libraries/glut/">GLUT</a></strike> <a href="http://www.sfml-dev.org/">SFML</a> as an replacement for SDL), it can discover and use available libraries at runtime. The system your program is running on, just needs one of those libraries installed and can switch them without the need to recompile your program. This technique is probably most commonly used to load plugins into programs at runtime.</p>
<p>Theres a helpful Wikipedia article about <a href="http://en.wikipedia.org/wiki/Dynamic_loading">Dynamic loading</a>, they even describe how to load the SDL library in their examples. Besides that, theres an old discussion about that at <a href="http://lists.libsdl.org/pipermail/sdl-libsdl.org/2007-October/063161.html">lists.libsdl.org</a> and an article at eaten by a grue (<a href="http://eatenbyagrue.org/when_good_libraries_go_bad.html">when good libraries go bad</a>). However I didn&#8217;t find a complete example implementation for this, so I&#8217;m trying to <a href="http://apoc.sixserv.org/coderepo/listing.php?repname=dloadsdl">create one</a> with this article, although I&#8217;m not a very experienced C++ programmer.</p>
<p>The first thing to do is to load the library file, on POSIX/UNIX systems we do this with <code><a href="http://linux.die.net/man/3/dlopen">dlopen()</a></code>, on windows we use <code><a href="http://msdn.microsoft.com/en-us/library/ms684175%28VS.85%29.aspx">LoadLibrary</a>()</code> and for closing <code><a href="http://linux.die.net/man/3/dlclose">dlclose()</a></code> and <code><a href="http://msdn.microsoft.com/en-us/library/ms683152%28VS.85%29.aspx">FreeLibrary()</a></code> respectively. The following code snippet for open/closing the library should work on both Linux and Windows systems:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifdef _WIN32</span>
    <span style="color: #339900;">#include &lt;windows.h&gt;</span>
<span style="color: #339900;">#else</span>
    <span style="color: #339900;">#include &lt;dlfcn.h&gt;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #666666;">// [...]</span>
&nbsp;
<span style="color: #339900;">#ifdef _WIN32</span>
    HMODULE libhandle<span style="color: #008080;">;</span>
    libhandle <span style="color: #000080;">=</span> LoadLibrary<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;SDL.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#else</span>
    <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>libhandle<span style="color: #008080;">;</span>
    libhandle <span style="color: #000080;">=</span> dlopen<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;libSDL.so&quot;</span>, RTLD_LAZY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #666666;">// ... dlsym() and GetProcAddress() calls ...</span>
&nbsp;
<span style="color: #339900;">#ifdef _WIN32</span>
    FreeLibrary<span style="color: #008000;">&#40;</span>libhandle<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#else</span>
    dlclose<span style="color: #008000;">&#40;</span>libhandle<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span></pre></td></tr></table></div>

<p>You need to link your programs to the dl library for this to work (such as <code>-ldl</code> in gcc).</p>
<p>The returned handle can be used to retrieve symbol addresses and then to convert them to function pointers. Theres <code><a href="http://linux.die.net/man/3/dlsym">dlsym()</a></code> on POSIX and <code><a href="http://msdn.microsoft.com/en-us/library/ms683212%28VS.85%29.aspx">GetProcAddress()</a></code> on Windows machines. The following code does that cross-platform for <code><a href="http://sdl.beuc.net/sdl.wiki/SDL_SetVideoMode">SDL_SetVideoMode()</a></code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">SDL_Surface <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p_SDL_SetVideoMode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, Uint32<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#ifdef _WIN32</span>
    p_SDL_SetVideoMode <span style="color: #000080;">=</span> 
        <span style="color: #008000;">&#40;</span>SDL_Surface <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, Uint32<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> GetProcAddress<span style="color: #008000;">&#40;</span>libhandle, <span style="color: #FF0000;">&quot;SDL_SetVideoMode&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#else</span>
    p_SDL_SetVideoMode <span style="color: #000080;">=</span> 
        <span style="color: #008000;">&#40;</span>SDL_Surface <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, Uint32<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> dlsym<span style="color: #008000;">&#40;</span>libhandle, <span style="color: #FF0000;">&quot;SDL_SetVideoMode&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span></pre></td></tr></table></div>

<p>You need to look at the header files (<code>/usr/include/SDL/SDL_video.h</code>) for the exact declaration (return and parameter types). </p>
<p>Theres a problem here, when casting from an void pointer that is returned by <code>dlsym()</code> to a function pointer which is prohibited by the C/C++ ISO standards. I&#8217;m using a simple explicit (C-style) cast here and in practice, I not even encountered a warning with modern compilers. </p>
<p>Johan Petersson has written an article (<a href="http://www.trilithium.com/johan/2004/12/problem-with-dlsym/">When standards collide: the problem with dlsym</a>) about that problem in <a href="http://www.trilithium.com/johan/">Scatter/Gather thoughts</a>, I want to quote:</p>
<blockquote><p><em>You probably noticed that I omitted the C-style cast from my earlier example. Alas, most C and C++ compilers will allow the conversion when you use a C-style cast. You may not even get a warning, even though it&#8217;s prohibited in ISO C as well as ISO C++. This kind of conversion is a common compiler extension. So common, in fact, that many people don&#8217;t realize it&#8217;s not in the standards.</em></p></blockquote>
<p>The article is from 2004, I don&#8217;t know if the current C/C++ standards had changed any of that. </p>
<p>In the last code snippet theres an redundancy that can be resolved by using a <code>typedef</code> instead:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> SDL_Surface <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>Type_SDL_SetVideoMode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span>, Uint32<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Type_SDL_SetVideoMode p_SDL_SetVideoMode<span style="color: #008080;">;</span>
<span style="color: #339900;">#ifdef _WIN32</span>
    p_SDL_SetVideoMode <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>Type_SDL_SetVideoMode<span style="color: #008000;">&#41;</span> GetProcAddress<span style="color: #008000;">&#40;</span>libhandle, <span style="color: #FF0000;">&quot;SDL_SetVideoMode&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#else</span>
    p_SDL_SetVideoMode <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>Type_SDL_SetVideoMode<span style="color: #008000;">&#41;</span> dlsym<span style="color: #008000;">&#40;</span>libhandle, <span style="color: #FF0000;">&quot;SDL_SetVideoMode&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span></pre></td></tr></table></div>

<p>Thats everything you need to call that function: <code>(p_SDL_SetVideoMode)(...);</code> The major problem here is that SDL has over 200 functions that &#8220;must&#8221; be declared and addressed this way. I&#8217;ve written a <a href="http://apoc.sixserv.org/coderepo/filedetails.php?repname=dloadsdl&#038;path=%2Flibsdl_header_parser.rb">ruby script</a> for Linux that does that for the SDL headers within <code>/usr/include/SDL</code>, at first it reads the symbol names directly from the <code>libSDL.so</code> shared library utilizing <code><a href="http://linux.die.net/man/1/nm">nm</a></code>, then it parses the header files for the function declarations found by <code>nm</code> and generates the code for the pointer declaration and explicit casts (/dlsym calls) automatically.</p>
<p>I&#8217;ve created some <a href="http://apoc.sixserv.org/coderepo/listing.php?repname=dloadsdl">example code</a> that defines an abstract class <code><a href="http://apoc.sixserv.org/coderepo/filedetails.php?repname=dloadsdl&#038;path=%2FDLLoader.h">DLLoader</a></code> and an implementation <code><a href="http://apoc.sixserv.org/coderepo/filedetails.php?repname=dloadsdl&#038;path=%2FDLLoaderSDL.h">DLLoaderSDL</a></code> that includes an struct with the automatically generated function pointers etc. The test program loads the library and shows a SDL window for 3 seconds.<br />
I&#8217;ve tested the program with GCC 4.5.0 (Linux), GCC 3.4.5 (Windows MinGW) and CL 14.0 (Windows VC++). I also added Makefiles for each of those compiler suites.</p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2010/05/21/dynamic-loading-of-sdl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPA_Supplicant für Windows</title>
		<link>http://sixserv.org/2008/09/01/wpa_supplicant-fur-windows/</link>
		<comments>http://sixserv.org/2008/09/01/wpa_supplicant-fur-windows/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 17:39:08 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[wep]]></category>
		<category><![CDATA[wlan]]></category>
		<category><![CDATA[wpa]]></category>
		<category><![CDATA[wpa_supplicant]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=69</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_windows.png" width="50" height="51" alt="" title="Windows" /><br/>Gestern stand ich vor dem Problem ein neues WLAN-Netzwerk unter Windows XP einzurichten, soweit ja kein Problem werdet ihr sagen, allerdings wurde vorher das unsichere WEP eingesetzt und eine der verwendeten WLAN-PCMCIA Karten unterstützt kein WPA. Dazu kommt noch das der letzte Treiber von 2004 ist. Es handelt sich um eine “Acer WLAN 11g PCMCIA(wlan-g-pc2)”. [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_windows.png" width="50" height="51" alt="" title="Windows" /><br/><p>Gestern stand ich vor dem Problem ein neues WLAN-Netzwerk unter Windows XP einzurichten, soweit ja kein Problem werdet ihr sagen, allerdings wurde vorher das unsichere WEP eingesetzt und eine der verwendeten WLAN-PCMCIA Karten unterstützt kein WPA. Dazu kommt noch das der letzte Treiber von 2004 ist. Es handelt sich um eine “Acer WLAN 11g PCMCIA(wlan-g-pc2)”.</p>
<p>Gelöst habe ich das Problem letztendlich mit <a href="http://hostap.epitest.fi/wpa_supplicant/">wpa_supplicant</a> <a href="http://hostap.epitest.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/README-Windows.txt">für Windows</a>, welches nach einigen Testen auch durchaus stabil läuft. Das ganze wirkt schon ein wenig abenteuerlich(<a href="http://www.winpcap.org/">Winpcap</a> wird z.B. benötigt um an die Pakete und das Interface zu gelangen), allerdings ist es schnell eingerichtet(läuft über die gleiche wpa_supplicant.conf wie unter Linux) und unterstützt dann auch tatsächlich alle Protokolle die Supplicant unterstützt.<br />
Zusätzlich gibt es eine spezielle Version(wpasvc.exe) die man als Dienst(Service) registrieren kann. Nachteil ist allerdings das sie ihre Konfiguration aus der Registry bezieht, was wie bekannt ein wenig umständlich ist.</p>
<p>Trotzdem ist es eine wunderbare Möglichkeit ältere WLAN-Adapter unter Windows für WPA Fit zu machen.</p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2008/09/01/wpa_supplicant-fur-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
