Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #include <stdio.h>
- #include <windows.h>
- int main(int argc, char *argv[])
- {
- char devname[MAX_PATH] = "\\\\.\\";
- HANDLE serial;
- BOOL ret;
- PROCESS_INFORMATION pi;
- DCB dcb = {
- .DCBlength = sizeof(DCB),
- .BaudRate = CBR_115200,
- .ByteSize = 8,
- .Parity = NOPARITY,
- .StopBits = ONESTOPBIT,
- };
- if (argc != 2) {
- puts("no device supplied");
- return 1;
- }
- if (strlen(devname) + strlen(argv[1]) > MAX_PATH) {
- puts("invalid device");
- return 1;
- }
- strcat(devname, argv[1]);
- serial = CreateFile(devname, GENERIC_READ | GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (serial == INVALID_HANDLE_VALUE) {
- puts("CreateFile() failed");
- return 1;
- }
- SetCommState(serial, &dcb);
- /* Method 1: via STARTUPINFO.hStd{Input,Output,Error} */
- STARTUPINFO si = {
- .cb = sizeof(si),
- .dwFlags = STARTF_USESTDHANDLES,
- .hStdInput = serial,
- .hStdOutput = serial,
- .hStdError = serial,
- }
- ret = CreateProcess(NULL, "cmd.exe", NULL, NULL, FALSE,
- HIGH_PRIORITY_CLASS, NULL, "C:\\", &si, &pi);
- /* Method 2: via handle inheritance */
- if (!SetStdHandle(STD_INPUT_HANDLE, serial)) {
- puts("SetStdHandle(STD_INPUT_HANDLE) failed");
- return 1;
- }
- if (!SetStdHandle(STD_OUTPUT_HANDLE, serial)) {
- puts("SetStdHandle(STD_OUTPUT_HANDLE) failed");
- return 1;
- }
- if (!SetStdHandle(STD_ERROR_HANDLE, serial)) {
- puts("SetStdHandle(STD_ERROR_HANDLE) failed");
- return 1;
- }
- ret = CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE,
- HIGH_PRIORITY_CLASS, NULL, "C:\\", &si, &pi);
- if (!ret) {
- puts("CreateProcess() failed");
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement