@@ -250,14 +250,35 @@ void init_chipids(char *dir_to_scan) {
250
250
HANDLE hFind = INVALID_HANDLE_VALUE ;
251
251
WIN32_FIND_DATAA ffd ;
252
252
char filepath [MAX_PATH ] = {0 };
253
+ DWORD filepathlen ;
254
+ int numslash ;
253
255
StringCchCopyA (filepath , STLINK_ARRAY_SIZE (filepath ), dir_to_scan );
254
256
255
- if ( FAILED (
256
- StringCchCatA ( filepath , STLINK_ARRAY_SIZE ( filepath ), "\\*.chip" )) ) {
257
- ELOG ("Path to chips's dir too long. \n" );
257
+ filepathlen = GetModuleFileNameA ( NULL , filepath , STLINK_ARRAY_SIZE ( filepath ));
258
+ if ( filepathlen == 0 ) {
259
+ ELOG ("GetModuleFileNameA failed: %u \n" , ( unsigned ) GetLastError () );
258
260
return ;
259
261
}
260
262
263
+ // Chop off exe and bin directory to get installation directory
264
+ // 'C:\path-to-stlink\bin\st-util.exe' -> 'C:\path-to-stlink'
265
+ numslash = 2 ;
266
+ while (filepathlen > 0 && numslash > 0 ) {
267
+ if (filepath [filepathlen - 1 ] == '\\' ) {
268
+ numslash -- ;
269
+ }
270
+ filepathlen -- ;
271
+ }
272
+ if (filepathlen <= 0 ) {
273
+ ELOG ("GetModuleFileNameA returned an invalid path: %s\n" , filepath );
274
+ return ;
275
+ }
276
+
277
+ filepath [filepathlen ] = '\0' ;
278
+ StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), "\\" );
279
+ StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), dir_to_scan );
280
+ StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), "\\*.chip" );
281
+
261
282
hFind = FindFirstFileA (filepath , & ffd );
262
283
263
284
if (INVALID_HANDLE_VALUE == hFind ) {
@@ -266,8 +287,9 @@ void init_chipids(char *dir_to_scan) {
266
287
}
267
288
268
289
do {
269
- memset (filepath , 0 , STLINK_ARRAY_SIZE (filepath ));
270
- StringCchCopyA (filepath , STLINK_ARRAY_SIZE (filepath ), dir_to_scan );
290
+ filepath [filepathlen ] = '\0' ;
291
+ StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), "\\" );
292
+ StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), dir_to_scan );
271
293
StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), "\\" );
272
294
StringCchCatA (filepath , STLINK_ARRAY_SIZE (filepath ), ffd .cFileName );
273
295
process_chipfile (filepath );
0 commit comments