<?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[Problem mit getUserName]]></title><description><![CDATA[<p>Ich habe es noch nicht geschaft den Usernamem mit GetuserName auszulesen, irgendwie hapert es immer an dem 2ten Parameter.<br />
Kann mir bitte jemand einen Codesnippet posten, da man imm Internet kaum infos findet.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/154202/problem-mit-getusername</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Jul 2026 13:35:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/154202.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Jul 2006 11:33:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit getUserName on Tue, 25 Jul 2006 11:33:13 GMT]]></title><description><![CDATA[<p>Ich habe es noch nicht geschaft den Usernamem mit GetuserName auszulesen, irgendwie hapert es immer an dem 2ten Parameter.<br />
Kann mir bitte jemand einen Codesnippet posten, da man imm Internet kaum infos findet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1103739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1103739</guid><dc:creator><![CDATA[xgx]]></dc:creator><pubDate>Tue, 25 Jul 2006 11:33:13 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit getUserName on Tue, 25 Jul 2006 11:52:03 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;new&gt;
#include &lt;windows.h&gt;

using namespace std;

int main( )
{
    char dummy;
    char *buffer;
    unsigned long length = 1;

    GetUserName( dummy, &amp;length );

    if( GetLastError( ) == ERROR_MORE_DATA ) {

        try {

            buffer = new char[ length + 1 ];

        } catch( std::bad_alloc ) {

            MessageBox( 0, &quot;Not enough memory!&quot;, &quot;Error:&quot;, MB_OK );
            return EXIT_FAILURE;
        }

        GetUserName( buffer, &amp;length );

    } else {

        MessageBox( 0, &quot;GetUserName() failed!&quot;, &quot;Error:&quot;, MB_OK );
        return EXIT_FAILURE;
    }

    MessageBox( 0, buffer, &quot;Username:&quot;, MB_OK );

    delete [ ] buffer;
}
</code></pre>
<p>Greetz, Swordfish</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1103750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1103750</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Tue, 25 Jul 2006 11:52:03 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit getUserName on Tue, 25 Jul 2006 12:01:16 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char userName[UNLEN];
DWORD bufferSize = sizeof(userName);
GetUserName(userName, &amp;bufferSize);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1103761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1103761</guid><dc:creator><![CDATA[unlen]]></dc:creator><pubDate>Tue, 25 Jul 2006 12:01:16 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit getUserName on Tue, 25 Jul 2006 15:57:37 GMT]]></title><description><![CDATA[<p>TCHAR statt char!<br />
UNICODE!/ANSI!</p>
<p>Swordfish schrieb:</p>
<blockquote>
<pre><code class="language-cpp">char dummy;
    char *buffer;
    unsigned long length = 1;

    GetUserName( dummy, &amp;length );
</code></pre>
</blockquote>
<p>Warum so kompliziert ?!:</p>
<pre><code class="language-cpp">DWORD dwBufferLength = 0L;
GetUserName(NULL, &amp;dwBufferLength);
PTCHAR pszNameBuffer = new TCHAR[dwBufferLength];
GetUserName(pszNameBuffer , &amp;dwBufferLength);
MessageBox(NULL, pszNameBuffer, NULL, 0);
</code></pre>
<p>PS: Fehlerüberprüfung für new ist nicht nötig, wer kein Speicher hat, hat andere Probleme. dwBufferLength muss lediglich auf 0 gesetzt werden, damit der erste Aufruf uns die Länge des Namen ermitteln kann.</p>
<p>siehe:</p>
<p>MSDN schrieb:</p>
<blockquote>
<p><strong>nSize</strong><br />
Pointer to a DWORD variable that, on input, specifies the maximum size, in characters, of the buffer specified by the lpBuffer parameter. If the function succeeds, the variable receives the number of characters copied to the buffer. If the buffer is not large enough, the function fails and the variable receives the required buffer size, in characters, including the terminating null character.</p>
</blockquote>
<p>Die Sache mit UNLEN find ich nicht sehr schön. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1103953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1103953</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Tue, 25 Jul 2006 15:57:37 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit getUserName on Tue, 25 Jul 2006 17:11:19 GMT]]></title><description><![CDATA[<p>unlen schrieb:</p>
<blockquote>
<pre><code class="language-cpp">char userName[UNLEN];
DWORD bufferSize = sizeof(userName);
GetUserName(userName, &amp;bufferSize);
</code></pre>
</blockquote>
<p>Dazu vllt. noch was: <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /></p>
<p>MSDN schrieb:</p>
<blockquote>
<p>A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in LMCONS.H.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> beziehungsweise:</p>
<p>LMCONS.H schrieb:</p>
<blockquote>
<p>NOTE: Lengths of strings are given as the maximum lengths of the<br />
string in characters (not bytes). This does not include space for the<br />
terminating 0-characters. When allocating space for such an item,<br />
use the form:</p>
<p>TCHAR username[UNLEN+1];</p>
</blockquote>
<p>Kurz: es fehlt ein:</p>
<pre><code class="language-cpp">#include &lt;LMCONS.H&gt;
</code></pre>
<p>und es muss heißen:</p>
<pre><code class="language-cpp">char userName[UNLEN + 1];
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/26a0.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--warning"
      title=":warning:"
      alt="⚠"
    /><br />
Auch hier würde ich die <em>UNICODE-unabhängige Verwendung von TCHAR</em> vorziehen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1104007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1104007</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Tue, 25 Jul 2006 17:11:19 GMT</pubDate></item></channel></rss>