How can i get the system's name? (computer-name)

Posted By: Toon

How can i get the system's name? (computer-name) - 05/05/12 22:01

What is the easiest way to get the system's name / computer-name?
Posted By: fogman

Re: How can i get the system's name? (computer-name) - 05/06/12 01:05

Afaik it is "GetComputerName". Link:

GetComputerName


Posted By: Toon

Re: How can i get the system's name? (computer-name) - 05/06/12 15:16

Oke this was exactly what i was looking for but I don't know how to use it exept for including the windows.h

I tried this (just a guess :P), but it doesn't work

Code:
STRING* comp = "#128";
GetComputerName(comp);


Posted By: WretchedSid

Re: How can i get the system's name? (computer-name) - 05/06/12 16:23

You should have looked at the link which perfectly describes all parameters:
Code:
STRING *comp;

void getComputerName()
{
    char buffer[MAX_COMPUTERNAME_LENGTH + 1];
    long size = MAX_COMPUTERNAME_LENGTH + 1;

    memset(buffer, 0, size);


    if(GetComputerName(buffer, &size))
    {
         str_cpy(comp, buffer);
    }
    else
    {
         str_cpy(comp, "Couldn't get the computer name");
    }
}


Posted By: Toon

Re: How can i get the system's name? (computer-name) - 05/10/12 09:37

Thanks for the reply! I did look at the msdn-page but I have no clue whatsoever about this 'real c-programming' tongue. I learned c-script back in the days of A6 and made the transition to lite-c recently...

But now my next question ^^ the code you posted returned the following error:

Error in line 10: syntax error < char buffer [MAX_COMPUTERNAME_LENGTH + 1]; >

Here is the code of the test I tried to run:

Code:
#include <acknex.h>
#include <windows.h> 
#include <default.c>

STRING *comp;

void getComputerName()
{
    char buffer [MAX_COMPUTERNAME_LENGTH + 1];
    long size = MAX_COMPUTERNAME_LENGTH + 1;

    memset(buffer, 0, size);


    if(GetComputerName(buffer, &size))
    {
         str_cpy(comp, buffer);
    }
    else
    {
         str_cpy(comp, "Couldn't get the computer name");
    }
    error(comp);
}

void main()
{
  while(!key_enter){wait(1);}
  getComputerName();
}


Posted By: Uhrwerk

Re: How can i get the system's name? (computer-name) - 05/10/12 14:25

Because you didn't define that constant.
Code:
#define MAX_COMPUTERNAME_LENGTH 32


Additionally the lite-c compilter doesn't like expressions where it expects constants. S you should be on the safe side when using
Code:
char buffer [32];
long size = 32;


instead.
Posted By: WretchedSid

Re: How can i get the system's name? (computer-name) - 05/10/12 14:34

Originally Posted By: Uhrwerk
Additionally the lite-c compilter doesn't like expressions where it expects constants. S you should be on the safe side when using

Are you sure? Last time I checked, it worked quite good, at least in structs.

Also, MAX_COMPUTERNAME_LENGTH should be defined in windows.h, at least thats were I expected it.
Posted By: Toon

Re: How can i get the system's name? (computer-name) - 05/10/12 15:32

Thanks people! Here is the finished code for those who are interested:

Code:
#include <acknex.h>
#include <windows.h> 

STRING* comp = "#128";

void getComputerName()
{
    char buffer [128];
    long size = 128;

    memset(buffer, 0, size);
    
    if(GetComputerName(buffer, &size))
    {
         str_cpy(comp, buffer);
    }
    else
    {
         str_cpy(comp, "Couldn't get the computer name");
    }
    
    error(comp);
}

void main()
{
  getComputerName();
}


Posted By: Uhrwerk

Re: How can i get the system's name? (computer-name) - 05/10/12 16:50

Originally Posted By: JustSid
Are you sure? Last time I checked, it worked quite good, at least in structs.
No, I am not sure. Checked that several month ago. Might well be that JCL changed it in the meantime.

Originally Posted By: JustSid
Also, MAX_COMPUTERNAME_LENGTH should be defined in windows.h, at least that's were I expected it.
Should but isn't.
Posted By: frankjiang

Re: How can i get the system's name? (computer-name) - 05/12/12 14:06

you can write dll above this function by Cpp anything you want to do
© 2024 lite-C Forums