<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[std::string und char*]]></title><description><![CDATA[<p>Hey!</p>
<p>CreateProcess() hat LPSTR als zweiten Parameter. Ich möchte mit std::string arbeiten, das geht hier aber wohl nicht?</p>
<p>Dies dürfte ungültig sein, da std::string nicht terminiert:</p>
<pre><code class="language-cpp">std::string string = &quot;bla.exe -test 1 2 3&quot;
CreateProcess(&quot;bla.exe&quot;, &amp;string[0], ...)
</code></pre>
<p>Dem string '\0' anhängen ignoriert std::string.</p>
<p>Was also tun?<br />
Muss ich jetzt zwingend mit char-arrays und memcpy() rumfrickeln?</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/237197/std-string-und-char</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 11:44:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/237197.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Mar 2009 17:57:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 17:57:49 GMT]]></title><description><![CDATA[<p>Hey!</p>
<p>CreateProcess() hat LPSTR als zweiten Parameter. Ich möchte mit std::string arbeiten, das geht hier aber wohl nicht?</p>
<p>Dies dürfte ungültig sein, da std::string nicht terminiert:</p>
<pre><code class="language-cpp">std::string string = &quot;bla.exe -test 1 2 3&quot;
CreateProcess(&quot;bla.exe&quot;, &amp;string[0], ...)
</code></pre>
<p>Dem string '\0' anhängen ignoriert std::string.</p>
<p>Was also tun?<br />
Muss ich jetzt zwingend mit char-arrays und memcpy() rumfrickeln?</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1685952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1685952</guid><dc:creator><![CDATA[Frage15]]></dc:creator><pubDate>Wed, 25 Mar 2009 17:57:49 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 18:06:35 GMT]]></title><description><![CDATA[<p>vielleicht</p>
<pre><code class="language-cpp">CreateProcess(&quot;bla.exe&quot;, stri.c_str(), ...)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1685955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1685955</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 25 Mar 2009 18:06:35 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 18:14:14 GMT]]></title><description><![CDATA[<p>Geht leider nicht, da der zweite Parameter char* ist, doch c_str() gibt const char* zurück.<br />
Laut MSDN sollte es funktionieren, die Kommandozeile komplett im ersten Argument zu übergeben. Geht aber anscheinend nicht, es startet kein Programm:</p>
<p>CreateProcess(&quot;bla.exe -test 1 2 3&quot;, 0, ...)</p>
<p>MSDN schreibt auch dass die Unicode-version schreibend auf Parameter 2 zugreifen könnte:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms682425" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms682425</a> (lpCommandLine 2. Absatz)</p>
<p>Jedoch geht dadurch für mich nicht 100%ig hervor dass ich bei der ASCII Version einen const_cast machen darf.</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1685964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1685964</guid><dc:creator><![CDATA[Frage15]]></dc:creator><pubDate>Wed, 25 Mar 2009 18:14:14 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 18:25:08 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char cmdLine[] = &quot;bla.exe -test 1 2 3&quot;;
CreateProcess(&quot;bla.exe&quot;, &amp;cmdLine[0], ...);

// Aber ich nehme mal an, dass du wirklich einen std::string vorliegen hast.
// Dann musst du es wohl so machen:
std::string cmdLine; // Mit was auch immer gefüllt.

// Puffer erstellen, auf den auch geschrieben werden kann.
std::vector&lt;char&gt; buffer(cmdLine.begin(), cmdLine.end());

// Nullterminierung.
buffer.push_back(0);

CreateProcess(&quot;bla.exe&quot;, &amp;buffer[0], ...);
</code></pre>
<p>const_cast wäre nach Dokumentation tatsächlich möglich. Da würde ich aber minimum CreateProcessA hinschreiben. Aber trotzdem rate ich davon ab. Da muss nur jemand anderes kommen, oder einem selbst in ein paar Monaten und man denkt sich nichts dabei, verändert was kleines und BUMM. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1685975</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1685975</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Wed, 25 Mar 2009 18:25:08 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 18:29:48 GMT]]></title><description><![CDATA[<p>Frage15 schrieb:</p>
<blockquote>
<p>Laut MSDN sollte es funktionieren, die Kommandozeile komplett im ersten Argument zu übergeben. Geht aber anscheinend nicht, es startet kein Programm:</p>
<p>CreateProcess(&quot;bla.exe -test 1 2 3&quot;, 0, ...)</p>
<p>MSDN schreibt auch dass die Unicode-version schreibend auf Parameter 2 zugreifen könnte:</p>
</blockquote>
<p>hab ich auch so verstanden:</p>
<p>MSDN schrieb:</p>
<blockquote>
<p>The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.</p>
<p><strong>The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.</strong></p>
</blockquote>
<p>Allerdings solltest du es damit mal im WinAPI-Forum und nicht im C++ Forum versuchen...</p>
<p>Ich seh gerade noch das Bsp ganz unten:</p>
<p>MSDN schrieb:</p>
<blockquote>
<pre><code class="language-cpp">LPTSTR szCmdline[] = _tcsdup( TEXT(&quot;\&quot;C:\\Program Files\\MyApp\&quot; -L -S&quot;) );
CreateProcess(NULL, szCmdline, /*...*/);
</code></pre>
</blockquote>
<p>Hast dus mal so rum probiert?<br />
(Sorum ists auch hier gemacht:<br />
<a href="http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx</a><br />
)</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1685977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1685977</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 25 Mar 2009 18:29:48 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 20:20:57 GMT]]></title><description><![CDATA[<p>Ich entscheide mich für den const_cast&lt;&gt;.</p>
<p>Ah danke <a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/16305">@unskilled</a>, so funktionierts, ich Depp hatte die Parameter auch extra in Anführungsstrichen.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686045</guid><dc:creator><![CDATA[Frage15]]></dc:creator><pubDate>Wed, 25 Mar 2009 20:20:57 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 21:18:27 GMT]]></title><description><![CDATA[<p>Frage15 schrieb:</p>
<blockquote>
<p>Ah danke <a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/16305">@unskilled</a>, so funktionierts, ich Depp hatte die Parameter auch extra in Anführungsstrichen.</p>
</blockquote>
<p>kaum macht mans richtig - schon funktionierst ^^<br />
auch, wenn sich das (die doku, also die msdn) in meinen augen ein wenig wiederspricht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";D"
      alt="😉"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686065</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 25 Mar 2009 21:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 21:44:30 GMT]]></title><description><![CDATA[<p>Frage15 schrieb:</p>
<blockquote>
<p>CreateProcess() hat LPSTR als zweiten Parameter. Ich möchte mit std::string arbeiten, das geht hier aber wohl nicht?</p>
<p>Dies dürfte ungültig sein, da std::string nicht terminiert:</p>
<pre><code class="language-cpp">std::string string = &quot;bla.exe -test 1 2 3&quot;
CreateProcess(&quot;bla.exe&quot;, &amp;string[0], ...)
</code></pre>
</blockquote>
<p>Doch, das geht so! std::string hat immer eine terminierende '\0' hinten und eine zweite brauchst du nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686074</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Wed, 25 Mar 2009 21:44:30 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 21:48:21 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>Frage15 schrieb:</p>
<blockquote>
<p>CreateProcess() hat LPSTR als zweiten Parameter. Ich möchte mit std::string arbeiten, das geht hier aber wohl nicht?</p>
<p>Dies dürfte ungültig sein, da std::string nicht terminiert:</p>
<pre><code class="language-cpp">std::string string = &quot;bla.exe -test 1 2 3&quot;
CreateProcess(&quot;bla.exe&quot;, &amp;string[0], ...)
</code></pre>
</blockquote>
<p>Doch, das geht so! std::string hat immer eine terminierende '\0' hinten und eine zweite brauchst du nicht.</p>
</blockquote>
<p>Ne, das ist absolut undefiniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686079</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686079</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 25 Mar 2009 21:48:21 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Wed, 25 Mar 2009 22:00:50 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Ne, das ist absolut undefiniert.</p>
</blockquote>
<p>Selbst wenn, dieses Verhalten findest du in allen Implementationen, die du unter Windows nutzen kannst. Geht ja ohne große Frickelei auch nicht anders, da <code>c_str()</code> einen <code>const char*</code> mit '\0' hinten herausgibt, dabei den string aber nicht ändern darf (weil const).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686084</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Wed, 25 Mar 2009 22:00:50 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Thu, 26 Mar 2009 10:17:56 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>Selbst wenn, dieses Verhalten findest du in allen Implementationen, die du unter Windows nutzen kannst.</p>
</blockquote>
<p>Ist das so? Ich denke nicht. Und selbst wenn, wer sagt Dir, dass &lt;benutzte Implementierung einsetzen&gt; das nicht beim nächsten Release ändert? Und dann wundert man sich über bizarre Fehler.<br />
Ich würde in solchen Fällen immer auf Draveres Vorschlag zurückgreifen. Damit ist man auf der sicheren Seite, und so kompliziert isses auch nicht.</p>
<p>Badestrand schrieb:</p>
<blockquote>
<p>Geht ja ohne große Frickelei auch nicht anders, da <code>c_str()</code> einen <code>const char*</code> mit '\0' hinten herausgibt, dabei den string aber nicht ändern darf (weil const).</p>
</blockquote>
<p>Ja, das ist auch gut so. Der von c_str() zurückgegebene Buffer muss nichts mit dem im String verwalteten Buffer zu tu haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686208</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 26 Mar 2009 10:17:56 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Thu, 26 Mar 2009 15:36:16 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>...Geht ja ohne große Frickelei auch nicht anders, da <code>c_str()</code> einen <code>const char*</code> mit '\0' hinten herausgibt, dabei den string aber nicht ändern darf (weil const).</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Was soll daran Frickelei sein? Einfach eine Kopie und fertig.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686424</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686424</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 26 Mar 2009 15:36:16 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Thu, 26 Mar 2009 17:15:56 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Ist das so? Ich denke nicht.</p>
</blockquote>
<p>Zumindest VS03-08, die GCC-C++-Bib und StlPort handhaben das so, warum auch sollte für <code>char&amp; operator[](int pos)</code> und <code>c_str()</code> ein anderer Buffer angegeben werden. Es geht eben nicht sinnvoll anders. Oder zeig mir wie, dann bin ich ruhig <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Simon2 schrieb:</p>
<blockquote>
<p>Was soll daran Frickelei sein? Einfach eine Kopie und fertig.</p>
</blockquote>
<p><code>return strdup(m_buf)</code> ? Zeig mal, was du meinst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686483</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 26 Mar 2009 17:15:56 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Fri, 27 Mar 2009 08:01:17 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>...</p>
<p>Simon2 schrieb:</p>
<blockquote>
<p>Was soll daran Frickelei sein? Einfach eine Kopie und fertig.</p>
</blockquote>
<p><code>return strdup(m_buf)</code> ? Zeig mal, was du meinst.</p>
</blockquote>
<p>Mal nur als Skizze:</p>
<pre><code class="language-cpp">class string {
   char* extBuf;
   char* intBuf;
   size_t len;
public:
   char const* c_str() { 
      if(changed) {
         extBuf = new char[len+1];
         copy(intBuf, intBuf+len, extBuf);
         extBuf[len] = '\0';
      }
      return extBuf;
   }
};
</code></pre>
<p>Soweit ich recht weiß, gibt der Standard keine Zusage über die &quot;Gültigkeitsdauer&quot; des CStrings, der sich hinter dem Rückgabewert von c_str() verbirgt.</p>
<p>Natürlich kann man da noch viel mit caching, reference counting, internen Arrays,.... optimieren - hier geht es nur um eine Skizze.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1686767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686767</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Fri, 27 Mar 2009 08:01:17 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Fri, 27 Mar 2009 11:08:19 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p><code>return strdup(m_buf)</code> ? Zeig mal, was du meinst.</p>
</blockquote>
<pre><code class="language-cpp">std::string bla(&quot;Besser kopieren als probieren!&quot;);
std::vector&lt;std::string::value_type&gt; buffer(bla.begin(), bla.end());
buffer.push_back('\0');
crazy_nonconst_evil_operations(&amp;buffer[0]);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1686851</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1686851</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Fri, 27 Mar 2009 11:08:19 GMT</pubDate></item><item><title><![CDATA[Reply to std::string und char* on Fri, 27 Mar 2009 16:46:26 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Mal nur als Skizze:</p>
</blockquote>
<p>Dann mach das mal, während <code>c_str()</code> const ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1687069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1687069</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Fri, 27 Mar 2009 16:46:26 GMT</pubDate></item></channel></rss>