1
1
---
2
2
title : Sample C++ project for code analysis
3
- ms.date : 11/04/2016
3
+ description : " How to create a sample solution for use in the code analysis walkthrough for Microsoft C++ in Visual Studio."
4
+ ms.date : 04/14/2020
4
5
ms.topic : sample
5
6
helpviewer_keywords :
6
7
- " demo sample [Visual Studio ALM]"
@@ -11,45 +12,45 @@ ms.assetid: 09e1b9f7-5916-4ed6-a001-5c2d7e710682
11
12
12
13
This following procedures show you how to create the sample for [ Walkthrough: Analyze C/C++ code for defects] ( ../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md ) . The procedures create:
13
14
14
- - A Visual Studio solution named CppDemo.
15
+ - A Visual Studio solution named * CppDemo* .
15
16
16
- - A static library project named CodeDefects.
17
+ - A static library project named * CodeDefects* .
17
18
18
- - A static library project named Annotations.
19
+ - A static library project named * Annotations* .
19
20
20
21
The procedures also provide the code for the header and * .cpp* files for the static libraries.
21
22
22
23
## Create the CppDemo solution and the CodeDefects project
23
24
24
25
1 . Open Visual Studio and select ** Create a new project**
25
26
26
- 1 . Change language filter to ** C++**
27
+ 1 . In the ** Create a new project ** dialog, change the language filter to ** C++** .
27
28
28
- 1 . Select ** Empty Project ** and click ** Next**
29
+ 1 . Select ** Windows Desktop Wizard ** and choose the ** Next** button.
29
30
30
- 1 . In the ** Project Name ** text box, type ** CodeDefects**
31
+ 1 . On the ** Configure your new project ** page, in the ** Project name ** text box, enter ** CodeDefects** .
31
32
32
- 1 . In the ** Solution name** text box, type ** CppDemo**
33
+ 1 . In the ** Solution name** text box, enter * CppDemo* .
33
34
34
- 1 . Click ** Create**
35
+ 1 . Choose ** Create** .
35
36
36
- ## Configure the CodeDefects project as a static library
37
+ 1 . In the ** Windows Desktop Project ** dialog, change the ** Application type ** to ** Static Library (.lib) ** .
37
38
38
- 1 . In Solution Explorer, right-click ** CodeDefects ** and then click ** Properties ** .
39
+ 1 . Under ** Additional options ** , select ** Empty project ** .
39
40
40
- 1 . Expand ** Configuration Properties ** and then click ** General ** .
41
+ 1 . Choose ** OK ** to create the solution and project .
41
42
42
- 1 . In the ** General ** list, change ** Configuration Type ** , to ** Static library (.lib) ** .
43
+ ## Add the header and source file to the CodeDefects project
43
44
44
- 1 . In the ** Advanced ** list, change ** Target File Extension ** to ** .lib **
45
+ 1 . In Solution Explorer, expand ** CodeDefects ** .
45
46
46
- ## Add the header and source file to the CodeDefects project
47
+ 1 . Right-click to open the context menu for ** Header Files ** . Choose ** Add ** > ** New Item ** .
47
48
48
- 1 . In Solution Explorer, expand ** CodeDefects ** , right-click ** Header Files ** , click ** Add ** , and then click ** New Item ** .
49
+ 1 . In the ** Add New Item ** dialog box, select ** Visual C++ ** > ** Code ** , and then select ** Header File (.h) ** .
49
50
50
- 1 . In the ** Add New Item ** dialog box, click ** Code ** , and then click ** Header File (.h) ** .
51
+ 1 . In the ** Name ** edit box, enter * Bug.h * , and then choose the ** Add ** button .
51
52
52
- 1 . In the ** Name ** box, type ** Bug.h** and then click ** Add ** .
53
+ 1 . In the edit window for * Bug.h* , select and delete the contents .
53
54
54
55
1 . Copy the following code and paste it into the * Bug.h* file in the editor.
55
56
@@ -58,9 +59,8 @@ The procedures also provide the code for the header and *.cpp* files for the sta
58
59
59
60
#include < windows.h>
60
61
61
- // These functions are consumed by the sample
62
- // but are not defined. This project cannot be linked!
63
- bool CheckDomain (LPCTSTR);
62
+ // Function prototypes
63
+ bool CheckDomain (wchar_t const * );
64
64
HRESULT ReadUserAccount();
65
65
66
66
// These constants define the common sizes of the
@@ -69,24 +69,34 @@ The procedures also provide the code for the header and *.cpp* files for the sta
69
69
const int ACCOUNT_DOMAIN_LEN = 128;
70
70
```
71
71
72
- 1 . In Solution Explorer, right-click ** Source Files** , point to ** New ** , and then click ** New Item** .
72
+ 1 . In Solution Explorer, right-click to open the context menu for ** Source Files** . Choose ** Add ** > ** New Item** .
73
73
74
- 1 . In the ** Add New Item** dialog box, click ** C++ File (.cpp)**
74
+ 1 . In the ** Add New Item** dialog box, select ** C++ File (.cpp)** .
75
75
76
- 1 . In the ** Name** box, type ** Bug.cpp** and then click ** Add** .
76
+ 1 . In the ** Name** edit box, enter * Bug.cpp* , and then choose the ** Add** button .
77
77
78
78
1 . Copy the following code and paste it into the * Bug.cpp* file in the editor.
79
79
80
80
``` cpp
81
81
#include " Bug.h"
82
82
83
83
// the user account
84
- TCHAR g_userAccount[USER_ACCOUNT_LEN] = {};
84
+ wchar_t g_userAccount[USER_ACCOUNT_LEN] = { L"domain \\ user" };
85
85
int len = 0;
86
86
87
+ bool CheckDomain(wchar_t const* domain)
88
+ {
89
+ return (wcsnlen_s(domain, USER_ACCOUNT_LEN) > 0);
90
+ }
91
+
92
+ HRESULT ReadUserAccount()
93
+ {
94
+ return S_OK;
95
+ }
96
+
87
97
bool ProcessDomain()
88
98
{
89
- TCHAR * domain = new TCHAR [ACCOUNT_DOMAIN_LEN];
99
+ wchar_t * domain = new wchar_t [ACCOUNT_DOMAIN_LEN];
90
100
// ReadUserAccount gets a 'domain\user' input from
91
101
//the user into the global 'g_userAccount'
92
102
if (ReadUserAccount())
@@ -95,22 +105,22 @@ The procedures also provide the code for the header and *.cpp* files for the sta
95
105
// character onto the 'domain' buffer
96
106
for (len = 0; (len < ACCOUNT_DOMAIN_LEN) && (g_userAccount[len] != L'\0'); len++)
97
107
{
98
- if (g_userAccount[len] == '\\')
108
+ if (g_userAccount[len] == L '\\')
99
109
{
100
110
// Stops copying on the domain and user separator ('\')
101
111
break;
102
112
}
103
113
domain[len] = g_userAccount[len];
104
114
}
105
- if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
115
+ if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L '\\'))
106
116
{
107
117
// '\' was not found. Invalid domain\user string.
108
118
delete[] domain;
109
119
return false;
110
120
}
111
121
else
112
122
{
113
- domain[len] = '\0';
123
+ domain[len] = L '\0';
114
124
}
115
125
// Process domain string
116
126
bool result = CheckDomain(domain);
@@ -133,31 +143,33 @@ The procedures also provide the code for the header and *.cpp* files for the sta
133
143
}
134
144
```
135
145
136
- 1 . Click the ** File** menu, and then click ** Save All** .
146
+ 1 . On the menu bar, choose ** File** > ** Save All** .
137
147
138
148
## Add the Annotations project and configure it as a static library
139
149
140
- 1 . In Solution Explorer, click ** CppDemo** , point to ** Add** , and then click ** New Project** .
141
-
142
- 1 . In the ** Add a new project** dialog box, Change language filter to ** C++** and select ** Empty Project** then click ** Next** .
150
+ 1 . In Solution Explorer, right-click ** CppDemo** to open the context menu. Choose ** Add** > ** New Project** .
143
151
144
- 1 . In the ** Project name ** text box, type ** Annotations ** , and then click ** Create ** .
152
+ 1 . In the ** Add a new project ** dialog box, select ** Windows Desktop Wizard ** , and then choose the ** Next ** button .
145
153
146
- 1 . In Solution Explorer, right-click ** Annotations ** and then click ** Properties ** .
154
+ 1 . On the ** Configure your new project ** page, in the ** Project name ** text box, enter * Annotations * , and then choose ** Create ** .
147
155
148
- 1 . Expand ** Configuration Properties ** and then click ** General ** .
156
+ 1 . In the ** Windows Desktop Project ** dialog, change the ** Application type ** to ** Static Library (.lib) ** .
149
157
150
- 1 . In the ** General ** list, change ** Configuration Type ** , to and then click ** Static library (.lib) ** .
158
+ 1 . Under ** Additional options ** , select ** Empty project ** .
151
159
152
- 1 . In the ** Advanced ** list, select the text in the column next to ** Target File extension ** , and then type ** .lib ** .
160
+ 1 . Choose ** OK ** to create the project .
153
161
154
162
## Add the header file and source file to the Annotations project
155
163
156
- 1 . In Solution Explorer, expand ** Annotations** , right-click ** Header Files** , click ** Add** , and then click ** New Item** .
164
+ 1 . In Solution Explorer, expand ** Annotations** .
165
+
166
+ 1 . Right-click to open the context menu for ** Header Files** . Choose ** Add** > ** New Item** .
167
+
168
+ 1 . In the ** Add New Item** dialog box, select ** Visual C++** > ** Code** , and then select ** Header File (.h)** .
157
169
158
- 1 . In the ** Add New Item ** dialog box, click ** Header File (.h) ** .
170
+ 1 . In the ** Name ** edit box, enter * annotations.h * , and then choose the ** Add ** button .
159
171
160
- 1 . In the ** Name ** box, type ** annotations.h** and then click ** Add ** .
172
+ 1 . In the edit window for * annotations.h* , select and delete the contents .
161
173
162
174
1 . Copy the following code and paste it into the * annotations.h* file in the editor.
163
175
@@ -176,16 +188,23 @@ The procedures also provide the code for the header and *.cpp* files for the sta
176
188
_Ret_maybenull_ LinkedList* AllocateNode();
177
189
```
178
190
179
- 1 . In Solution Explorer, right-click ** Source Files** , point to ** New ** , and then click ** New Item** .
191
+ 1 . In Solution Explorer, right-click to open the context menu for ** Source Files** . Choose ** Add ** > ** New Item** .
180
192
181
- 1 . In the ** Add New Item** dialog box, click ** Code ** and then click ** C++ File (.cpp)**
193
+ 1 . In the ** Add New Item** dialog box, select ** C++ File (.cpp)** .
182
194
183
- 1 . In the ** Name** box, type ** annotations.cpp** and then click ** Add** .
195
+ 1 . In the ** Name** edit box, enter * annotations.cpp* , and then choose the ** Add** button .
184
196
185
197
1 . Copy the following code and paste it into the * annotations.cpp* file in the editor.
186
198
187
199
``` cpp
188
200
#include " annotations.h"
201
+ #include < malloc.h>
202
+
203
+ _Ret_maybenull_ LinkedList* AllocateNode ()
204
+ {
205
+ LinkedList* result = static_cast<LinkedList*>(malloc(sizeof(LinkedList)));
206
+ return result;
207
+ }
189
208
190
209
LinkedList* AddTail(LinkedList* node, int value)
191
210
{
@@ -205,6 +224,6 @@ The procedures also provide the code for the header and *.cpp* files for the sta
205
224
}
206
225
```
207
226
208
- 1 . Click the ** File** menu, and then click ** Save All** .
227
+ 1 . On the menu bar, choose ** File** > ** Save All** .
209
228
210
229
The solution is now complete and should build without errors.
0 commit comments