반응형

콘솔 윈도우(cmd)를 열지 않은 상태에서 명령을 실행하고 결과를 읽고 출력해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char psBuffer[128];
    FILE* pPipe;
 
    // Run command so that it writes its output to a pipe. Open this
    // pipe with read text attribute so that we can read it
    // like a text file.
    if ((pPipe = _popen("wmic baseboard get manufacturer, product, serialnumber""rt")) == NULL)
    {
        exit(1);
    }
 
    /* Read pipe until end of file, or an error occurs. */
    while (fgets(psBuffer, 128, pPipe))
    {
        puts(psBuffer);
    }
 
    int endOfFileVal = feof(pPipe);
    int closeReturnVal = _pclose(pPipe);
 
    if (endOfFileVal)
    {
        printf("\nProcess returned %d\n", closeReturnVal);
    } else
    {
        printf("Error: Failed to read the pipe to the end.\n");
    }
}
 

 

 

메인보드 정보가 출력된다.

 

아래와 같은 결과가 나온다면 wmic.exe를 프로젝트 폴더에 복사해 넣으면 된다.

'wmic'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치 파일이 아닙니다.

Process returned 1

 

반응형

'C, C++' 카테고리의 다른 글

Detect Windows Display Scale Factor 윈도우 배율 확인  (0) 2025.12.22
Qt platform plugin error fix  (0) 2021.09.26
Qt6 설치 및 간단한 사용법  (0) 2021.09.25
MariaDB(MySQL) C API  (0) 2021.08.29
SQLite - C/C++  (0) 2021.08.27
Posted by J-sean
:
반응형

ipconfig 명령을 이용해 IP 주소 구성을 삭제 하거나 갱신하는 프로그램을 만들어 보자.

IP 주소 구성 삭제시 인터넷 연결을 끊을 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.Diagnostics;
using System.Security;
 
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SetInternetConnection("release"); // 인터넷 연결 해제
            //SetInternetConnection("renew"); // 인터넷 연결
        }
 
        static void SetInternetConnection(string command)
        {
            try
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo()
                {
                    FileName = "cmd.exe",
                    Arguments = "/c ipconfig /" + command,
                    WindowStyle = ProcessWindowStyle.Hidden
                };
 
                //ProcessStartInfo processStartInfo = new ProcessStartInfo();
                //processStartInfo.FileName = "cmd.exe";
                //processStartInfo.Arguments = "/c ipconfig /" + command;
                //processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 
                Process.Start(processStartInfo);
                // This class contains a link demand at the class level that applies
                // to all members. A SecurityException is thrown when the immediate
                // caller does not have full-trust permission.
            }
            catch (SecurityException e)
            {
                Console.WriteLine("Security Exception: {0}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

프로그램을 실행하면 인터넷 연결이 끊긴다.

 

SetInternetConnection() 호출 주석을 바꾸면 인터넷이 연결된다.

 

반응형
Posted by J-sean
:
반응형

'Open Command Line' opens a command line at the root of the project. Support for all consoles such as CMD, PowerShell, Bash, etc. Provides syntax highlighting, Intellisense and execution of .cmd and .bat files.


This extension adds a new command to the project context menu that will open a command prompt on the project's path. If the solution node is selected in Solution Explorer, then a console will open at the root of the .sln file.


Open Command LIne은 작업 중인 Visual Studio 프로젝트 폴더에서 바로 command prompt를 열 수 있게 해주는 extension 입니다.

CMD, PowerShell, Bash등을 지원합니다.


Run Tools - Extensions and Updates...


Search 'Open Command Line' on Online tab and click Download.


Installation starts when Visual Studio closes.


Close Visual Studio and click Modify on VSIX Installer.


Install it.


Click Close.




Right-click on Solution Explorer - Open Command Line - Default(cmd) (Shortcut: Alt + Space)


Command Prompt opens on the project's path.


Right-click on Solution Explorer - Open Command Line - Settings...


You can change settings.




반응형
Posted by J-sean
: