Display computer name to a label

Reverend Jim commented: I don't see the point in this. -3
rproffitt commented: Too vague. Might be Linux, Pi, smart badge, web page or any number of things. Elaborate and show what you tried so far. +17

My browser keep crashing

I'm using windows

I don't know why DaniWeb keep on crashing whenever I try to write

See
Screenshot_20250312_122133_com.android.chrome.jpg

Error can't convert char to Text when I output ComputerName to label

Cannot convert parameter 1 from char[16] to system::String ^

How about posting some code?

<edit> Sorry. I didn't scroll back far enough to see the link.

Cannot convert parameter 1 from char[16] to system::String ^

You need to convert your char array to a CLI string in the first instance, before trying to pass it onto some other function expecting a CLI string.
https://learn.microsoft.com/en-us/cpp/extensions/string-cpp-component-extensions?view=msvc-170

I don't know why DaniWeb keep on crashing whenever I try to write

On my phone (in two different browsers), the thing in the rounded square is the number of open tabs.

Maybe you have an enormous number of tabs open, and you just ran out of memory.

I tried that but I get this error
     Error  14  error C2440: 'initializing' : cannot convert from 'char [16]' to 'System::String ^'

Regarding browser crashing, only with Dani its crashes on the other sites it doesn't crash 

I tried that but I get this error

Prove it - post some code.

You won't believe how many times people claim "I've done X" only to find out many posts later that they haven't done X at all, or they managed to fumble it and do "Y" instead.

char ComputerName [MAX_COMPUTERNAME_LENGTH + 1];
            DWORD cbComputerName = sizeof (ComputerName);
            if(GetComputerName (ComputerName, &cbComputerName)){
                String^ pcname(ComputerName);

                label9->Text =  pcname;
            }

Back to the guessing games....

https://learn.microsoft.com/en-us/cpp/extensions/string-cpp-component-extensions?view=msvc-170
Lists 3 different namespaces where things called 'String' are present.

using namespace Platform;
using namespace default;
using namespace System;

