teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コード記載ミスにより

2021/03/19 05:10

投稿

ykondo
ykondo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -108,9 +108,6 @@
108
108
  IntPtr SubMenuhWnd = GetSubMenu((IntPtr)MenuhWnd, 0);
109
109
  textBox1.AppendText(Convert.ToString(SubMenuhWnd.ToInt32(), 16) + "\r" + "\n");
110
110
 
111
- SendMessage(SubMenuhWnd, WM_LBUTTONDOWN, MK_LBUTTON, "0");
112
- SendMessage(SubMenuhWnd, WM_LBUTTONUP, 0x00000000, "0");
113
-
114
111
  // 以下に1⃣のコードを書きたい。
115
112
 
116
113
  /*

1

radian様のご指摘に基づきまして、修正致しました。

2021/03/19 05:10

投稿

ykondo
ykondo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -2,10 +2,14 @@
2
2
 
3
3
  理由は、 Eding-CNCというソフトを起動させたいのですが、
4
4
 
5
- ① Process.Startを使用しても、Errorが起こり、立ち上がらない。
5
+ ① Process.Startを使用しても、起動Errorが起こり、立ち上がらない。
6
6
 
7
+ エラー内容 TOS_ERROR CreateProcess returned 5(アクセスが拒否されました。)
8
+
7
9
  ② Process.Startで【ファイル名を指定して実行】を起動し、ウインドウハンドル検索後、SendMessageでコマンドを送信( -verb runasを付与 )して起動させようとしても、管理者として起動できない。
8
10
 
11
+ Eding-CNC側の方で、【 server priority not REALTIME, Please start as administrator 】という警告が出てしまいます。
12
+
9
13
  ③ 新しいタスクの実行(N)で、「このタスクに管理者特権を付与して作成します。」にチェックを入れて起動すると、きちんと起動する。
10
14
 
11
15
  からです。
@@ -22,28 +26,147 @@
22
26
 
23
27
  何卒ご教授願います。
24
28
 
25
- ProcessStartInfo pInfo = new ProcessStartInfo();
26
- pInfo.FileName = "taskmgr";
29
+ 開発環境 Windows10 VisualStudio2019
27
- //pInfo.Verb = "RunAs";
28
- pInfo.UseShellExecute = true;
29
- Process.Start(pInfo);
30
+ プロジェクトの種類 WindowsForms
31
+ フレームワークの種類 .NET Framework4.7.2
30
32
 
33
+ ```using System;
34
+ using System.Collections.Generic;
35
+ using System.ComponentModel;
36
+ using System.Data;
37
+ using System.Drawing;
38
+ using System.Linq;
39
+ using System.Text;
31
- System.Threading.Thread.Sleep(100);
40
+ using System.Threading.Tasks;
41
+ using System.Windows.Forms;
42
+ using System.Diagnostics;
43
+ using System.Runtime.InteropServices;
32
44
 
33
- IntPtr hWnd = FindWindow("TaskManagerWindow", "タスク マネージャー");
34
- if (hWnd != IntPtr.Zero)
45
+ namespace ExternalProgramLoadTest
35
46
  {
36
- //メニューハンドルをゲット
37
- int MenuhWnd = GetMenu(hWnd.ToInt32());
47
+ public partial class Form1 : Form
38
-
39
- if (MenuhWnd != 0)
40
48
  {
41
- //サブメニューのハンドルをゲット 第2引数はインデックス番号
49
+ [DllImport("user32.dll", CharSet = CharSet.Auto)]
42
- IntPtr SubMenuhWnd = GetSubMenu((IntPtr)MenuhWnd, 0);
50
+ static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
43
51
 
44
- ここから、わかりません。
52
+ [DllImport("user32.dll", CharSet = CharSet.Auto)]
53
+ static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);
45
54
 
46
-    1⃣【ファイル】のサブメニューにある【新しいタスクの実行(N)】を選択
55
+ [DllImport("User32.dll")]
56
+ public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
57
+
58
+ [DllImport("user32.dll")]
59
+ static extern int GetMenu(int hWnd);
60
+
61
+ [DllImport("user32.dll")]
62
+ static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);
63
+
64
+ [DllImport("user32.dll")]
65
+ static extern bool SetForegroundWindow(IntPtr hWnd);
66
+
67
+ [DllImport("user32.dll")]
68
+ static extern int TrackPopupMenu(IntPtr hMenu, int uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr prcRect);
69
+
70
+ private const int WM_SETTEXT = 0x000c;
71
+
72
+ private const int WM_LBUTTONDOWN = 0x201;
73
+ private const int WM_LBUTTONUP = 0x202;
74
+ private const int MK_LBUTTON = 0x0001;
75
+
76
+ private const int WM_COMMAND = 0x0111;
77
+
78
+ private const int TPM_LEFTBUTTON = 0;
79
+ private const int TPM_RETURNCMD = 0x100;
80
+
81
+ public Form1()
82
+ {
47
-      して、『新しいタスクの作成ダイアログ』を開く
83
+ InitializeComponent();
48
- }
84
+ }
85
+
86
+ private void button1_Click(object sender, EventArgs e)
87
+ {
88
+ ProcessStartInfo pInfo = new ProcessStartInfo();
89
+ pInfo.FileName = "taskmgr";
90
+ //pInfo.Verb = "RunAs";
91
+ pInfo.UseShellExecute = true;
92
+ Process.Start(pInfo);
93
+
94
+ System.Threading.Thread.Sleep(1000);
95
+
96
+ IntPtr hWnd = FindWindow("TaskManagerWindow", "タスク マネージャー");
97
+ textBox1.AppendText(Convert.ToString(hWnd.ToInt32(), 16) + "\r" + "\n");
98
+
99
+ if (hWnd != IntPtr.Zero)
100
+ {
101
+ //メニューハンドル取得
102
+ int MenuhWnd = GetMenu(hWnd.ToInt32());
103
+ textBox1.AppendText(Convert.ToString(MenuhWnd, 16) + "\r" + "\n");
104
+
105
+ if (MenuhWnd != 0)
106
+ {
107
+ //サブメニューのハンドルを取得 第2引数はインデックス番号を指定
108
+ IntPtr SubMenuhWnd = GetSubMenu((IntPtr)MenuhWnd, 0);
109
+ textBox1.AppendText(Convert.ToString(SubMenuhWnd.ToInt32(), 16) + "\r" + "\n");
110
+
111
+ SendMessage(SubMenuhWnd, WM_LBUTTONDOWN, MK_LBUTTON, "0");
112
+ SendMessage(SubMenuhWnd, WM_LBUTTONUP, 0x00000000, "0");
113
+
114
+ // 以下に1⃣のコードを書きたい。
115
+
116
+ /*
117
+ //自分自身を最前面へ
118
+ SetForegroundWindow((IntPtr)this.Handle);
119
+
120
+ //選択したメニュー項目のIDを取得
121
+ int intID = TrackPopupMenu(SubMenuhWnd, TPM_LEFTBUTTON | TPM_RETURNCMD,
122
+ this.Left, this.Top, 0, (IntPtr)this.Handle, (IntPtr)0);
123
+ textBox1.AppendText(intID.ToString() + "\r" + "\n");
124
+
125
+ //メッセージ送信
126
+ int ret = SendMessage(hWnd, WM_COMMAND, intID, null);
127
+ //textBox1.AppendText(ret.ToString() + "\r" + "\n");
128
+ */
49
- }
129
+ }
130
+ else
131
+ {
132
+ MessageBox.Show("メニューハンドル取得失敗!");
133
+ }
134
+ }
135
+ else
136
+ {
137
+ MessageBox.Show("タスクマネージャーが見つかりませんでした。");
138
+ }
139
+
140
+
141
+ hWnd = FindWindow("#32770", "新しいタスクの作成");
142
+ textBox1.AppendText(Convert.ToString(hWnd.ToInt32(), 16) + "\r" + "\n");
143
+
144
+ if (hWnd != IntPtr.Zero)
145
+ {
146
+ IntPtr hWnd_Edit = FindWindowEx(hWnd, IntPtr.Zero, "ComboBox", null);
147
+ IntPtr hWnd_Admin = FindWindowEx(hWnd, IntPtr.Zero, "Button", "このタスクに管理者特権を付与して作成します。");
148
+ IntPtr hWnd_Button = FindWindowEx(hWnd, IntPtr.Zero, "Button", "OK");
149
+
150
+ textBox1.AppendText(Convert.ToString(hWnd_Edit.ToInt32(), 16) + "\r" + "\n");
151
+ textBox1.AppendText(Convert.ToString(hWnd_Admin.ToInt32(), 16) + "\r" + "\n");
152
+ textBox1.AppendText(Convert.ToString(hWnd_Button.ToInt32(), 16) + "\r" + "\n");
153
+
154
+ SendMessage(hWnd_Edit, WM_SETTEXT, 0, @"C:\CNC4.03\cnc.exe -verb runas");
155
+ System.Threading.Thread.Sleep(100);
156
+
157
+ SendMessage(hWnd_Admin, WM_LBUTTONDOWN, MK_LBUTTON, null);
158
+ SendMessage(hWnd_Admin, WM_LBUTTONUP, 0x00000000, null);
159
+ System.Threading.Thread.Sleep(100);
160
+
161
+ SendMessage(hWnd_Button, WM_LBUTTONDOWN, MK_LBUTTON, null);
162
+ SendMessage(hWnd_Button, WM_LBUTTONUP, 0x00000000, null);
163
+ }
164
+ else
165
+ {
166
+ MessageBox.Show("新しいタスクの作成ダイアログが見つかりませんでした。");
167
+ }
168
+ }
169
+ }
170
+ }
171
+ コード
172
+ ```