Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetComputerName() help 1

Status
Not open for further replies.

Valius

Programmer
Oct 20, 2000
174
US
I've tried using GetComputerName(), but I'm not getting anything. Has someone used it and been successful? Any info on this subject is greatly appreciated!

Niky Williams
Niky.Williams@NTSMarketing.com
NTS Marketing
 
This is what I'm getting in the Debug window...I thought my variables were large enough to hold the data...what's going on here?

HEAP[WSockClientDEV2.exe]: Invalid allocation size - 9b9b9b9a (exceeded 7ffdefff)
 
If Your Computer has no name, or the name is longer as MAX_COMPUTERNAME_LENGTH characters, or contains characters that are outside the standard character set, You can has this Problem.
I use the Code:
char cname[MAX_COMPUTERNAME_LENGTH+1];
int lengu = MAX_COMPUTERNAME_LENGTH+1;
if(!GetComputerName( cname, &lengu)) {
bSuccess_c = FALSE;
}
an it works (NT 4.0, Windows 95/98).
 
I also was having some difficulty using the GetComputerName() function and noticed the above posting. I took the code that was posted and cut/pasted it howere I wasn't able to get it to work.

I then added what I think was the correct header file <windows.h> and declared & initialized bSuccess which reduced the error, but I still have a casting issue. Could someone help? My code is below.

Thanks.

---------------------------------------------

#include <windows.h>
int main()
{
bool bSuccess_c=TRUE; // I added this declaration
char cname[MAX_COMPUTERNAME_LENGTH+1];
int lengu = MAX_COMPUTERNAME_LENGTH+1;
if(!GetComputerName( cname, &lengu))
{
bSuccess_c = FALSE;
}
return 0;
}
 
Use UNICODE & DWORD - this does work on any platform !!!

CString csComputer
DWORD dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR charComputer[MAX_COMPUTERNAME_LENGTH + 1];

if (GetComputerName(charComputer, &dwComputerSize))
{
csComputer.Format(&quot;%s&quot;, charComputer);
csComputer.MakeUpper();
}
Spencer Window (not a joke name)
spencer.window@eastmidlandcomputers.ltd.uk
 
I'm having a little bit of trouble implementing the suggested code. I like the idea of making the code more portable. I still consider myself a beginner+ and I want to learn to write code that's portable.

Clearly I'm using VC++. I cut/pasted your code into my development environment, added a main and 'return 0' and added a semecolon after the 'CString. I then compiled the program and got 5 errors. Most of which (if not all) are centered around CString. The first error is the famous &quot;error C2065: 'CString' : undeclared identifier&quot;.

So, I suspected that I needed to include a header or additional library in my setup. I then went to my &quot;C++ Primer&quot; book by Lippman (a good reference) and my &quot;Beginning Visual C++ 6&quot; book by Horton to see what I needed to do. Although I must of have missed it, there are sections that talk about CString however they don't give specific instructions on how to declare and implement the CString class. I did however notice a program that called an include file called <cstring> so I inserted it into my code.

help?

thanks/jerry

---------------------below is my code w/cstring-------------

#include <windows.h>
#include <cstring>
int main()
{
int i;
i=2;
CString csComputer;
DWORD dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR charComputer[MAX_COMPUTERNAME_LENGTH + 1];

if (GetComputerName(charComputer, &dwComputerSize))
{
csComputer.Format(&quot;%s&quot;, charComputer);
csComputer.MakeUpper();
}
return 0;
}
 
If you made your project using the Wizard then it should have created StdAfx.h & StdAfx.cpp. The header file should be included in every other header.

The StdAfx.h includes Microsoft's standard headers for MFC stuff & the like.

Try replacing all the #include statements with #include &quot;stdafx.h&quot;

If you haven't got a stdafx.h then try
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
Spencer Window (not a joke name)
spencer.window@eastmidlandcomputers.ltd.uk
 
Wow....this is getting interesting.

I create this VC++ project using a simple console application. So the code below is the entire project. There are no other files included above and beyond the indicated header include statements.

I'm still getting the CString undeclared error which results in a total of five errors (I suspect all related to the CString issue). Below is the code.