Or maybe you have your own String implementation, who knows (we don't).

What version of the compiler are you using?

As a test, does this compile without error?
String^ pcname("Barney");

commented: Not OP but tag tells c++ but not which one or code shown. I have also run into VC++ 1.5 users that I can't help with. +17

I'm using Microsoft Visual Studio 2010 (Visual C++ Windows Form Project).
Regarding the test yes it doe compile/build/run with no error. Here are the includes

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <Windows.h>
#include <WinSock.h>

Here are the namespaces

using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO;
    using namespace System::Net;
    using namespace System::Text::RegularExpressions;
    using namespace std; // This I recently added as I was trying to solve this issue using different solutions from the internet.
commented: I see that your system is no longer supported from what I can find. Nov 2024 was recent so while that's in play, so is the lack of code sharing. +17

Eek! I’m so sorry. We upgraded our editor last week and I suppose it’s possible a bug was introduced in my code?

Can you please start a new thread in the Meta DaniWeb forum explaining exactly what you were clicking on, etc. just before it crashes, along with your browser and operating system. I don’t really want to conflate this thread with debugging DaniWeb.

@Dani this happens when I type a longer message than it froze and crash. I just tried testing using a different browser and it seems ok with Tor browser, the crashing happens on Chrome browser on my Android phone.

I guess the issue is only picked up by Chrome. I know chrome does give issues, I saw while I was developing a browser extension, Firefox is always the best.

As you can see I wrote this long message and it didn't crash, but if I were to type on a Chrome it would have crashed without even reaching 10 words.

I've managed to find a solution on my own and it was something so simple, I'm not sure why C++ programmers were unable to pick this up.
Basically I need to output ComputerName using gcnew String() so it be label9->Text = gcnew String(ComputerName);

Thanks to everyone who tried hepling. I will try to find out what does this means and why it is used to better understand this.

For VS2010 project support in VS2022, to compile your example I did the following:

Created header stdafx.h:

#pragma once

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <Windows.h>
#include <WinSock.h>
#include <msclr/marshal_cppstd.h> 

namespace MR_MNamespace {
    void DisplayComputerName();
}

Created mr_m.cpp:

#include "stdafx.h"

using namespace System;
using namespace MR_MNamespace;

namespace MR_MNamespace {
    void DisplayComputerName() {
        wchar_t ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
        DWORD cbComputerName = MAX_COMPUTERNAME_LENGTH + 1;

        if (GetComputerNameW(ComputerName, &cbComputerName)) {
            std::wcout << L"Computer Name: " << ComputerName << std::endl;
        }
    }
}

int main() {

    DisplayComputerName();

    String^ managedString = gcnew String(L"Hello DaniWeb from .NET!");
    std::string stdString = msclr::interop::marshal_as<std::string>(managedString);

    std::cout << "Managed String: " << stdString << std::endl;

    return 0;
}

To compile, I set the Properties.
Configuration Properties->C/C++->General-> Common Language RunTime Support->Common Language RunTime Support (/clr)
Configuration Properties->C/C++->Code Generation-> Enable C++ Exceptions-> Yes with SEH Exceptions (/EHa)
Configuration Properties->C/C++->Code Generation-> Basic Runtime Checks-> Default
Configuration Properties->C/C++->Command Line-> /AI"C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

Compiles:

Computer Name: NASA-PC
Managed String: Hello DaniWeb from .NET!

To get the VS2010 Visual C++ Windows Forms with VS2022 to work, I did the following:

With your Visual Studio Installer make sure the .NET Desktop Development and Desktop Development with C++ workloads are installed.
Created mr_m.h

#pragma once
#include <windows.h> 
using namespace System;
using namespace System::Windows::Forms;

public ref class Mr_M : public Form
{
public:
    Mr_M()
    {
        this->Text = "Mr M v1.0";
        this->Size = System::Drawing::Size(350, 350);

        Label^ label9 = gcnew Label();
        label9->Location = System::Drawing::Point(50, 50);
        label9->Size = System::Drawing::Size(300, 20);
        this->Controls->Add(label9);

        wchar_t ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
        DWORD cbComputerName = MAX_COMPUTERNAME_LENGTH + 1; 

        if (GetComputerNameW(ComputerName, &cbComputerName)) {
            String^ pcname = gcnew String(ComputerName); 
            label9->Text = "Computer name: " + pcname; 
        }
        else {
            label9->Text = "Failed to retrieve computer name.";
        }
    }
};

Created mr_m.cpp

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

#include "mr_m.h"

[STAThread]
void Main()
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    Application::Run(gcnew Mr_M());
}

I changed the Properties.
Configuration Properties->C/C++->General-> Common Language RunTime Support->Common Language RunTime Support (/clr)
Configuration Properties->C/C++->Code Generation-> Enable C++ Exceptions-> Yes with SEH Exceptions (/EHa)
Configuration Properties->C/C++->Code Generation-> Basic Runtime Checks-> Default
Configuration Properties->C/C++->Command Line-> /AI"C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
Configuration Properties->Linker->System-> Windows (/SUBSYSTEM:WINDOWS)
Configuration Properties->Linker->Advanced->Entry Point-> Main

Edit project file mr_m.vcxproj, under <ItemGroup> add the References:

  </ItemDefinitionGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Drawing" />
     <ClCompile Include="mr_m.cpp">
      <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsManaged>
      <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsManaged>
      <RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">MultiThreadedDebugDLL</RuntimeLibrary>
      <RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='Release|x64'">MultiThreadedDebugDLL</RuntimeLibrary>
    </ClCompile>
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="mr_m.h" />
  </ItemGroup>

You'll have a 350x350 Windows Form application:

Mr M v1.0

Computer name: NASA-PC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.