Jump to content

[SOLVED] Why doesn't this get the name? C++


Recommended Posts

I found the following code example based off of the windows API. i already know a good bit about the windows API and already can make windows, windows inside of windows, dialogs, modeless dialogs, and quite a few controls.  Right now I was trying to get together some of the information from the users computer, but this is throwing a lot of C++ compiler errors.

#include <string>
#include <iostream.h>

string GetLocalComputerName() {
  TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  string strRetVal;
  DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1;

  if(GetComputerName(chrComputerName,&dwBufferSize)) {
    // We got the name, set the return value.
    strRetVal = chrComputerName;
  } else {
    // Failed to get the name, call GetLastError here to get
    // the error code.
    strRetVal = "";
  }

  return(strRetVal);
}

[compilererrors]

C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.|

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp|7|error: `string' does not name a type|

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp||In function `LRESULT WndProc(HWND__*, UINT, WPARAM, LPARAM)':|

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp|44|error: `string' was not declared in this scope|

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp|44|error: expected `;' before "tmp"|

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp|44|warning: unused variable 'string'|

||=== Build finished: 3 errors, 2 warnings ===|

[/compilererrors]

That was what happened when I used his code directly. I already knew enough about to change over from the deprecated headers so I fixed that by changing the header to reflect the undeprecated version, rebuilt, and got this. I also fixed a few othre errors I knew had to fix but I was left with:

[compilererrors]

C:\xampp\htdocs\cpp projects\Personal Multi-Tool\main.cpp|7|error: `string' does not name a type|

||=== Build finished: 1 errors, 0 warnings ===|

[/compilerrrors]

 

My new function is

[function]

string GetLocalComputerName() {

  TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1];

  string strRetVal;

  DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1;

 

  if(GetComputerName(chrComputerName,&dwBufferSize)) {

    // We got the name, set the return value.

    strRetVal = chrComputerName;

  } else {

    // Failed to get the name, call GetLastError here to get

    // the error code.

    strRetVal = "";

  }

 

  return(strRetVal);

}

 

[/function]

Any advice?

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.