반응형

Visual Studio를 사용하다 터미널(콘솔) 창을 열어야 할 경우가 있다. Window+R - cmd 명령을 실행해 작업 경로로 이동해도 되지만 Visual Studio에서 바로 작업 경로 터미널 창을 열 수 있다.

 

View - Terminal (단축키: Ctrl+`)

 

Developer PowerShell 창이 열린다.

 

PowerShell 창에서 작업해도 되지만 원한다면 Deveoper Command Prompt로 바꿀 수 있다.

 

Options - Terminal 을 확인해 보면 Developer PowerShell이 기본으로 설정되어 있다.

 

Developer Command Prompt를 기본으로 설정한다. (Set as Default)

 

View - Terminal (단축키: Ctrl+`) 을 선택하면 Developer Command Prompt가 열린다.

 

Tools - Command Line - Developer Command Prompt / Developer PowerShell 을 선택해도 원하는 창을 바로 열 수 있다.

 

차이가 있다면 Visual Studio에서 열리는게 아닌, 독립된 창으로 열린다.

 

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

콘솔 윈도우(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
:
반응형

어셈블리 언어로 작성된 프로그램 실행 시 인수를 확인해 보자.

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
include masm32rt.inc
 
.data
    strTitle db "Command Line Arguments",0
    strMessage db 128 dup(?)
 
.code
start:
    invoke GetCL, 1, ADDR strMessage
    invoke MessageBox, 0, ADDR strMessage, ADDR strTitle, MB_OK
    invoke ExitProcess, 0
end start
 

 

GetCL 함수의 첫 번째 인수로 1을 주면 프로그램의 첫 번째 인수가 저장된다.

 

첫 번째 인수 'abcd'가 출력된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
include masm32rt.inc
 
.data
    strTitle db "Command Line Arguments",0
    strMessage db 128 dup(?)
 
.code
start:
    invoke GetCL, 0, ADDR strMessage
    invoke MessageBox, 0, ADDR strMessage, ADDR strTitle, MB_OK
    invoke ExitProcess, 0
end start
 

 

GetCL 함수의 첫 번째 인수로 0을 주면 프로그램 파일명이 저장된다.

 

파일 이름 First.exe가 출력된다.

 

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

안드로이드 리눅스 커널 명령을 실행해 보자.

 

레이아웃에 에디트박스, 버튼, 텍스트뷰를 적당히 배치한다.

 

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
53
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    Button button;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editTextTextPersonName);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer stringBuffer = new StringBuffer();
 
                java.lang.Process process;
                try {
                    process = Runtime.getRuntime().exec(editText.getText().toString());
                    process.waitFor();
                    // Causes the current thread to wait, if necessary, until the process
                    // represented by this Process object has terminated.
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuffer.append(line + "\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String result = stringBuffer.toString();
                textView.setText(result);
            }
        });
    }
}
 

 

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

 

실행하면 위와같은 화면이 나타난다.

 

간단한 명령어를 입력하고 결과를 확인한다.

 

 

명령어 실행시 매번 새로운 프로세스에서 실행되기 때문에 연속적인 작업을 할 수 는 없다.

예) cd /proc, pwd를 차례로 실행해도 결과는 항상 /이다.

 

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
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    Button button;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editTextTextPersonName);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Runtime.getRuntime().exec(editText.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
 

 

소스를 위와 같이 바꾸고 input 명령어를 사용해 보자.

(process.waitFor() 를 사용하면 아래 예에서 제대로 동작하지 않는다)

 

input keyevent KEYCODE_BACK 명령을 실행하면 Back 버튼이 눌린것 처럼 동작한다.

 

input keyevent KEYCODE_CALL 명령을 실행하면 전화 화면이 나타난다.

※ 참고

KeyEvent

 

반응형
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
: