<p>In a C++ Windows (XP and NT, if it makes a difference) application I'm working on, I need to get the network name associated with the computer the code is executing on, so that I can convert local filenames from C:\filename.ext to \\network_name\C$\filename.ext. How would I do this?</p>

<p>Alternatively, if there's a function that will just do the conversion I described, that would be even better. I looked into WNetGetUniversalName, but that doesn't seem to work with local (C drive) files.</p>

[{'answer_id': 11132, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 4, 'selected': True, 'text': '<p>You\'ll want Win32\'s GetComputerName:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms724295(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms724295(VS.85).aspx</a></p>\n'}, {'answer_id': 11196, 'author': 'Pascal', 'author_id': 1311, 'author_profile': 'https://Stackoverflow.com/users/1311', 'pm_score': 3, 'selected': False, 'text': '<p>There are more than one alternatives:</p>\n\n<p>a. Use Win32\'s GetComputerName() as suggested by Stu. <br> \nExample: <a href="http://www.techbytes.ca/techbyte97.html" rel="noreferrer">http://www.techbytes.ca/techbyte97.html</a> <br>\nOR <br>\nb. Use the function gethostname() under Winsock. This function is cross platform and may help if your app is going to be run on other platforms besides Windows. <br>\nMSDN Reference: <a href="http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx</a> <br>\nOR <br>\nc. Use the function getaddrinfo(). <br>\nMSDN reference: <a href="http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx</a></p>\n'}, {'answer_id': 71343, 'author': 'jilles de wit', 'author_id': 7531, 'author_profile': 'https://Stackoverflow.com/users/7531', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with Pascal on using winsock\'s gethostname() function.\nHere you go:</p>\n\n<pre><code>#include &lt;winsock2.h&gt; //of course this is the way to go on windows only\n\n#pragma comment(lib, "Ws2_32.lib")\n\nvoid GetHostName(std::string&amp; host_name)\n{\n    WSAData wsa_data;\n    int ret_code;\n\n    char buf[MAX_PATH];\n\n    WSAStartup(MAKEWORD(1, 1), &amp;wsa_data);\n    ret_code = gethostname(buf, MAX_PATH);\n\n    if (ret_code == SOCKET_ERROR)\n        host_name = "unknown";\n    else\n        host_name = buf;\n\n\n    WSACleanup();\n\n}\n</code></pre>\n'}, {'answer_id': 33008849, 'author': 'Ajay Gupta', 'author_id': 2663073, 'author_profile': 'https://Stackoverflow.com/users/2663073', 'pm_score': 1, 'selected': False, 'text': '<p>If you want only the name of the local computer (NetBIOS) use <code>GetComputerName</code> function. It retrives only local computer name that is established at system startup, when the system reads it from the registry.</p>\n\n<pre><code>BOOL WINAPI GetComputerName(\n  _Out_   LPTSTR  lpBuffer,\n _Inout_ LPDWORD lpnSize\n);\n</code></pre>\n\n<p>More about <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295%28v=vs.85%29.aspx" rel="nofollow">GetComputerName</a></p>\n\n<p>If you want to get DNS host name, DNS domain name, or the fully qualified DNS name call the <code>GetComputerNameEx</code> function.</p>\n\n<pre><code>BOOL WINAPI GetComputerNameEx(\n  _In_    COMPUTER_NAME_FORMAT NameType,\n  _Out_   LPTSTR               lpBuffer,\n  _Inout_ LPDWORD              lpnSize\n);\n</code></pre>\n\n<p>More about <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724301%28v=vs.85%29.aspx" rel="nofollow">GetComputerNameEx</a></p>\n'}]
['https://Stackoverflow.com/questions/11127', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/179/']