# 64bit Compilation

twinBASIC can compile native 64bit executables in addition to 32bit. The syntax is compatible with VBA7 for this: the `LongPtr` data type and the standard to mark APIs `PtrSafe`.

Using the [Fusion](/Features/Fusion.md) feature, it is also possible to use both 32bit and 64bit ActiveX controls in 32bit *and* 64bit projects.

## Building a 64-bit Version
{: index="switch to 32-bit" }

The [build configuration](/tB/IDE/Reference/Toolbar.md#build-configuration) box on the toolbar chooses between a 32-bit and a 64-bit build: **win32** or **win64**. <kbd>Ctrl</kbd>+<kbd>F1</kbd> switches to **win64**, and <kbd>Ctrl</kbd>+<kbd>F2</kbd> switches back to **win32**. The IDE remembers the choice for each project.

The two builds write different files. The default [Build Output Path](/tB/IDE/Project-Settings.md#build-output-path) ends in `${ProjectName}_${Architecture}.${FileExtension}`, and `${Architecture}` is `win32` or `win64`. So a Standard DLL project named `MathGreetLib` builds `Build\MathGreetLib_win32.dll` in one mode and `Build\MathGreetLib_win64.dll` in the other. The variable `${Bitness}` is `32` or `64` for the same two modes, for a path that names only the number.

## Example Syntax

```tb check_build
Public Declare PtrSafe Sub foo Lib "bar" (ByVal hWnd As LongPtr)
```

## Important Considerations

> [!IMPORTANT]
> There is a lot more required to get most 32bit apps to work properly as 64bit. Only some `Long` variables are to be changed, and this is determined by their C/C++ data types, of which there are many. Examples that need to be `LongPtr` include handles like `HWND, HBITMAP, HICON,` and `HANDLE`; pointers like `void*, PVOID, ULONG_PTR, DWORD_PTR,` and `LPWSTR/PWSTR/LPCWSTR/WCHAR*` when passed as `Long`; and the `SIZE_T` type found in CopyMemory and memory allocation functions.

While the `PtrSafe` keyword is not mandatory, these changes still must be made. Additionally, any code working with memory pointers must account for the fact all the types mentioned (and the many more not), as well as v-table entries, are now either 4 or 8 bytes, when most programmers have traditionally hard coded 4 bytes. There are also UDT alignment issues more frequently. This is all very complex and you should seek resources and advice when moving to 64bit (though remember, 32bit is still supported so this isn't a requirement).

For common Windows APIs and COM interfaces, a community-developed package is available that provides 64bit compatible definitions: [Windows Development Library for twinBASIC (WinDevLib)](https://github.com/fafalone/WinDevLib).