Any ideas?

thanks,

Jerry

----- 100% of code below. Created as empty consolue project ----


#include <iostream>
#include <stdafx.h>

using namespace std;

int main()
{
CString csComputer;
DWORD dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR charComputer[MAX_COMPUTERNAME_LENGTH + 1];

if (GetComputerName(charComputer, &dwComputerSize))
{
csComputer.Format(&quot;%s&quot;, charComputer);
csComputer.MakeUpper();
}
return 0;
}
 
What version of Microsoft Visual C++ are you using ?

the line that reads using namespace std
is something do with Microsoft Studio .NET assemblies is it not ?

I'm still using MS Studio 6.0 I'm afraid, so I don't know that much about it. There is a Tek-Tips forum though, have you tried posting a question there ?

I have never had to write a console-based prog, so it's a case of the blind leading the blind !!!

Spencer Window (not a joke name)
spencer.window@eastmidlandcomputers.ltd.uk
 
I'm using Visual Studio VC++ 6.0

The &quot;using namespace std&quot; helps keep the std classes and any custom classes that I create with the same name separated...I think.

Anyway, well, thanks for all the help. I am interested in creating portable C++ code and CString seems to be fundamental to that process..so I need to figure this one out without MFC.

I suspect that since CString isn't being recognized as having been properly declared....I most likely missing either a simple 'include' or a library.

Any other ideas?

If not, let's see if anyone else comes along?

thanks again, your posts were helpful...

Jerry
 
CStrings are declared in afx.h, so put #include <afx.h> at top of prog !!!

Also, when you created the project, did you choose bottom radio button 'console application with MFC support' ? (this puts what you need into 'stdafx.h'

Spencer Window (not a joke name)
spencer.window@eastmidlandcomputers.ltd.uk
 
Whoa....got over the CString undeclared issue. <afx.h> must be where it is declared.

After I compiled the code with the proper header I immediatly got the below undeclared external variables. Typically this means from past experience that I'm missing a libarary or another header.

After seeing the &quot;nafxcwd.lib&quot; referenced in the error, I added it to my settings within libarary and got the same error.

Hate to ask again, ...but any ideas? I'ved done the usual (i.e. check for headers and lib that are missing...). I'm gonna keep trying here.....

/jerry


------------------------------------------------
//#include <iostream>
#include <afx.h>
int main()
{
CString csComputer;
DWORD dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR charComputer[MAX_COMPUTERNAME_LENGTH + 1];
if (GetComputerName(charComputer, &dwComputerSize))
{
csComputer.Format(&quot;%s&quot;, charComputer);
csComputer.MakeUpper();
}
return 0;
}

--------------------Configuration: computername - Win32 Debug--------------------
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/computername.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

computername.exe - 3 error(s), 0 warning(s)
 
Things like this can usually be found on MSDN website or CD-ROM version. I did quick search & found a fairly large article, so I've chopped the relevant bit out.

Article ID: Q138400
Linking code compiled with /MT with Libc.lib may cause unresolved externals on _beginthread, _beginthreadex, _endthread, and _endthreadex. LIBC is not multithreaded, so it does not contain these run-time functions. This case occurs with Visual C++ version 2.0. The version of MFC that shipped with 2.0 is multithread-aware.

I've just created a new console based project (with MFC support) & pasted you code straight in & it compiles & runs - suggest you do the same !!! Spencer Window (not a joke name)
spencer.window@eastmidlandcomputers.ltd.uk
 
The following works when incorporated into a console project that has been created with MFC support.

Thanks for the help....here is the working code. It does display the computer name. This code can be cut/pasted into console project that was created with MFC support.

/jerry

------------------------------------------
CString csComputer;
DWORD dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR charComputer[MAX_COMPUTERNAME_LENGTH + 1];
if (GetComputerName(charComputer, &dwComputerSize))
{
csComputer.Format(&quot;%s&quot;, charComputer);
csComputer.MakeUpper();
cout<<endl<<&quot;computer name is: &quot;<<(LPCTSTR) csComputer<<endl;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top