<?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>Blog de Nico-pyright(c) &#187; C/C++ &#8211; Win32</title>
	<atom:link href="https://blog.developpez.com/nico-pyright/pcategory/cc-win32/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/nico-pyright</link>
	<description></description>
	<lastBuildDate>Mon, 08 Apr 2013 09:26:35 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>[C] Implémentation de l’algo de Dijkstra en C</title>
		<link>https://blog.developpez.com/nico-pyright/p9300/cc-win32/c_implementation_de_l_algo_de_dijkstra_e</link>
		<comments>https://blog.developpez.com/nico-pyright/p9300/cc-win32/c_implementation_de_l_algo_de_dijkstra_e#comments</comments>
		<pubDate>Sat, 18 Sep 2010 14:41:00 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[J’ai eu besoin d’utiliser l’algo bien connu de Dijkstra pour trouver un plus court chemin entre 2 points. Le principe est très bien expliqué sur wikipédia, mais j’avais besoin d’une implémentation en C. Après quelques recherches, je suis tombé sur cette page qui m’offre l’aglo tout cuit, mais sans son utilisation. Je montre ici comment l’utiliser après une ou deux corrections de coquille dans l’algo trouvé. Il faut dans un premier temps une matrice adjacente, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>J’ai eu besoin d’utiliser l’algo bien connu de Dijkstra pour trouver un plus court chemin entre 2 points. Le principe est très bien expliqué sur <a href="http://fr.wikipedia.org/wiki/Algorithme_de_Dijkstra" target="_blank">wikipédia</a>, mais j’avais besoin d’une implémentation en C.</p>
<p>Après quelques recherches, je suis tombé sur <a href="http://www.answers.com/topic/dijkstra-s-algorithm-1" target="_blank">cette page</a> qui m’offre l’aglo tout cuit, mais sans son utilisation.</p>
<p>Je montre ici comment l’utiliser après une ou deux corrections de coquille dans l’algo trouvé.</p>
<p>Il faut dans un premier temps une matrice adjacente, qui peut être de cette forme :</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #cc6633">#define</span> INFINI 10000</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #cc6633">#define</span> NB_SOMMET 6</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>&#160; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">int</span> mat[NB_SOMMET][NB_SOMMET] =  </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     {{ 0, 171, INFINI, INFINI, INFINI, INFINI},</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     {171,   0,  30, INFINI, INFINI, INFINI},</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     {INFINI,  30,   0, INFINI,  39,  13},</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     {INFINI, INFINI, INFINI,   0,  30, INFINI},</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     {INFINI, INFINI, 39,   30,   0,  26},</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     {INFINI, INFINI,  13, INFINI,  26,   0}};</pre>
</div>
</div>
<p>On a une constante “Infini” qui est suffisamment grande, c’est à dire plus grande que la distance maxi entre 2 sommets. Et une matrice carrée contenant la distance entre chaque sommet.</p>
<p>Ensuite la méthode avec les 2-3 trucs corrigés :</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">void</span> dodijkstra(<span style="color: #0000ff">int</span> sr,<span style="color: #0000ff">int</span> ds, <span style="color: #0000ff">int</span> path[])</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">if</span> (sr == ds)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>         <span style="color: #0000ff">return</span>;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>&#160; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     <span style="color: #0000ff">struct</span> node</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         <span style="color: #0000ff">int</span> pre;   <span style="color: #008000">/* Predecessor */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>         <span style="color: #0000ff">int</span> length; <span style="color: #008000">/* Length between the nodes */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         <span style="color: #0000ff">enum</span> {perm,tent} label; <span style="color: #008000">/* Enumeration for permanent and tentative labels */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     }state[NB_SOMMET];</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         <span style="color: #0000ff">int</span> i,k,min;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         <span style="color: #0000ff">struct</span> node *p;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>         <span style="color: #008000">/* Initialisation of the nodes aka First step of Dijkstra Algo */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         <span style="color: #0000ff">for</span>(p=&amp;state[0];p&lt;&amp;state[NB_SOMMET];p++)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>            p-&gt;pre= -1;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>            p-&gt;length=INFINI;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>            p-&gt;label=node::tent;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>         }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>         state[ds].length=0; <span style="color: #008000">/* Destination length set to zero */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>         state[ds].label=node::perm; <span style="color: #008000">/* Destination set to be the permanent node */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>         k=ds; <span style="color: #008000">/* initial working node */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>         <span style="color: #008000">/* Checking for a better path from the node k ? */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>         <span style="color: #0000ff">do</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>            <span style="color: #0000ff">for</span>(i=0;i&lt;NB_SOMMET;i++)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>               {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>                   <span style="color: #0000ff">if</span>(mat[k][i]!=0 &amp;&amp; state[i].label==node::tent)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>                      {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>                         <span style="color: #0000ff">if</span>((state[k].length+mat[k][i])&lt;state[i].length)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>                            {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>                                state[i].pre=k;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>                                state[i].length=state[k].length+mat[k][i];</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>                            }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>                      }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>               }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>              k=0;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>              min=INFINI;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>               <span style="color: #008000">/* Find a node which is tentatively labeled and with minimum label */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>               <span style="color: #0000ff">for</span>(i=0;i&lt;NB_SOMMET;i++)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>               {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>                  <span style="color: #0000ff">if</span>(state[i].label==node::tent &amp;&amp; state[i].length&lt;min)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>                     {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>                         min=state[i].length;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>                         k=i;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>                     }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>               }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>               state[k].label=node::perm;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>         } <span style="color: #0000ff">while</span>(k!=sr);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>         </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>         i=0;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>         k=sr;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         <span style="color: #008000">/* Print the path to the output array */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>         <span style="color: #0000ff">do</span> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>         {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>             path[i++]=k;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>             k=state[k].pre;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>         } <span style="color: #0000ff">while</span>(k&gt;=0);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>         path[i]=k;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span> }</pre>
</div>
</div>
<p>et enfin, l’appel de la méthode :</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">int</span> path[150];</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> z = 0 ; z &lt; 150 ; z++)</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     path[z] = -1;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> dodijkstra(sommetSource, sommetDestination, path);</pre>
</div>
</div>
<p>On initialise le tableau de résultat. Ici j’ai pris 150 arbitrairement, le but est que le tableau puisse contenir toutes les étapes intermédiaires.</p>
<p>L’algorithme nous rempli le tableau avec le trajet optimal.</p>
<p>path[0] contiendra la source<br />
path[i] contient les différents sommets intermédiaires<br />
path[n] contient la destination<br />
path[n-&gt;150] contient -1</p>
<p>Simple et efficace !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[C++] Interview de Bjarne Stroustrup dans Msdn Magazine. L&#8217;évolution des langages</title>
		<link>https://blog.developpez.com/nico-pyright/p5556/cc-win32/c_interview_de_bjarne_stroustrup_dans_ms</link>
		<comments>https://blog.developpez.com/nico-pyright/p5556/cc-win32/c_interview_de_bjarne_stroustrup_dans_ms#comments</comments>
		<pubDate>Fri, 25 Apr 2008 09:43:44 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici le lien : http://msdn2.microsoft.com/fr-fr/magazine/cc500572.aspx Bonne lecture]]></description>
				<content:encoded><![CDATA[<p>Voici le lien : http://msdn2.microsoft.com/fr-fr/magazine/cc500572.aspx</p>
<p>Bonne lecture <img src="https://blog.developpez.com/nico-pyright/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[C++/CLI] Intéropérabilité de structure</title>
		<link>https://blog.developpez.com/nico-pyright/p5013/ccli/c_cli_interoperabilite_de_structure</link>
		<comments>https://blog.developpez.com/nico-pyright/p5013/ccli/c_cli_interoperabilite_de_structure#comments</comments>
		<pubDate>Fri, 01 Feb 2008 21:52:49 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je viens de mettre à jour mon tutoriel sur l&#8217;intéropérabilité où j&#8217;expose une manière plus simple de mettre à jour une structure C native par référence depuis un programme C#. soit la structure C suivante typedef struct { &#160; &#160; int age; &#160; &#160; int taille; &#160; } MASTRUCTURE; et la méthode suivante qui incrémente une structure passée en paramètre : extern &#34;C&#34; __declspec(dllexport) void Inc(MASTRUCTURE * s) &#160; { &#160; &#160; s-&#62;age++; &#160; &#160; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Je viens de mettre à jour mon <a href="http://nico-pyright.developpez.com/tutoriel/vc2005/interop2/#Lupdatestruct">tutoriel sur l&rsquo;intéropérabilité</a> où j&rsquo;expose une manière plus simple de mettre à jour une structure C native par référence depuis un programme C#.</p>
<p><span id="more-34"></span></p>
<p>soit la structure C suivante</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">typedef struct { &nbsp;<br />
&nbsp; int age; &nbsp;<br />
&nbsp; int taille; &nbsp;<br />
} MASTRUCTURE;</div></div>
<p>et la méthode suivante qui incrémente une structure passée en paramètre :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">extern &quot;C&quot; __declspec(dllexport) void Inc(MASTRUCTURE * s) &nbsp;<br />
{ &nbsp;<br />
&nbsp; s-&gt;age++; &nbsp;<br />
&nbsp; s-&gt;taille++; &nbsp;<br />
}</div></div>
<p>On peut utiliser les méthodes <strong>PtrToStructure</strong> et <strong>StructureToPtr</strong> pour faire l&rsquo;intéropérabilité, mais on peut également utiliser une méthode très simple en déclarant notre structure avec le mot clé <strong>struct</strong> :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[StructLayout(LayoutKind.Sequential)] <br />
public struct MaStructCSharp <br />
{ <br />
&nbsp; public int age; <br />
&nbsp; public int taille; <br />
}</div></div>
<p>Et si on déclare le DllImport avec une référence :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[DllImport(&quot;libCStruct.dll&quot;)] <br />
public static extern void Inc(ref MaStructCSharp s);</div></div>
<p>On pourra faire l&rsquo;implémentation de notre code C# ainsi :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MaStructCSharp s = new MaStructCSharp(); <br />
s.age = 27; <br />
s.taille = 185; <br />
Inc(ref s); <br />
Console.WriteLine(s.age); <br />
Console.WriteLine(s.taille);</div></div>
<p>Voilà</p>
<p>J&rsquo;en profite pour remercier Mike pour ses remarques constructives sur mon article <img src="https://blog.developpez.com/nico-pyright/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVP pour une nouvelle année</title>
		<link>https://blog.developpez.com/nico-pyright/p4734/ccli/mvp_pour_une_nouvelle_annee</link>
		<comments>https://blog.developpez.com/nico-pyright/p4734/ccli/mvp_pour_une_nouvelle_annee#comments</comments>
		<pubDate>Thu, 03 Jan 2008 08:05:45 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>
		<category><![CDATA[Visual C++ IDE]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Me voici également renouvelé pour une nouvelle année de MVP à votre service. J&#8217;en profite pour vous adresser mes meilleurs voeux pour cette année, qu&#8217;elle soit riche en C++, en .Net et en surprise de toutes sortes. Bons devs]]></description>
				<content:encoded><![CDATA[<p>Me voici également renouvelé pour une nouvelle année de MVP à votre service.</p>
<p>J&rsquo;en profite pour vous adresser mes meilleurs voeux pour cette année, qu&rsquo;elle soit riche en C++, en .Net et en surprise de toutes sortes.</p>
<p>Bons devs <img src="https://blog.developpez.com/nico-pyright/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutoriel : Introduction à l&#8217;intéropérabilité (partie 2)</title>
		<link>https://blog.developpez.com/nico-pyright/p4448/ccli/tutoriel_introduction_a_l_interoperabili_2</link>
		<comments>https://blog.developpez.com/nico-pyright/p4448/ccli/tutoriel_introduction_a_l_interoperabili_2#comments</comments>
		<pubDate>Sun, 28 Oct 2007 23:00:00 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vous présente la deuxième partie de mon tutoriel d&#8217;introduction à l&#8217;intéropérabilité. Vous apprendrez comment utiliser des structures C plus ou moins complexes dans un programme C#. Vous apprendrez aussi comment fonctionne COM Interop ainsi que le hosting de CLR afin d&#8217;utiliser des objets .Net dans une application native. Aller au tutoriel d&#8217;introduction à l&#8217;intéropérabilité (partie 2) Bonne lecture]]></description>
				<content:encoded><![CDATA[<p>Je vous présente la deuxième partie de mon tutoriel d&rsquo;introduction à l&rsquo;intéropérabilité.</p>
<p>Vous apprendrez comment utiliser des structures C plus ou moins complexes dans un programme C#. Vous apprendrez aussi comment fonctionne COM Interop ainsi que le hosting de CLR afin d&rsquo;utiliser des objets .Net dans une application native.</p>
<p><a href="http://nico-pyright.developpez.com/tutoriel/vc2005/interop2/">Aller au tutoriel d&rsquo;introduction à l&rsquo;intéropérabilité (partie 2)</a></p>
<p>Bonne lecture</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>L&#8217;avenir de visual c++ ?</title>
		<link>https://blog.developpez.com/nico-pyright/p4059/ccli/l_avenir_de_visual_c</link>
		<comments>https://blog.developpez.com/nico-pyright/p4059/ccli/l_avenir_de_visual_c#comments</comments>
		<pubDate>Thu, 09 Aug 2007 19:00:00 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Somasegar nous informe de l&#8217;orientation stratégique de Visual C++. Comme on pouvait s&#8217;en douter, les priorités s&#8217;articulent autour de : l&#8217;augmentation du support sur le développement natif l&#8217;interop pour permettre a des applications natives d&#8217;accéder aux composants des framework.Net (WPF, WCF, &#8230;) que ce soit pour un relooking ou pour tirer parti des avancées du framework. l&#8217;interop en tant que pont entre le monde managé et natif, pour tout ce qui est wrapper. Des perspectives [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://blogs.msdn.com/somasegar/default.aspx">Somasegar</a> nous informe de l&rsquo;orientation stratégique de Visual C++.</p>
<p>Comme on pouvait s&rsquo;en douter, les priorités s&rsquo;articulent autour de : </p>
<ul>
<li> l&rsquo;augmentation du support sur le développement natif</li>
<li> l&rsquo;interop pour permettre a des applications natives d&rsquo;accéder aux composants des framework.Net (WPF, WCF, &#8230;) que ce soit pour un relooking ou pour tirer parti des avancées du framework.</li>
<li> l&rsquo;interop en tant que pont entre le monde managé et natif, pour tout ce qui est wrapper.</li>
</ul>
<p>Des perspectives pour le C++/CLI, langage roi de l&rsquo;intéropérabilité&#8230; et pour tout ce qui est natif, le C++ reste le langage de premier choix.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comment intercepter des exceptions managées depuis du code natif ?</title>
		<link>https://blog.developpez.com/nico-pyright/p4044/ccli/comment_intercepter_des_exceptions_manag</link>
		<comments>https://blog.developpez.com/nico-pyright/p4044/ccli/comment_intercepter_des_exceptions_manag#comments</comments>
		<pubDate>Tue, 07 Aug 2007 22:00:00 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Des fois, il peut etre utile de pouvoir intercepter une exception managée à partir d&#8217;un code natif. Soit le programme suivant qui simule, grace aux pragma unmanaged et managed, une intéraction entre un code natif et un code managé : using namespace System; &#160; void fonctionManagee() { &#160; throw gcnew Exception(&#34;Exception managée&#34;); } &#160; #pragma unmanaged void fonctionNative() { &#160; fonctionManagee(); } #pragma managed &#160; int main() { &#160; fonctionNative(); &#160; return 0; } Si [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Des fois, il peut etre utile de pouvoir intercepter une exception managée à partir d&rsquo;un code natif.</p>
<p>Soit le programme suivant qui simule, grace aux pragma <strong>unmanaged </strong>et <strong>managed</strong>, une intéraction entre un code natif et un code managé :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">using namespace System; <br />
&nbsp;<br />
void fonctionManagee() <br />
{ <br />
&nbsp; throw gcnew Exception(&quot;Exception managée&quot;); <br />
} <br />
&nbsp;<br />
#pragma unmanaged <br />
void fonctionNative() <br />
{ <br />
&nbsp; fonctionManagee(); <br />
} <br />
#pragma managed <br />
&nbsp;<br />
int main() <br />
{ <br />
&nbsp; fonctionNative(); <br />
&nbsp; return 0; <br />
}</div></div>
<p><span id="more-10"></span></p>
<p>Si on exécute cet exemple, on obtient un crash de l&rsquo;application à cause de la levée d&rsquo;exception dans la méthode managée.</p>
<p>Le premier reflexe est d&rsquo;utiliser un try catch &#8230; mais &#8230; quel type d&rsquo;exception attraper ? Une solution pourrait être de tout attraper, avec une construction de ce genre :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">void fonctionNative() <br />
{ <br />
&nbsp; try <br />
&nbsp; { <br />
&nbsp; &nbsp; fonctionManagee(); <br />
&nbsp; } <br />
&nbsp; catch (...) <br />
&nbsp; { <br />
&nbsp; &nbsp; std::cout &lt;&lt; &quot;N'importe quelle exception est attrapee, meme managee&quot; &lt;&lt; std::endl; <br />
&nbsp; } <br />
}</div></div>
<p>Cette construction a l&rsquo;avantage de permettre d&rsquo;intercepter l&rsquo;exception levée dans la fonction managée depuis le code natif, mais a l&rsquo;inconvénient de tout intercepter, à cause de la construction <strong>&laquo;&nbsp;&#8230;&nbsp;&raquo;</strong> suivant le catch.</p>
<p>La solution est d&rsquo;utiliser un block <strong>try except</strong>, comme ci-dessous :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">void fonctionNative() <br />
{ <br />
&nbsp; __try <br />
&nbsp; { <br />
&nbsp; &nbsp; fonctionManagee(); <br />
&nbsp; } <br />
&nbsp; __except(GetExceptionCode() == 0xE0434F4D ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) <br />
&nbsp; { <br />
&nbsp; &nbsp; std::cout &lt;&lt; &quot;Uniquement les exceptions managees sont attrapees&quot; &lt;&lt; std::endl; <br />
&nbsp; } <br />
}</div></div>
<p>0xE0434F4D est le code d&rsquo;erreur SEH correspond. (pour l&rsquo;anectode, 0xE0434F4D correspond à 0xE0+&nbsp;&raquo;COM&nbsp;&raquo; (= &lsquo;àCOM&rsquo;), car à l&rsquo;origine, le CLR a été appelé COM+ 2.0. Le nom du projet a changé mais pas les codes d&rsquo;exception)</p>
<p>L&rsquo;inconvénient est qu&rsquo;on ne peut pas avoir d&rsquo;information sur l&rsquo;exception, comme dans notre cas, le message d&rsquo;exception levé.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intéropérabilité de tableau dans une structure, le problème des tableaux à plusieurs dimensions</title>
		<link>https://blog.developpez.com/nico-pyright/p3877/cc-win32/interoperabilite_de_tableau_dans_une_str</link>
		<comments>https://blog.developpez.com/nico-pyright/p3877/cc-win32/interoperabilite_de_tableau_dans_une_str#comments</comments>
		<pubDate>Fri, 29 Jun 2007 19:00:00 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[On vient de me poser une question : je veux utiliser cette structure en C dans mon programme C# typedef struct &#160; { &#160; int val; &#160; int telfixe[10][2]; } MASTRUCTURETEL; j&#8217;ai un peu modifié l&#8217;exemple pour que ce soit plus simple, mais la question porte sur le tableau à deux dimensions. Alors, comment faire ? Sur l&#8217;exemple de mes précédents billets, je vais créer une dll qui va exporter une fonction qui me retourne [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>On vient de me poser une question :</p>
<p>je veux utiliser cette structure en C dans mon programme C#</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">typedef struct &nbsp;<br />
{ <br />
&nbsp; int val; <br />
&nbsp; int telfixe[10][2]; <br />
} MASTRUCTURETEL;</div></div>
<p>j&rsquo;ai un peu modifié l&rsquo;exemple pour que ce soit plus simple, mais la question porte sur le tableau à deux dimensions.</p>
<p>Alors, comment faire ? <span id="more-32"></span></p>
<p>Sur l&rsquo;exemple de mes précédents billets, je vais créer une dll qui va exporter une fonction qui me retourne une structure :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">typedef struct &nbsp;<br />
{ <br />
&nbsp; int val; <br />
&nbsp; int telfixe[10][2]; <br />
} MASTRUCTURETEL; <br />
&nbsp;<br />
extern &quot;C&quot; __declspec(dllexport) MASTRUCTURETEL * GetUneStructure() &nbsp;<br />
{ <br />
&nbsp; MASTRUCTURETEL *maStruct = new MASTRUCTURETEL; <br />
&nbsp; for (int i = 0 ; i &lt; 10 ; i++) <br />
&nbsp; &nbsp; for (int j = 0; j &lt; 2 ; j++) <br />
&nbsp; &nbsp; &nbsp; maStruct-&gt;telfixe[i][j] = (i+1)*(j+1); <br />
&nbsp; maStruct-&gt;val = 10; <br />
&nbsp; return maStruct; &nbsp;<br />
}</div></div>
<p>J&rsquo;initialise mon tableau avec un peu n&rsquo;importe quoi, mais bon voila, c&rsquo;est exactement le tableau que je voulais avec les valeurs que je voulais :p</p>
<p>Maintenant, je veux pouvoir l&rsquo;utiliser dans mon programme C#.</p>
<p>Il faut savoir que le marshaleur ne sait pas marshaler des tableaux multi-dimmensionnels, et donc que fatalement, nous allons récupérer un tableau à une seule dimension. Pour ce faire, on crée la structure C# ainsi :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; [StructLayout(LayoutKind.Sequential)] <br />
&nbsp; &nbsp; public class MaStructTelCSharp <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; public int val; <br />
&nbsp; &nbsp; &nbsp; &nbsp; [MarshalAs(UnmanagedType.ByValArray, SizeConst=10*2, ArraySubType=UnmanagedType.SysInt)] <br />
&nbsp; &nbsp; &nbsp; &nbsp; public int[] telFixe; <br />
&nbsp; &nbsp; }</div></div>
<p>L&rsquo;important est de noter la taille du tableau dans le paramètre SizeConst de l&rsquo;attribut MarshalAs. On lui précise aussi qu&rsquo;on veut récupérer des entiers.</p>
<p>Puis, on récupère la structure ainsi :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; [DllImport(&quot;testLib.dll&quot;)] <br />
&nbsp; &nbsp; &nbsp; &nbsp; public static extern IntPtr GetUneStructure(); <br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ... <br />
&nbsp; &nbsp; &nbsp; &nbsp; IntPtr maStructureCUnmanaged = GetUneStructure(); <br />
&nbsp; &nbsp; &nbsp; &nbsp; MaStructTelCSharp maStructureCSharp = new MaStructTelCSharp(); <br />
&nbsp; &nbsp; &nbsp; &nbsp; Marshal.PtrToStructure(maStructureCUnmanaged, maStructureCSharp);</div></div>
<p>Au final donc, on se retrouve avec une structure qui contient un tableau à une dimension au lieu d&rsquo;un tableau à deux dimensions.<br />
C&rsquo;est triste mais c&rsquo;est ainsi.</p>
<p>Rien ne vous empeche de reconstruire éventuellement un tableau à deux dimensions à partir de notre tableau à 1 dimension.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>COM Interop &#8211; Conserver la signature des méthodes</title>
		<link>https://blog.developpez.com/nico-pyright/p3844/cc-win32/com_interop_conserver_la_signature_des_m</link>
		<comments>https://blog.developpez.com/nico-pyright/p3844/cc-win32/com_interop_conserver_la_signature_des_m#comments</comments>
		<pubDate>Sun, 24 Jun 2007 19:32:15 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Le billet précédent présente une méthode qui simplifie grandement l&#8217;écriture de notre programme natif qui utilise un objet managé via COM Interop. La contrainte qu&#8217;il rajoute est la transformation des signatures des méthodes à la sauce COM. Ainsi, une méthode : String^ ReadLine(); sera wrappée en HRESULT ReadLine(/*[out,retval]*/ BSTR); Ce qui suggère l&#8217;utilisation de la macro SUCCEEDED pour tester la bonne marche de la fonction. Mais pouvons-nous encore simplifier et se passer de ce type [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Le billet précédent présente une méthode qui simplifie grandement l&rsquo;écriture de notre programme natif qui utilise un objet managé via COM Interop.<br />
La contrainte qu&rsquo;il rajoute est la transformation des signatures des méthodes à la sauce COM.<br />
Ainsi, une méthode :</p>
<p><code class="codecolorer text default"><span class="text">String^ ReadLine();</span></code></p>
<p>sera wrappée en </p>
<p><code class="codecolorer text default"><span class="text">HRESULT ReadLine(/*[out,retval]*/ BSTR);</span></code></p>
<p>Ce qui suggère l&rsquo;utilisation de la macro SUCCEEDED pour tester la bonne marche de la fonction.</p>
<p>Mais pouvons-nous encore simplifier et se passer de ce type de signature ? On aimerait pouvoir avoir une signature du genre :</p>
<p><code class="codecolorer text default"><span class="text">BSTR ReadLine();</span></code></p>
<p>Bref, conserver la signature de la méthode, aux types près &#8230;</p>
<p><span id="more-24"></span></p>
<p>Et bien c&rsquo;est possible, grâce à un attribut disponible dans le namespace System::Runtime::CompilerServices.</p>
<p>Notre classe managée ne change pas d&rsquo;écriture, mais l&rsquo;interface sera écrite ainsi :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">using namespace System::Runtime::CompilerServices; <br />
&nbsp;<br />
namespace MonStreamReader &nbsp;<br />
{ <br />
&nbsp; public interface class IClassWrap <br />
&nbsp; { <br />
&nbsp; &nbsp; [MethodImpl(MethodImplOptions::PreserveSig)] <br />
&nbsp; &nbsp; void SetFile(String ^fileName); <br />
&nbsp; &nbsp; [MethodImpl(MethodImplOptions::PreserveSig)] <br />
&nbsp; &nbsp; bool Create(); <br />
&nbsp; &nbsp; [MethodImpl(MethodImplOptions::PreserveSig)] <br />
&nbsp; &nbsp; String^ ReadLine(); <br />
&nbsp; }; <br />
&nbsp;<br />
&nbsp; public ref class ClassWrap : IClassWrap <br />
&nbsp; { <br />
&nbsp; &nbsp; &nbsp;......... <br />
&nbsp; } <br />
}</div></div>
<p>L&rsquo;attribut <strong>PreserveSig</strong>, comme son nom le suggère un peu, permet de préserver la signature ; et au final de simplifier encore un brin le programme natif.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">int _tmain(int argc, _TCHAR* argv[]) <br />
{ <br />
&nbsp; CoInitialize(NULL); <br />
&nbsp; MonStreamReader::IClassWrapPtr pClass(__uuidof(MonStreamReader::ClassWrap)); <br />
&nbsp; CComBSTR filePath = &quot;c:\\test.txt&quot;; <br />
&nbsp; pClass-&gt;SetFile(filePath); <br />
&nbsp; if (pClass-&gt;Create()) <br />
&nbsp; { <br />
&nbsp; &nbsp; CComBSTR line; <br />
&nbsp; &nbsp; line = pClass-&gt;ReadLine(); <br />
&nbsp; &nbsp; while (line) <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; _bstr_t line_(line); <br />
&nbsp; &nbsp; &nbsp; std::cout &lt;&lt; line_ &lt;&lt; std::endl; <br />
&nbsp; &nbsp; &nbsp; line = pClass-&gt;ReadLine(); <br />
&nbsp; &nbsp; } <br />
&nbsp; } <br />
&nbsp; return 0; <br />
}</div></div>
<p>Dingue ca, on dirait presque du C++/CLI :p, à part un ou deux BSTR qui trainent &#8230;</p>
<p>Un dernier petit truc : ne faites pas trop confiance à l&rsquo;intellisense, il se débrouille pas trop mal, mais suite à quelques compilations, enregistrements, &#8230; dès fois, il pert un peu le nord. Heureusement que nous savons exactement ce que nous voulons faire <img src="https://blog.developpez.com/nico-pyright/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapper de classes pour COM Interop, simplification et utilisation d&#8217;interfaces</title>
		<link>https://blog.developpez.com/nico-pyright/p3821/cc-win32/wrapper_de_classes_pour_com_interop_simp</link>
		<comments>https://blog.developpez.com/nico-pyright/p3821/cc-win32/wrapper_de_classes_pour_com_interop_simp#comments</comments>
		<pubDate>Thu, 21 Jun 2007 00:19:38 +0000</pubDate>
		<dc:creator><![CDATA[nico-pyright(c)]]></dc:creator>
				<category><![CDATA[C++ Interop]]></category>
		<category><![CDATA[C/C++ - Win32]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Si vous êtes allé au bout du précédent billet, je vous félicite. Je rappelle que le but est de pouvoir utiliser un objet .net depuis notre exe natif pur et de passer des paramètres à son constructeur via un wrapper CLI. Quand je re-regarde le code du billet, il me fait peur Tout ce code pour finalement si peu de choses&#8230; N&#8217;y aurait-il pas moyen de faire plus simple ? Forcément, si j&#8217;écris ca, c&#8217;est [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Si vous êtes allé au bout du précédent billet, je vous félicite. Je rappelle que le but est de pouvoir utiliser un objet .net depuis notre exe natif pur et de passer des paramètres à son constructeur via un wrapper CLI.<br />
Quand je re-regarde le code du billet, il me fait peur <img src="https://blog.developpez.com/nico-pyright/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> Tout ce code pour finalement si peu de choses&#8230;</p>
<p>N&rsquo;y aurait-il pas moyen de faire plus simple ?</p>
<p>Forcément, si j&rsquo;écris ca, c&rsquo;est qu&rsquo;il doit y avoir un moyen&#8230;<span id="more-23"></span></p>
<p>La solution est d&rsquo;utiliser la capacité de visual studio à incorporer les tlb grâce à #import et à le convertir en classes C++.</p>
<p>Nous transformons notre précédente classe managée qui wrappe de manière simpliste le StreamReader pour faire en sorte que la classe implémente une interface :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">using namespace System; <br />
using namespace System::IO; <br />
&nbsp;<br />
namespace MonStreamReader &nbsp;<br />
{ <br />
&nbsp; public interface class IClassWrap <br />
&nbsp; { <br />
&nbsp; &nbsp; void SetFile(String ^fileName); <br />
&nbsp; &nbsp; bool Create(); <br />
&nbsp; &nbsp; String^ ReadLine(); <br />
&nbsp; }; <br />
&nbsp;<br />
&nbsp; public ref class ClassWrap : IClassWrap <br />
&nbsp; { <br />
&nbsp; private: &nbsp;<br />
&nbsp; &nbsp; String ^_fileName; <br />
&nbsp; &nbsp; StreamReader ^sr; <br />
&nbsp; protected: <br />
&nbsp; &nbsp; !ClassWrap() <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; if (sr) <br />
&nbsp; &nbsp; &nbsp; &nbsp; sr-&gt;Close(); <br />
&nbsp; &nbsp; } <br />
&nbsp; public: <br />
&nbsp; &nbsp; ClassWrap() {} <br />
&nbsp; &nbsp; ~ClassWrap() <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; this-&gt;!ClassWrap(); <br />
&nbsp; &nbsp; } <br />
&nbsp; &nbsp; virtual void SetFile(String ^fileName) <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; _fileName = fileName; <br />
&nbsp; &nbsp; } <br />
&nbsp; &nbsp; virtual bool Create() <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; if (!String::IsNullOrEmpty(_fileName)) <br />
&nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; try <br />
&nbsp; &nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sr = gcnew StreamReader(_fileName); <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true; <br />
&nbsp; &nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ^) <br />
&nbsp; &nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sr = nullptr; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false; <br />
&nbsp; &nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; return false; <br />
&nbsp; &nbsp; } <br />
&nbsp; &nbsp; virtual String^ ReadLine() <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; if (sr) <br />
&nbsp; &nbsp; &nbsp; &nbsp; return sr-&gt;ReadLine(); <br />
&nbsp; &nbsp; &nbsp; return nullptr; <br />
&nbsp; &nbsp; } &nbsp;}; <br />
}</div></div>
<p>On génère le tlb, et on l&rsquo;importe dans notre programme natif. Visual C++ va générer des classes et notamment une qui nous interesse IClassWrapPtr. C&rsquo;est un smart pointer qui permet d&rsquo;accèder à l&rsquo;objet COM.</p>
<p>Il ne reste plus qu&rsquo;à utiliser ce smart pointer pour appeler directement les méthodes de notre classe. Notez que les fonctions ont été relookées à la mode COM. On récupère un HRESULT et le retour de fonction se fait par un paramètre en sortie [out].</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#include &lt;atlsafe.h&gt; <br />
#include &lt;iostream&gt; <br />
&nbsp;<br />
#import &quot;..\debug\MonStreamReader.tlb&quot; raw_interfaces_only <br />
&nbsp;<br />
&nbsp;<br />
int _tmain(int argc, _TCHAR* argv[]) <br />
{ <br />
&nbsp; CoInitialize(NULL); <br />
&nbsp; MonStreamReader::IClassWrapPtr pClass(__uuidof(MonStreamReader::ClassWrap)); <br />
&nbsp; CComBSTR filePath = &quot;c:\\test.txt&quot;; <br />
&nbsp; if (SUCCEEDED(pClass-&gt;SetFile(filePath))) <br />
&nbsp; { <br />
&nbsp; &nbsp; unsigned char resultBool; <br />
&nbsp; &nbsp; if (SUCCEEDED(pClass-&gt;Create(&amp;resultBool))) <br />
&nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; if (resultBool) <br />
&nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; CComBSTR line; <br />
&nbsp; &nbsp; &nbsp; &nbsp; if (SUCCEEDED(pClass-&gt;ReadLine(&amp;line))) <br />
&nbsp; &nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (line) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _bstr_t line_(line); <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::cout &lt;&lt; line_ &lt;&lt; std::endl; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FAILED(pClass-&gt;ReadLine(&amp;line))) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; } <br />
&nbsp; } <br />
&nbsp; return 0; <br />
}</div></div>
<p>C&rsquo;est quand même plus digeste !!<br />
Notez l&rsquo;utilisation de CComBstr pour passer des chaines qui seront marshalées en String^.net. Par contre, mon retour booleen donne un unsigned char.<br />
Pour l&rsquo;affichage, j&rsquo;utilise également la classe _bstr_t qui dispose d&rsquo;une surcharge de char *.</p>
<p>Vous pouvez <a href="ftp://ftp-developpez.com/nico-pyright/blog/MonStreamReader.rar">télécharger le projet exemple</a>.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
