Skip to content
Tcl Source Code Changes To Migrating C extensions to Tcl 9
Login

Changes To Migrating C extensions to Tcl 9

Changes to "Migrating C extensions to Tcl 9" between 2025-04-25 16:45:25 and 2025-04-25 17:23:39

1
2
3
4
5
6
7
8
9


1
2
3
4
5
6
7
-
-







(Work in progress...)

Also see [Porting extensions to Tcl 9](https://wiki.tcl-lang.org/page/Porting+extensions+to+Tcl+9)
for another post on the topic.

## Init stubs and package require with open version

The list of versions should include TCL 9. Here is an example for TCL 8.5 up to any TCL 9 version.
Note that the upper bound "10" is exclusive.
︙
318
319
320
321
322
323
324



















































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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

to

```
ZbarAsyncDecodeObjCmd(ClientData clientData, Tcl_Interp *interp, int objc,  Tcl_Obj *const objv[])
```

### Deprecated of MS-Windows TCHAR utility functions 

The MS-Windows string character type `TCHAR` is 8 bit or 16 bit, depending on preprocessor define `_UNICODE`.
This functionality is outdated today and only 16 bit string types should be used to call the MS-Windows API.

TCL 8.6 had two functions to support this, which are still present but deprecated and not documented any more.
In consequence, the following code should be replaced (with `_UNICODE` defined):

```
Tcl_Size lStr;
char *pStr;
Tcl_DString *dStr;
pStr = Tcl_GetStringFromObj(objv[PositionSPar],&lStr);
// Old
Tcl_WinUtfToTChar(pStr,lStr,dStr);
// New
Tcl_DStringInit( &sPar1 );
Tcl_UtfToWCharDString( pStr, lStr, &sPar1);
// both solutions
TextOutW(pdlg.hDC, X0, Y0, Tcl_DStringValue( &dPar1 ),
        Tcl_DStringLength(&dPar1 )/sizeof(WCHAR));
```

Remark that the new function does not initialize the Dstring, but appends to it.

And the other way around (get the default printer name):

```
DWORD BufferSize;
TCHAR *pDefaultPrinter;
Tcl_DString Printer;
GetDefaultPrinter(NULL, &BufferSize);
if (BufferSize == 0) {return NULL;}
pDefaultPrinter = (TCHAR *) Tcl_Alloc(BufferSize * sizeof(TCHAR));
GetDefaultPrinter(pDefaultPrinter, &BufferSize);
// Old
Tcl_WinTCharToUtf(pDefaultPrinter, -1, &Printer);
// New
Tcl_DStringInit(&Printer);
Tcl_WCharToUtfDString(pDefaultPrinter, -1, &Printer);
// both
Tcl_Free((char *)pDefaultPrinter);
Tcl_DStringResult(interp, &Printer);
return RET_OK;
```

Remark the following difference of the old and the new function:

   *   The DString is not initialized allowing to append
   *   The size argument is in wide characters, not in bytes (e.g. `/ sizeof(WCHAR)`)
   *   Following the documentation, the argument `-1` was formerly not allowed for the size to read until the 0 word. This is not my observation (as seen in the upper code).