@@ -47,7 +47,7 @@ DWORD GetProcessIdFromExe(const std::wstring& exeName) {
47
47
// Create a snapshot of all running processes
48
48
HANDLE hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
49
49
if (hSnapshot == INVALID_HANDLE_VALUE) {
50
- printf ( " Error taking snapshot of processes." ) ;
50
+ std::cerr << " [ERROR]: Error taking snapshot of processes." << std::endl ;
51
51
return 0 ;
52
52
}
53
53
@@ -100,17 +100,17 @@ int send_auth_to_obs(std::string payload) {
100
100
sa.bInheritHandle = FALSE ;
101
101
102
102
int connect_attempts_remaining = 6 ;
103
-
103
+ std::cout << " Attempting to connect to Marketplace Connect Plugin " << std::flush;
104
104
while (connect_attempts_remaining-- > 0 &&
105
105
pipe == INVALID_HANDLE_VALUE) {
106
106
pipe_number = 0 ;
107
107
while (pipe_number < 10 ) {
108
108
attempt_name = base_name + std::to_string (pipe_number);
109
- printf ( " Attempting %s \n " , attempt_name. c_str ()) ;
109
+ std::cout << " . " << std::flush ;
110
110
pipe = CreateFileA (attempt_name.c_str (), GENERIC_WRITE,
111
111
0 , &sa, OPEN_EXISTING, 0 , NULL );
112
112
if (pipe != INVALID_HANDLE_VALUE) {
113
- printf ( " Success \n " ) ;
113
+ std::cout << " \n Success " << std::endl ;
114
114
break ;
115
115
}
116
116
pipe_number++;
@@ -120,22 +120,82 @@ int send_auth_to_obs(std::string payload) {
120
120
}
121
121
}
122
122
if (pipe == INVALID_HANDLE_VALUE) {
123
- printf ( " Could not open named pipe! " ) ;
123
+ std::cerr << " [ERROR] Could not find connection Marketplace Connect Plugin. " << std::endl ;
124
124
return 1 ;
125
125
}
126
126
DWORD mode = PIPE_READMODE_MESSAGE;
127
127
auto success = SetNamedPipeHandleState (pipe, &mode, NULL , NULL );
128
128
if (!success) {
129
129
CloseHandle (pipe);
130
- printf ( " Could not configure named pipe! " ) ;
130
+ std::cerr << " [ERROR] Could not negotiate connection with Marketplace Connect Plugin. " << std::endl ;
131
131
return 1 ;
132
132
}
133
133
134
134
DWORD written = 0 ;
135
135
success = WriteFile (pipe, payload.c_str (), static_cast <DWORD>(payload.size ()), &written,
136
136
NULL );
137
137
if (!success || written < payload.size ()) {
138
- printf (" Failed to write to named pipe!" );
138
+ std::cerr << " [ERROR] Could not send data to Marketplace Connect Plugin." << std::endl;
139
+ CloseHandle (pipe);
140
+ return 1 ;
141
+ }
142
+
143
+ CloseHandle (pipe);
144
+ return 0 ;
145
+ }
146
+
147
+ int open_obs_mp_window ()
148
+ {
149
+ int pipe_number = 0 ;
150
+ std::string pipe_name = " elgato_cloud" ;
151
+ std::string base_name = " \\\\ .\\ pipe\\ " + pipe_name;
152
+ std::string attempt_name;
153
+ HANDLE pipe = INVALID_HANDLE_VALUE;
154
+ SECURITY_ATTRIBUTES sa;
155
+ SECURITY_DESCRIPTOR sd;
156
+ InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
157
+ SetSecurityDescriptorDacl (&sd, TRUE , NULL , FALSE );
158
+ sa.nLength = sizeof (sa);
159
+ sa.lpSecurityDescriptor = &sd;
160
+ sa.bInheritHandle = FALSE ;
161
+
162
+ int connect_attempts_remaining = 60 ;
163
+
164
+ std::cout << " Waiting for OBS to launch" << std::flush;
165
+
166
+ while (connect_attempts_remaining-- > 0 &&
167
+ pipe == INVALID_HANDLE_VALUE) {
168
+ pipe_number = 0 ;
169
+ attempt_name = base_name + std::to_string (pipe_number);
170
+ std::cout << " ." << std::flush;
171
+ pipe = CreateFileA (attempt_name.c_str (), GENERIC_WRITE,
172
+ 0 , &sa, OPEN_EXISTING, 0 , NULL );
173
+ if (pipe != INVALID_HANDLE_VALUE) {
174
+ std::cout << " \n Connected To OBS" << std::endl;
175
+ break ;
176
+ }
177
+ if (pipe == INVALID_HANDLE_VALUE) {
178
+ Sleep (1000 );
179
+ }
180
+ }
181
+ if (pipe == INVALID_HANDLE_VALUE) {
182
+ std::cerr << " [ERROR] Could not find connection Marketplace Connect Plugin." << std::endl;
183
+ return 1 ;
184
+ }
185
+ DWORD mode = PIPE_READMODE_MESSAGE;
186
+ auto success = SetNamedPipeHandleState (pipe, &mode, NULL , NULL );
187
+ if (!success) {
188
+ CloseHandle (pipe);
189
+ std::cerr << " [ERROR] Could not negotiate connection with Marketplace Connect Plugin." << std::endl;
190
+ return 1 ;
191
+ }
192
+
193
+ DWORD written = 0 ;
194
+ std::string payload = " elgatolink://open" ;
195
+ success = WriteFile (pipe, payload.c_str (), static_cast <DWORD>(payload.size ()), &written,
196
+ NULL );
197
+ if (!success || written < payload.size ()) {
198
+ std::cerr << " [ERROR] Could not send data to Marketplace Connect Plugin." << std::endl;
139
199
CloseHandle (pipe);
140
200
return 1 ;
141
201
}
@@ -169,12 +229,12 @@ int launch_obs()
169
229
STARTUPINFOW si = { sizeof (STARTUPINFO) };
170
230
PROCESS_INFORMATION pi;
171
231
172
- printf (" Try create %ls with %ls\n " , launch_path.c_str (),
173
- wd.c_str ());
232
+ // printf("Try create %ls with %ls\n", launch_path.c_str(),
233
+ // wd.c_str());
174
234
175
235
// WCHAR lpCommandLine[] = L"";
176
236
if (CreateProcess (launch_path.c_str (), NULL , NULL , NULL , FALSE , 0 , NULL , wd.c_str (), &si, &pi) == 0 ) {
177
- printf ( " Failed to launch OBS." ) ;
237
+ std::cerr << " [ERROR] Failed to launch OBS." << std::endl ;
178
238
return 1 ;
179
239
}
180
240
@@ -185,7 +245,7 @@ int launch_obs()
185
245
} else if (running) {
186
246
DWORD processId = GetProcessIdFromExe (L" obs64.exe" );
187
247
if (processId == 0 ) {
188
- printf ( " Process not found! " ) ;
248
+ std::cerr << " [ERROR] OBS appears to have quit. " << std::endl ;
189
249
return 1 ;
190
250
}
191
251
EnumWindows (WindowToForeground, processId);
@@ -198,26 +258,31 @@ bool obs_is_running(std::wstring name) {
198
258
if (!iswalnum (name[i]))
199
259
name[i] = L' _' ;
200
260
}
201
-
202
- printf (" CHECKING %ls\n " , name.c_str ());
203
261
HANDLE h = OpenMutexW (SYNCHRONIZE, false , name.c_str ());
204
262
return !!h;
205
263
}
206
264
207
265
int main (int argc, char *argv[]) {
208
266
if (argc < 2 ) {
209
- printf (" Please provide an argument" );
267
+ std::cerr << " [ERROR] No argument provided." << std::endl;
268
+ system (" pause" );
210
269
return 1 ;
211
270
}
271
+ int resp;
212
272
213
273
std::string payload = argv[1 ];
214
274
215
275
if (payload.find (" elgatolink://auth" ) == 0 ) {
216
276
// Send auth to OBS which requested it.
217
- return send_auth_to_obs (payload);
277
+ resp = send_auth_to_obs (payload);
218
278
} else {
219
- return launch_obs ();
279
+ resp = launch_obs ();
280
+ if (resp == 0 ) resp = open_obs_mp_window ();
281
+ }
282
+
283
+ if (resp == 1 ) {
284
+ system (" pause" );
220
285
}
221
- printf ( " No obs found to launch. " );
222
- return 1 ;
286
+
287
+ return resp ;
223
288
}
0 commit comments