-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGNUplot.cpp
More file actions
484 lines (448 loc) · 18.4 KB
/
GNUplot.cpp
File metadata and controls
484 lines (448 loc) · 18.4 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* requirements:
* gnuplot has to be installed (http://www.gnuplot.info/download.html)
* for Windows: set Path-Variable for Gnuplot path (e.g. C:/gnuplot/binary)
* or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path); */
#include "GNUplot.h"
#if defined(DEBUG) || defined (_DEBUG)
//include or whatever
#else if defined(NDEBUG)
//Do this
#endif
const TCHAR GNUPLOTEXE[] = TEXT("C:\\gnuplot\\bin\\gnuplot.exe");
/* CHAR * GNUPlotInterface::Init(...)
* This function is used to initialize a "real-time" plot. It sets the plot up
* for plotting data from individual values, as well as other formatting issues
* like the title name and line type, etc.
* "gnuplotCommandLine" specifies the entire initialization sequence. For
* clarification on command arguments see "gnuplot.pdf" in C:\Gnuplot\docs.
*/
std::string GNUPlotInterface::Init(const CHAR *gnuplotLINETYPE, const CHAR *gnuplotTITLE){
DWORD dwToWrite;
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
std::string gnuplotCommand;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
gnuplotCommand = "set term windows\n";
#else
/*This section only for non-windows terminal users.
* gnuplotCommand = "set term &&&&&" depending on your terminal type (i.e. "set
* term macintosh*/
//std::cout<<"Add definition for gnuplotCommand here. (depends on your terminal type)"<<std::endl;
//gnuplotCommand = "set term macintosh\n"; //Change 'macintosh' to your terminal type
#endif
gnuplotCommand.append("plot \"-\" with "); //"-" for realtime plotting
gnuplotCommand.append(gnuplotLINETYPE); //Linetype
gnuplotCommand.append(" title \"");
gnuplotCommand.append(gnuplotTITLE); gnuplotCommand.append("\"\n"); //Title
gnuplotCommand.append("1 1\n2 2\ne\n"); //Some initial plot (So we don't get warning messages)
dwToWrite = gnuplotCommand.length();
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommand.c_str(), dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Init StdIN write"));
/* Read reply from gnuplot.exe (Should be none with proper command line) */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if ( !bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Init StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString;
}
/* GNUPlotInterface::Write(...)
* This is used to send specific commands AS-IS to gnuplot. Examples include
* setting the xrange, yrange or other "set" commands. Make sure to include the
* newline character \n at the end of this command if required.
* "gnuplotCommandLine" specifies the arguments to be sent to gnuplot.exe
*/
std::string GNUPlotInterface::Write(const CHAR *gnuplotCommandLine){
DWORD dwToWrite = (DWORD) strlen(gnuplotCommandLine);
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommandLine, dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Plot(...,1) StdIN write"));
/* Now read the reply from gnuplot.exe */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if(!bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Plot(...) StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString; // Return the char array sent from gnuplot.exe
}
/* GNUPlotInterface::Plot(...)
* To be used only with "Real-time" plotting of data (individual points)
* This is the first (with minimum overhead) of the overloaded Plot(...)
* functions.
* "gnuplotCommandLine" specifies the arguments to be sent to gnuplot.exe.
* "commandArg" specifies whether certain monotonous necessary arguments were
* ommitted from commandLine. Typical arguments that might have been ommitted
* include "replot" or the "e" character. Currently commandArg only supports
* value of 0.
*/
std::string GNUPlotInterface::Plot(const CHAR *gnuplotCommandLine, int commandArg){
DWORD dwToWrite;
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
if(commandArg == 0){
/* Only the plot points are supplied in commandLine, so add "replot" and "e"
* to the commandLine string (prepend "replot", append "e") */
std::string gnuplotCommand = "replot\n";
gnuplotCommand.append(gnuplotCommandLine);
gnuplotCommand.append("e\n");
dwToWrite = gnuplotCommand.length();
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommand.c_str(), dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Plot(...,1) StdIN write"));
}
else{
SetLastError(ERROR_INVALID_DATA);
ErrorExit(TEXT("GNUPlotInterface::Plot(...,#) commandArg"));
}
/* Now read the reply from gnuplot.exe */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if(!bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Plot(...) StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString; // Return the char array sent from gnuplot.exe
}
/* GNUPlotInterface::Plot(...) [overloaded]
* To be used only with "Real-time" plotting of data (individual points)
* This is the second of the overloaded Plot(...) functions.
* "data" specifies the data points to be sent to gnuplot.exe. The data should
* be organized as follows:
* data[0][0] = x[0], data[1][0] = y[0];
* data[0][1] = x[1], data[1][1] = y[1];
* data[0][2] = x[2], data[1][2] = y[2];
* "dims" specifies how many dimensions in the data array (currently only 1 or 2
* dimensions are supported)
* "length" specifies how long the data array is (i.e. how many data points)
*/
std::string GNUPlotInterface::Plot(std::vector<double> data[], int dims, int length){
DWORD dwToWrite;
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
std::string gnuplotCommand = "replot\n";
CHAR doubleVal[20];
if(dims == 1)
for(int i = 0; i<length; i++){
sprintf_s(doubleVal,"%.5f",data[0][i]);
gnuplotCommand.append(doubleVal); //y-value
gnuplotCommand.append("\n");
}
else if(dims == 2)
for(int i = 0; i<length; i++){
sprintf_s(doubleVal,"%.5f",data[0][i]); //x-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append(" ");
sprintf_s(doubleVal,"%.5f",data[1][i]); //y-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append("\n");
}
else{
SetLastError(ERROR_INVALID_DATA);
ErrorExit(TEXT("GNUPlotInterface::Plot(...,#,...) Invalid #"));
}
gnuplotCommand.append("e\n");
dwToWrite = gnuplotCommand.length();
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommand.c_str(), dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdIN write"));
/* Now read the reply from gnuplot.exe */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if(!bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString;
}
/* GNUPlotInterface::Plot(...) [overloaded]
* To be used only with "Real-time" plotting of data (individual points)
* This is the third of the overloaded Plot(...) functions and can be used
* in a way very familiar to Matlab plotting.
* "xValues" specifies the x array of values for plotting.
* "yValues" specifies the y array of values for plotting.
* "length" specifies how long the data arrays are (i.e. how many data points)
*/
std::string GNUPlotInterface::Plot(double xValues[], double yValues[], int length){
DWORD dwToWrite;
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
std::string gnuplotCommand = "replot\n";
CHAR doubleVal[20];
for(int i = 0; i<length; i++){
sprintf_s(doubleVal,20,"%.5f",xValues[i]); //x-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append(" ");
sprintf_s(doubleVal,20,"%.5f",yValues[i]); //y-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append("\n");
}
gnuplotCommand.append("e\n");
dwToWrite = gnuplotCommand.length();
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommand.c_str(), dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdIN write"));
/* Now read the reply from gnuplot.exe */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if(!bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString;
}
/* void GNUPlotInterface::Plot(...)
* To be used only with "Real-time" plotting of data (individual points)
* This is the fourth of the overloaded Plot(...) functions and can be used
* in a way very familiar to Matlab plotting. Only the y values are specified;
* the x values are assigned based on the index of each y value (like Matlab)
* "yValues" specifies the y array of values for plotting.
* "length" specifies how long the data arrays are (i.e. how many data points)
*/
std::string GNUPlotInterface::Plot(double yValues[], int length){
DWORD dwToWrite;
DWORD dwWritten = 0;
BOOL bSuccess = FALSE;
std::string gnuplotCommand = "replot\n";
CHAR doubleVal[20];
for(int i = 0; i<length; i++){
sprintf_s(doubleVal,20,"%d",i); //x-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append(" ");
sprintf_s(doubleVal,20,"%.5f",yValues[i]); //y-value
gnuplotCommand.append(doubleVal); gnuplotCommand.append("\n");
}
gnuplotCommand.append("e\n");
dwToWrite = gnuplotCommand.length();
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotCommand.c_str(), dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdIN write"));
/* Now read the reply from gnuplot.exe */
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd,chBuf,dwToRead,&dwRead,NULL);
if(!bSuccess)
ErrorExit(TEXT("In GNUPlotInterface::Plot(data) StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
std::string returnString(chBuf);
return returnString;
}
/* void GNUPlotInterface::CreatePipe()
* This function simply creates the pipes used to interface with gnuplot.exe.
* It MUST be called before StartProcess(), otherwise StartProcess() will not
* have initialized pipe handles to call gnuplot.exe with, and an error will
* occur.
*/
void GNUPlotInterface::CreatePipes(void)
{
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
/* Create a pipe for the child process's STDIN. */
if (! CreatePipe(&m_hGNUPlotStd_IN_Rd, &m_hGNUPlotStd_IN_Wr, &saAttr, 0))
ErrorExit(TEXT("Stdin CreatePipe"));
// Ensure the write handle to the pipe for STDIN is not inherited.
if ( ! SetHandleInformation(m_hGNUPlotStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
ErrorExit(TEXT("Stdin SetHandleInformation"));
/* Create a pipe for the child process's STDOUT. */
if (! CreatePipe(&m_hGNUPlotStd_OUT_Rd, &m_hGNUPlotStd_OUT_Wr, &saAttr, 0))
ErrorExit(TEXT("Stdout CreatePipe"));
// Ensure the read handle to the pipe for STDOUT is not inherited.
if ( ! SetHandleInformation(m_hGNUPlotStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
ErrorExit(TEXT("Stdin SetHandleInformation"));
}
/* void GNUPlotInterface::StartProcess()
* This function starts the gnuplot.exe process and sets the appropriate pipe
* handles for communication. The class member variable m_ProcInfo is returned
* containing a handle to the process, and the thread it is contained in. The
* path to gnuplot.exe is assumed to be "C:\Gnuplot\binary", if it is not, then
* modify the "const TCHAR *GNUPLOT_EXE" declaration.
*/
void GNUPlotInterface::StartProcess(void)
// Create a child process that uses the previously created pipes for STDIN and STDOUT.
{
const TCHAR *szProcessName = GNUPLOTEXE;
STARTUPINFO StartInfo;
DWORD dwCreationFlags;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &m_ProcInfo, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.
ZeroMemory( &StartInfo, sizeof(STARTUPINFO) );
StartInfo.cb = sizeof(STARTUPINFO);
StartInfo.dwX = 0; //Starting x-position in pixels
StartInfo.dwY = 0; //Starting y-position in pixels
StartInfo.dwXSize = 800; //Starting width in pixels
StartInfo.dwYSize = 800; //Starting height in pixels
StartInfo.hStdError = m_hGNUPlotStd_OUT_Wr;
StartInfo.hStdOutput = m_hGNUPlotStd_OUT_Wr;
StartInfo.hStdInput = m_hGNUPlotStd_IN_Rd;
StartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USEPOSITION |
STARTF_USESIZE;
/* Initialize the Creation Flags variable with the appropriate flags set
* The DETACHED_PROCESS flag prevents the gnuplot window from forcing it's way
* to the front when the command line becomes active. */
dwCreationFlags = DETACHED_PROCESS;
// Create the child process.
if(!CreateProcess(szProcessName,
NULL, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
dwCreationFlags, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&StartInfo, // STARTUPINFO pointer
&m_ProcInfo /* receives PROCESS_INFORMATION */))
{
// If an error occurs, exit the application.
ErrorExit(TEXT("CreateProcess"));
}
}
/* GNUPlotInterface::CloseAll()
* A simple cleanup function.
* This function closes all the relevant pipe, process and thread handles.
*/
std::string GNUPlotInterface::CloseAll(void)
{
/* Write a quit command to gnuplot.exe */
CHAR *gnuplotQuitCommand = "quit\n";
DWORD dwToWrite = strlen(gnuplotQuitCommand);
DWORD dwWritten = 0;
DWORD dwToRead = BUFSIZE;
DWORD dwRead = 0;
DWORD totalBytesAvail;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
bSuccess = WriteFile(m_hGNUPlotStd_IN_Wr, gnuplotQuitCommand, dwToWrite, &dwWritten, NULL);
if ( !bSuccess || dwWritten != dwToWrite )
ErrorExit(TEXT("In GNUPlotInterface::CloseAll StdIN write"));
/* Peek the pipe to see if data is available to be read. If no data available
* then don't try and read (i.e. prevent ReadFile from blocking by not calling
* it). */
PeekNamedPipe(m_hGNUPlotStd_OUT_Rd,NULL,BUFSIZE,NULL,&totalBytesAvail,NULL);
if(totalBytesAvail>0){
bSuccess = ReadFile(m_hGNUPlotStd_OUT_Rd, chBuf, dwToRead, &dwRead,NULL);
if ( !bSuccess )
ErrorExit(TEXT("In GNUPlotInterface::CloseAll StdOUT read"));
chBuf[dwRead] = '\0';
}
else
chBuf[0] = '\0';
/* Close the handle to the process and thread */
CloseHandle(m_ProcInfo.hProcess);
CloseHandle(m_ProcInfo.hThread);
/* Close the child process read-pipe handle */
if ( ! CloseHandle(m_hGNUPlotStd_IN_Rd) )
ErrorExit(TEXT("StdInWr CloseHandle"));
/* Close the parent process write-pipe handle */
if ( ! CloseHandle(m_hGNUPlotStd_IN_Wr) )
ErrorExit(TEXT("StdInWr CloseHandle"));
/* Close the parent process read-pipe handle */
if ( ! CloseHandle(m_hGNUPlotStd_OUT_Rd) )
ErrorExit(TEXT("StdInWr CloseHandle"));
/* Close the child process write-pipe handle */
if ( ! CloseHandle(m_hGNUPlotStd_OUT_Wr) )
ErrorExit(TEXT("StdInWr CloseHandle"));
std::string returnString(chBuf);
return returnString; // Return the char array sent from gnuplot.exe (reply to "quit")
}
/* GNUPlotInterface::ErrorExit(PTSTR lpszFunction)
* This function was taken directly from:
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx
* It is an error handling function which displays error messages in a message
* box, with error codes.
*/
void GNUPlotInterface::ErrorExit(PTSTR lpszFunction)
// Format a readable error message, display a message box,
// and exit from the application.
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(1);
}