├── .editorconfig ├── .github └── workflows │ └── vs2022.yml ├── .vs ├── base-console.vcxproj ├── base-console.vcxproj.filters └── base-console.vcxproj.user ├── LICENSE.txt ├── README.md ├── base-console.sln ├── rename.sh └── src ├── base-console.c ├── msapi_utf8.h └── version.rc /.editorconfig: -------------------------------------------------------------------------------- 1 | # indicate this is the root of the project 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | indent_size = 4 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | charset = utf-8 10 | -------------------------------------------------------------------------------- /.github/workflows/vs2022.yml: -------------------------------------------------------------------------------- 1 | name: VS2022 2 | 3 | on: [push, pull_request] 4 | 5 | env: 6 | FRIENDLY_NAME: Base Console 7 | CONFIGURATION: Release 8 | 9 | jobs: 10 | VS2022-Build: 11 | runs-on: windows-latest 12 | 13 | strategy: 14 | matrix: 15 | TARGET_PLATFORM: [x64, x86, arm64] 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v4 20 | 21 | - name: Add MSBuild to PATH 22 | uses: microsoft/setup-msbuild@v2 23 | with: 24 | msbuild-architecture: x64 25 | 26 | - name: Set version 27 | shell: bash 28 | id: set_version 29 | run: | 30 | git_tag=$(git describe --tags --abbrev=0 || echo v0.0) 31 | echo "version=$git_tag" >> $GITHUB_OUTPUT 32 | 33 | - name: Build 34 | shell: pwsh 35 | run: | 36 | # Build the GitHub Actions log URL for the current build 37 | $BUILD_URL="$Env:GITHUB_SERVER_URL/$Env:GITHUB_REPOSITORY/actions/runs/$Env:GITHUB_RUN_ID" 38 | # Build a version field for VERSION_INFO, by: 39 | # - Removing anything that's not a digit or a dot, and replacing dots by commas ("v2.3" -> "2,3") 40 | $FILE_VERSION="${{ steps.set_version.outputs.version }}" -replace '[^0-9.]' -replace '\.',',' 41 | # - Appending ",0" so that we end up with 4 sequences ("2,3" -> "2,3,0,0") 42 | while (($FILE_VERSION.ToCharArray() -eq ',').count -lt 3) { $FILE_VERSION = $FILE_VERSION + ",0" } 43 | # - Replacing commas with "%2c" since /p chokes on them even if the value is quoted ("2,3,0,0" -> "2%2c3%2c0%2c0") 44 | $FILE_VERSION=$FILE_VERSION -replace ',','%2c' 45 | # We'll use the VERSION_INFO 'Comments' field to insert some JSON data pertaining to this specific build 46 | # Note that we use JSON5 here, on account that RC.exe removes ALL doubles quotes from VERSION_INFO fields 47 | $JSON=@" 48 | { 49 | "Title": "Build Descriptor", 50 | "Version": 1.0, 51 | "Build": { 52 | "Facility": "GitHub Actions", 53 | "Url": "$BUILD_URL", 54 | "Repo": { 55 | "Branch": "$Env:GITHUB_REF_NAME", 56 | "Commit": "$Env:GITHUB_SHA", 57 | "License": "GPLv3+", 58 | "Name": "$Env:GITHUB_REPOSITORY", 59 | "Provider": "$Env:GITHUB_SERVER_URL", 60 | "Scm": "git" 61 | } 62 | } 63 | } 64 | "@ 65 | $JSON=$JSON -replace '\n','' -replace '\s+',' ' -replace ',','%2c' -replace '"',"'" 66 | msbuild ${{ github.event.repository.name }}.sln /m /p:Configuration=${{ env.CONFIGURATION }} /p:Platform=,Platform=${{ matrix.TARGET_PLATFORM }} /p:AppVersion=${{ steps.set_version.outputs.version }} /p:AppFileVersion="$FILE_VERSION" /p:AppComments="$JSON" 67 | 68 | - name: Display SHA-256 69 | run: sha256sum ./${{ matrix.TARGET_PLATFORM }}/${{ env.CONFIGURATION }}/*.exe 70 | 71 | - name: Upload artifacts 72 | uses: actions/upload-artifact@v4 73 | if: ${{ github.event_name == 'push' }} 74 | with: 75 | name: ${{ matrix.TARGET_PLATFORM }} 76 | path: ./${{ matrix.TARGET_PLATFORM }}/${{ env.CONFIGURATION }}/*.exe 77 | 78 | - name: Create release archive 79 | if: startsWith(github.ref, 'refs/tags/') 80 | run: 7z a ${{ github.event.repository.name }}_${{ matrix.TARGET_PLATFORM }}.zip ./${{ matrix.TARGET_PLATFORM }}/${{ env.CONFIGURATION }}/*.exe README.md LICENSE.txt 81 | 82 | - name: Upload release 83 | uses: softprops/action-gh-release@v1 84 | if: startsWith(github.ref, 'refs/tags/') 85 | with: 86 | token: ${{ secrets.GITHUB_TOKEN }} 87 | body: ${{ env.FRIENDLY_NAME}} ${{ steps.set_version.outputs.version }} 88 | files: ./*.zip 89 | -------------------------------------------------------------------------------- /.vs/base-console.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | ARM64 7 | 8 | 9 | Debug 10 | Win32 11 | 12 | 13 | Release 14 | ARM64 15 | 16 | 17 | Release 18 | Win32 19 | 20 | 21 | Debug 22 | x64 23 | 24 | 25 | Release 26 | x64 27 | 28 | 29 | 30 | 16.0 31 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232} 32 | Win32Proj 33 | baseconsole 34 | 35 | 36 | 37 | Application 38 | true 39 | Unicode 40 | v143 41 | 42 | 43 | Application 44 | false 45 | true 46 | Unicode 47 | v143 48 | 49 | 50 | Application 51 | true 52 | Unicode 53 | v143 54 | 55 | 56 | Application 57 | true 58 | Unicode 59 | v143 60 | 61 | 62 | Application 63 | false 64 | true 65 | Unicode 66 | v143 67 | 68 | 69 | Application 70 | false 71 | true 72 | Unicode 73 | v143 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 | <_ProjectFileVersion>10.0.30319.1 100 | $(SolutionDir)arm64\$(Configuration)\ 101 | $(SolutionDir)arm64\$(Configuration)\$(ProjectName)\ 102 | $(SolutionDir)arm64\$(Configuration)\ 103 | $(SolutionDir)arm64\$(Configuration)\$(ProjectName)\ 104 | $(SolutionDir)x86\$(Configuration)\ 105 | $(SolutionDir)x86\$(Configuration)\$(ProjectName)\ 106 | $(SolutionDir)x86\$(Configuration)\ 107 | $(SolutionDir)x86\$(Configuration)\$(ProjectName)\ 108 | $(SolutionDir)x64\$(Configuration)\ 109 | $(SolutionDir)x64\$(Configuration)\$(ProjectName)\ 110 | $(SolutionDir)x64\$(Configuration)\ 111 | $(SolutionDir)x64\$(Configuration)\$(ProjectName)\ 112 | false 113 | false 114 | false 115 | false 116 | false 117 | false 118 | 119 | 120 | true 121 | 122 | 123 | true 124 | 125 | 126 | true 127 | 128 | 129 | false 130 | 131 | 132 | false 133 | 134 | 135 | false 136 | 137 | 138 | 139 | /DAPP_VERSION=$(AppVersion) %(AdditionalOptions) 140 | 141 | 142 | 143 | 144 | /DAPP_FILE_VERSION=$(AppFileVersion) %(AdditionalOptions) 145 | 146 | 147 | 148 | 149 | /DAPP_COMMENTS="$(AppComments)" %(AdditionalOptions) 150 | 151 | 152 | 153 | 154 | Level3 155 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 156 | false 157 | CompileAsC 158 | MultiThreadedDebug 159 | 160 | 161 | Console 162 | true 163 | 164 | 165 | 166 | 167 | Level3 168 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 169 | false 170 | CompileAsC 171 | MultiThreadedDebug 172 | 173 | 174 | Console 175 | true 176 | 177 | 178 | 179 | 180 | Level3 181 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 182 | false 183 | CompileAsC 184 | MultiThreadedDebug 185 | 186 | 187 | Console 188 | true 189 | 190 | 191 | 192 | 193 | Level3 194 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 195 | false 196 | CompileAsC 197 | MultiThreaded 198 | 199 | 200 | Console 201 | true 202 | true 203 | true 204 | 205 | 206 | 207 | 208 | Level3 209 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 210 | false 211 | CompileAsC 212 | MultiThreaded 213 | 214 | 215 | Console 216 | true 217 | true 218 | true 219 | 220 | 221 | 222 | 223 | Level3 224 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 225 | false 226 | CompileAsC 227 | MultiThreaded 228 | 229 | 230 | Console 231 | true 232 | true 233 | true 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | -------------------------------------------------------------------------------- /.vs/base-console.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {9C680EF7-A004-47B8-9CDA-6BCFC312DBCB} 14 | rc 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | 28 | 29 | Resources 30 | 31 | 32 | -------------------------------------------------------------------------------- /.vs/base-console.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | base-console: A win32 console application template 2 | ================================================== 3 | 4 | [![Build status](https://img.shields.io/github/actions/workflow/status/pbatard/base-console/vs2022.yml?style=flat-square)](https://github.com/pbatard/base_console/actions/workflows/vs2022.yml) 5 | [![Release](https://img.shields.io/github/release-pre/pbatard/base-console.svg?style=flat-square)](https://github.com/pbatard/base-console/releases) 6 | [![Github stats](https://img.shields.io/github/downloads/pbatard/base-console/total.svg?style=flat-square)](https://github.com/pbatard/base-console/releases) 7 | [![Licence](https://img.shields.io/badge/license-GPLv3-blue.svg?style=flat-square)](https://www.gnu.org/licenses/gpl-3.0.en.html) 8 | 9 | Because sometimes I have to create a win32 console application in a hurry and the Visual Studio 10 | defaults are hopeless. 11 | -------------------------------------------------------------------------------- /base-console.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29926.136 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "base-console", ".vs\base-console.vcxproj", "{6C2BED99-5A0A-42A2-AEBE-66717FA92232}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|arm64 = Debug|arm64 11 | Debug|x64 = Debug|x64 12 | Debug|x86 = Debug|x86 13 | Release|arm64 = Release|arm64 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|arm64.ActiveCfg = Debug|ARM64 19 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|arm64.Build.0 = Debug|ARM64 20 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x64.ActiveCfg = Debug|x64 21 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x64.Build.0 = Debug|x64 22 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x86.ActiveCfg = Debug|Win32 23 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x86.Build.0 = Debug|Win32 24 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|arm64.ActiveCfg = Release|ARM64 25 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|arm64.Build.0 = Release|ARM64 26 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x64.ActiveCfg = Release|x64 27 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x64.Build.0 = Release|x64 28 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x86.ActiveCfg = Release|Win32 29 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x86.Build.0 = Release|Win32 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {7994FC0C-9D7C-4AD7-855B-C3525FD8709A} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /rename.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | type -P sed &>/dev/null || { echo "sed command not found. Aborting." >&2; exit 1; } 4 | type -P git &>/dev/null || { echo "git command not found. Aborting." >&2; exit 1; } 5 | 6 | if [ ! -n "$1" ]; then 7 | echo "You must provide the new project name (eg. 'my-project')" 8 | exit 1 9 | else 10 | NEW=$1 11 | fi 12 | 13 | if [[ $new =~ "_" ]]; then 14 | echo "A project names with underscore(s) is not suitable for GitHub" 15 | exit 1 16 | fi 17 | 18 | ORG=$(ls *.sln) 19 | ORG="${ORG%.*}" 20 | echo "Renaming '$ORG' to '$NEW'..." 21 | 22 | sed -b -i "s/$ORG/$NEW/g" $ORG.sln README.md .vs/*.vcxproj* src/*.c 23 | git mv $ORG.sln $NEW.sln 24 | git mv .vs/$ORG.vcxproj .vs/$NEW.vcxproj 25 | git mv .vs/$ORG.vcxproj.filters .vs/$NEW.vcxproj.filters 26 | git mv .vs/$ORG.vcxproj.user .vs/$NEW.vcxproj.user 27 | git mv src/$ORG.c src/$NEW.c 28 | # Delete existing tags 29 | git tag | xargs git tag -d 30 | 31 | echo "Do not forget to change FRIENDLY_NAME in vs2022.yml and update README.md." 32 | -------------------------------------------------------------------------------- /src/base-console.c: -------------------------------------------------------------------------------- 1 | /* 2 | * base-console - Because sometimes I want to release a win32 console 3 | * utility in a hurry, and I like to have it set up properly. 4 | * 5 | * Copyright © 2020-2024 Pete Batard 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifdef _DEBUG 22 | #define _CRTDBG_MAP_ALLOC 23 | #include 24 | #include 25 | #endif 26 | 27 | #define WIN32_LEAN_AND_MEAN 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "msapi_utf8.h" 35 | 36 | #define _STRINGIFY(x) #x 37 | #define STRINGIFY(x) _STRINGIFY(x) 38 | 39 | #ifndef APP_VERSION 40 | #define APP_VERSION_STR "[DEV]" 41 | #else 42 | #define APP_VERSION_STR STRINGIFY(APP_VERSION) 43 | #endif 44 | 45 | static __inline char* appname(const char* path) 46 | { 47 | static char appname[128]; 48 | _splitpath_s(path, NULL, 0, NULL, 0, appname, sizeof(appname), NULL, 0); 49 | return appname; 50 | } 51 | 52 | int main_utf8(int argc, char** argv) 53 | { 54 | fprintf(stderr, "%s %s © 2020-2024 Pete Batard \n\n", appname(argv[0]), APP_VERSION_STR); 55 | fprintf(stderr, "This program is free software; you can redistribute it and/or modify it under \n"); 56 | fprintf(stderr, "the terms of the GNU General Public License as published by the Free Software \n"); 57 | fprintf(stderr, "Foundation; either version 3 of the License or any later version.\n\n"); 58 | fprintf(stderr, "Official project and latest downloads at: https://github.com/pbatard/base-console\n\n"); 59 | 60 | fprintf(stdout, "Hello world!\n"); 61 | 62 | return 0; 63 | } 64 | 65 | int wmain(int argc, wchar_t** argv16) 66 | { 67 | SetConsoleOutputCP(CP_UTF8); 68 | char** argv = calloc(argc, sizeof(char*)); 69 | if (argv == NULL) 70 | return -1; 71 | for (int i = 0; i < argc; i++) 72 | argv[i] = wchar_to_utf8(argv16[i]); 73 | int r = main_utf8(argc, argv); 74 | for (int i = 0; i < argc; i++) 75 | free(argv[i]); 76 | free(argv); 77 | #ifdef _DEBUG 78 | _CrtDumpMemoryLeaks(); 79 | #endif 80 | return r; 81 | } 82 | -------------------------------------------------------------------------------- /src/msapi_utf8.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MSAPI_UTF8: Common API calls using UTF-8 strings 3 | * Compensating for what Microsoft should have done a long long time ago, that they 4 | * ONLY started to do in mid-2019 (What the £%^& took them so long?!?), as per: 5 | * https://docs.microsoft.com/en-us/windows/uwp/design/globalizing/use-utf8-code-page 6 | * 7 | * See also: https://utf8everywhere.org 8 | * 9 | * Copyright © 2010-2023 Pete Batard 10 | * 11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 3 of the License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #pragma once 43 | #if defined(_MSC_VER) 44 | // disable VS2012 Code Analysis warnings that are intentional 45 | #pragma warning(disable: 6387) // Don't care about bad params 46 | #endif 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #define _LTEXT(txt) L##txt 53 | #define LTEXT(txt) _LTEXT(txt) 54 | 55 | #define wchar_to_utf8_no_alloc(wsrc, dest, dest_size) \ 56 | WideCharToMultiByte(CP_UTF8, 0, wsrc, -1, dest, dest_size, NULL, NULL) 57 | #define utf8_to_wchar_no_alloc(src, wdest, wdest_size) \ 58 | MultiByteToWideChar(CP_UTF8, 0, src, -1, wdest, wdest_size) 59 | #define Edit_ReplaceSelU(hCtrl, str) ((void)SendMessageLU(hCtrl, EM_REPLACESEL, (WPARAM)FALSE, str)) 60 | #define ComboBox_AddStringU(hCtrl, str) ((int)(DWORD)SendMessageLU(hCtrl, CB_ADDSTRING, (WPARAM)FALSE, str)) 61 | #define ComboBox_InsertStringU(hCtrl, index, str) ((int)(DWORD)SendMessageLU(hCtrl, CB_INSERTSTRING, (WPARAM)index, str)) 62 | #define ComboBox_GetTextU(hCtrl, str, max_str) GetWindowTextU(hCtrl, str, max_str) 63 | #define GetSaveFileNameU(p) GetOpenSaveFileNameU(p, TRUE) 64 | #define GetOpenFileNameU(p) GetOpenSaveFileNameU(p, FALSE) 65 | #define ListView_SetItemTextU(hwndLV,i,iSubItem_,pszText_) { LVITEMW _ms_wlvi; _ms_wlvi.iSubItem = iSubItem_; \ 66 | _ms_wlvi.pszText = utf8_to_wchar(pszText_); \ 67 | SNDMSG((hwndLV),LVM_SETITEMTEXTW,(WPARAM)(i),(LPARAM)&_ms_wlvi); sfree(_ms_wlvi.pszText);} 68 | 69 | // Never ever use isdigit() or isspace(), etc. on UTF-8 strings! 70 | // These calls take an int and char is signed so MS compilers will produce an assert error on anything that's > 0x80 71 | #define isasciiU(c) isascii((unsigned char)(c)) 72 | #define iscntrlU(c) iscntrl((unsigned char)(c)) 73 | #define isdigitU(c) isdigit((unsigned char)(c)) 74 | #define isspaceU(c) isspace((unsigned char)(c)) 75 | #define isxdigitU(c) isxdigit((unsigned char)(c)) 76 | // NB: other issomething() calls are not implemented as they may require multibyte UTF-8 sequences to be converted 77 | 78 | #define sfree(p) do {if (p != NULL) {free((void*)(p)); p = NULL;}} while(0) 79 | #define wconvert(p) wchar_t* w ## p = utf8_to_wchar(p) 80 | #define walloc(p, size) wchar_t* w ## p = (p == NULL)?NULL:(wchar_t*)calloc(size, sizeof(wchar_t)) 81 | #define wfree(p) sfree(w ## p) 82 | 83 | /* 84 | * Converts an UTF-16 string to UTF8 (allocate returned string) 85 | * Returns NULL on error 86 | */ 87 | static __inline char* wchar_to_utf8(const wchar_t* wstr) 88 | { 89 | int size = 0; 90 | char* str = NULL; 91 | 92 | // Convert the empty string too 93 | if (wstr[0] == 0) 94 | return (char*)calloc(1, 1); 95 | 96 | // Find out the size we need to allocate for our converted string 97 | size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); 98 | if (size <= 1) // An empty string would be size 1 99 | return NULL; 100 | 101 | if ((str = (char*)calloc(size, 1)) == NULL) 102 | return NULL; 103 | 104 | if (wchar_to_utf8_no_alloc(wstr, str, size) != size) { 105 | sfree(str); 106 | return NULL; 107 | } 108 | 109 | return str; 110 | } 111 | 112 | /* 113 | * Converts an UTF8 string to UTF-16 (allocate returned string) 114 | * Returns NULL on error 115 | */ 116 | static __inline wchar_t* utf8_to_wchar(const char* str) 117 | { 118 | int size = 0; 119 | wchar_t* wstr = NULL; 120 | 121 | if (str == NULL) 122 | return NULL; 123 | 124 | // Convert the empty string too 125 | if (str[0] == 0) 126 | return (wchar_t*)calloc(1, sizeof(wchar_t)); 127 | 128 | // Find out the size we need to allocate for our converted string 129 | size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); 130 | if (size <= 1) // An empty string would be size 1 131 | return NULL; 132 | 133 | if ((wstr = (wchar_t*)calloc(size, sizeof(wchar_t))) == NULL) 134 | return NULL; 135 | 136 | if (utf8_to_wchar_no_alloc(str, wstr, size) != size) { 137 | sfree(wstr); 138 | return NULL; 139 | } 140 | return wstr; 141 | } 142 | 143 | /* 144 | * Converts an non NUL-terminated UTF-16 string of length len to UTF8 (allocate returned string) 145 | * Returns NULL on error 146 | */ 147 | static __inline char* wchar_len_to_utf8(const wchar_t* wstr, int wlen) 148 | { 149 | int size = 0; 150 | char* str = NULL; 151 | 152 | // Find out the size we need to allocate for our converted string 153 | size = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, NULL, 0, NULL, NULL); 154 | if (size <= 1) // An empty string would be size 1 155 | return NULL; 156 | 157 | if ((str = (char*)calloc(size, 1)) == NULL) 158 | return NULL; 159 | 160 | if (WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, str, size, NULL, NULL) != size) { 161 | sfree(str); 162 | return NULL; 163 | } 164 | 165 | return str; 166 | } 167 | 168 | static __inline DWORD FormatMessageU(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, 169 | DWORD dwLanguageId, char* lpBuffer, DWORD nSize, va_list *Arguments) 170 | { 171 | DWORD ret = 0, err = ERROR_INVALID_DATA; 172 | // coverity[returned_null] 173 | walloc(lpBuffer, nSize); 174 | ret = FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, wlpBuffer, nSize, Arguments); 175 | err = GetLastError(); 176 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nSize)) == 0)) { 177 | err = GetLastError(); 178 | ret = 0; 179 | } 180 | wfree(lpBuffer); 181 | SetLastError(err); 182 | return ret; 183 | } 184 | 185 | // SendMessage, with LPARAM as UTF-8 string 186 | static __inline LRESULT SendMessageLU(HWND hWnd, UINT Msg, WPARAM wParam, const char* lParam) 187 | { 188 | LRESULT ret = FALSE; 189 | DWORD err = ERROR_INVALID_DATA; 190 | wconvert(lParam); 191 | ret = SendMessageW(hWnd, Msg, wParam, (LPARAM)wlParam); 192 | err = GetLastError(); 193 | wfree(lParam); 194 | SetLastError(err); 195 | return ret; 196 | } 197 | 198 | static __inline int DrawTextExU(HDC hDC, LPCSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat, LPDRAWTEXTPARAMS lpDTParams) 199 | { 200 | int ret; 201 | DWORD err = ERROR_INVALID_DATA; 202 | wconvert(lpchText); 203 | ret = DrawTextExW(hDC, wlpchText, nCount, lpRect, uFormat, lpDTParams); 204 | err = GetLastError(); 205 | wfree(lpchText); 206 | SetLastError(err); 207 | return ret; 208 | } 209 | 210 | static __inline BOOL SHGetPathFromIDListU(LPCITEMIDLIST pidl, char* pszPath) 211 | { 212 | BOOL ret = FALSE; 213 | DWORD err = ERROR_INVALID_DATA; 214 | // coverity[returned_null] 215 | walloc(pszPath, MAX_PATH); 216 | ret = SHGetPathFromIDListW(pidl, wpszPath); 217 | err = GetLastError(); 218 | if ((ret) && (wchar_to_utf8_no_alloc(wpszPath, pszPath, MAX_PATH) == 0)) { 219 | err = GetLastError(); 220 | ret = FALSE; 221 | } 222 | wfree(pszPath); 223 | SetLastError(err); 224 | return ret; 225 | } 226 | 227 | static __inline HWND CreateWindowU(char* lpClassName, char* lpWindowName, 228 | DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, 229 | HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) 230 | { 231 | HWND ret = NULL; 232 | DWORD err = ERROR_INVALID_DATA; 233 | wconvert(lpClassName); 234 | wconvert(lpWindowName); 235 | ret = CreateWindowW(wlpClassName, wlpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); 236 | err = GetLastError(); 237 | wfree(lpClassName); 238 | wfree(lpWindowName); 239 | SetLastError(err); 240 | return ret; 241 | } 242 | 243 | static __inline HWND CreateWindowExU(DWORD dwExStyle, char* lpClassName, char* lpWindowName, 244 | DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, 245 | HINSTANCE hInstance, LPVOID lpParam) 246 | { 247 | HWND ret = NULL; 248 | DWORD err = ERROR_INVALID_DATA; 249 | wconvert(lpClassName); 250 | wconvert(lpWindowName); 251 | ret = CreateWindowExW(dwExStyle, wlpClassName, wlpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); 252 | err = GetLastError(); 253 | wfree(lpClassName); 254 | wfree(lpWindowName); 255 | SetLastError(err); 256 | return ret; 257 | } 258 | 259 | static __inline int MessageBoxU(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) 260 | { 261 | int ret; 262 | DWORD err = ERROR_INVALID_DATA; 263 | wconvert(lpText); 264 | wconvert(lpCaption); 265 | ret = MessageBoxW(hWnd, wlpText, wlpCaption, uType); 266 | err = GetLastError(); 267 | wfree(lpText); 268 | wfree(lpCaption); 269 | SetLastError(err); 270 | return ret; 271 | } 272 | 273 | static __inline int MessageBoxExU(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType, WORD wLanguageId) 274 | { 275 | int ret; 276 | DWORD err = ERROR_INVALID_DATA; 277 | wconvert(lpText); 278 | wconvert(lpCaption); 279 | ret = MessageBoxExW(hWnd, wlpText, wlpCaption, uType, wLanguageId); 280 | err = GetLastError(); 281 | wfree(lpText); 282 | wfree(lpCaption); 283 | SetLastError(err); 284 | return ret; 285 | } 286 | 287 | static __inline int LoadStringU(HINSTANCE hInstance, UINT uID, LPSTR lpBuffer, int nBufferMax) 288 | { 289 | int ret; 290 | DWORD err = ERROR_INVALID_DATA; 291 | if (nBufferMax == 0) { 292 | // read-only pointer to resource mode is not supported 293 | SetLastError(ERROR_INVALID_PARAMETER); 294 | return 0; 295 | } 296 | // coverity[returned_null] 297 | walloc(lpBuffer, nBufferMax); 298 | ret = LoadStringW(hInstance, uID, wlpBuffer, nBufferMax); 299 | err = GetLastError(); 300 | if ((ret > 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferMax)) == 0)) { 301 | err = GetLastError(); 302 | } 303 | wfree(lpBuffer); 304 | SetLastError(err); 305 | return ret; 306 | } 307 | 308 | static __inline HMODULE LoadLibraryU(LPCSTR lpFileName) 309 | { 310 | HMODULE ret; 311 | DWORD err = ERROR_INVALID_DATA; 312 | wconvert(lpFileName); 313 | ret = LoadLibraryW(wlpFileName); 314 | err = GetLastError(); 315 | wfree(lpFileName); 316 | SetLastError(err); 317 | return ret; 318 | } 319 | 320 | static __inline HMODULE LoadLibraryExU(LPCSTR lpFileName, HANDLE hFile, DWORD dwFlags) 321 | { 322 | HMODULE ret; 323 | DWORD err = ERROR_INVALID_DATA; 324 | wconvert(lpFileName); 325 | ret = LoadLibraryExW(wlpFileName, hFile, dwFlags); 326 | err = GetLastError(); 327 | wfree(lpFileName); 328 | SetLastError(err); 329 | return ret; 330 | } 331 | 332 | static __inline int DrawTextU(HDC hDC, LPCSTR lpText, int nCount, LPRECT lpRect, UINT uFormat) 333 | { 334 | int ret; 335 | DWORD err = ERROR_INVALID_DATA; 336 | wconvert(lpText); 337 | ret = DrawTextW(hDC, wlpText, nCount, lpRect, uFormat); 338 | err = GetLastError(); 339 | wfree(lpText); 340 | SetLastError(err); 341 | return ret; 342 | } 343 | 344 | static __inline int GetWindowTextU(HWND hWnd, char* lpString, int nMaxCount) 345 | { 346 | int ret = 0; 347 | DWORD err = ERROR_INVALID_PARAMETER; 348 | if (lpString == NULL || nMaxCount < 1) 349 | goto out; 350 | // Handle the empty string as GetWindowTextW() returns 0 then 351 | lpString[0] = 0; 352 | walloc(lpString, nMaxCount); 353 | ret = GetWindowTextW(hWnd, wlpString, nMaxCount); 354 | err = GetLastError(); 355 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpString, lpString, nMaxCount)) == 0)) 356 | err = GetLastError(); 357 | wfree(lpString); 358 | lpString[nMaxCount - 1] = 0; 359 | out: 360 | SetLastError(err); 361 | return ret; 362 | } 363 | 364 | static __inline BOOL SetWindowTextU(HWND hWnd, const char* lpString) 365 | { 366 | BOOL ret = FALSE; 367 | DWORD err = ERROR_INVALID_DATA; 368 | wconvert(lpString); 369 | ret = SetWindowTextW(hWnd, wlpString); 370 | err = GetLastError(); 371 | wfree(lpString); 372 | SetLastError(err); 373 | return ret; 374 | } 375 | 376 | static __inline int GetWindowTextLengthU(HWND hWnd) 377 | { 378 | int ret = 0; 379 | DWORD err = ERROR_INVALID_DATA; 380 | wchar_t* wbuf = NULL; 381 | char* buf = NULL; 382 | 383 | ret = GetWindowTextLengthW(hWnd); 384 | err = GetLastError(); 385 | if (ret == 0) goto out; 386 | wbuf = (wchar_t* )calloc(ret, sizeof(wchar_t)); 387 | err = GetLastError(); 388 | if (wbuf == NULL) { 389 | err = ERROR_OUTOFMEMORY; ret = 0; goto out; 390 | } 391 | ret = GetWindowTextW(hWnd, wbuf, ret); 392 | err = GetLastError(); 393 | if (ret == 0) goto out; 394 | buf = wchar_to_utf8(wbuf); 395 | err = GetLastError(); 396 | if (buf == NULL) { 397 | err = ERROR_OUTOFMEMORY; ret = 0; goto out; 398 | } 399 | ret = (int)strlen(buf) + 2; // GetDlgItemText seems to add a character 400 | err = GetLastError(); 401 | out: 402 | sfree(wbuf); 403 | sfree(buf); 404 | SetLastError(err); 405 | return ret; 406 | } 407 | 408 | static __inline UINT GetDlgItemTextU(HWND hDlg, int nIDDlgItem, char* lpString, int nMaxCount) 409 | { 410 | UINT ret = 0; 411 | DWORD err = ERROR_INVALID_DATA; 412 | // coverity[returned_null] 413 | walloc(lpString, nMaxCount); 414 | ret = GetDlgItemTextW(hDlg, nIDDlgItem, wlpString, nMaxCount); 415 | err = GetLastError(); 416 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpString, lpString, nMaxCount)) == 0)) { 417 | err = GetLastError(); 418 | } 419 | wfree(lpString); 420 | SetLastError(err); 421 | return ret; 422 | } 423 | 424 | static __inline BOOL SetDlgItemTextU(HWND hDlg, int nIDDlgItem, const char* lpString) 425 | { 426 | BOOL ret = FALSE; 427 | DWORD err = ERROR_INVALID_DATA; 428 | wconvert(lpString); 429 | ret = SetDlgItemTextW(hDlg, nIDDlgItem, wlpString); 430 | err = GetLastError(); 431 | wfree(lpString); 432 | SetLastError(err); 433 | return ret; 434 | } 435 | 436 | static __inline BOOL InsertMenuU(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, const char* lpNewItem) 437 | { 438 | BOOL ret = FALSE; 439 | DWORD err = ERROR_INVALID_DATA; 440 | wconvert(lpNewItem); 441 | ret = InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, wlpNewItem); 442 | err = GetLastError(); 443 | wfree(lpNewItem); 444 | SetLastError(err); 445 | return ret; 446 | } 447 | 448 | static __inline int ComboBox_GetLBTextU(HWND hCtrl, int index, char* lpString) 449 | { 450 | int size; 451 | DWORD err = ERROR_INVALID_DATA; 452 | wchar_t* wlpString; 453 | if (lpString == NULL) 454 | return CB_ERR; 455 | size = (int)SendMessageW(hCtrl, CB_GETLBTEXTLEN, (WPARAM)index, (LPARAM)0); 456 | if (size < 0) 457 | return size; 458 | wlpString = (wchar_t*)calloc((size_t)size + 1, sizeof(wchar_t)); 459 | size = (int)SendMessageW(hCtrl, CB_GETLBTEXT, (WPARAM)index, (LPARAM)wlpString); 460 | err = GetLastError(); 461 | if (size > 0) 462 | wchar_to_utf8_no_alloc(wlpString, lpString, size+1); 463 | wfree(lpString); 464 | SetLastError(err); 465 | return size; 466 | } 467 | 468 | static __inline DWORD CharUpperBuffU(char* lpString, DWORD len) 469 | { 470 | DWORD ret; 471 | wchar_t *wlpString = (wchar_t*)calloc(len, sizeof(wchar_t)); 472 | if (wlpString == NULL) 473 | return 0; 474 | utf8_to_wchar_no_alloc(lpString, wlpString, len); 475 | ret = CharUpperBuffW(wlpString, len); 476 | wchar_to_utf8_no_alloc(wlpString, lpString, len); 477 | free(wlpString); 478 | return ret; 479 | } 480 | 481 | static __inline HANDLE CreateFileU(const char* lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, 482 | LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, 483 | DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) 484 | { 485 | HANDLE ret = INVALID_HANDLE_VALUE; 486 | DWORD err = ERROR_INVALID_DATA; 487 | wconvert(lpFileName); 488 | ret = CreateFileW(wlpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, 489 | dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 490 | err = GetLastError(); 491 | wfree(lpFileName); 492 | SetLastError(err); 493 | return ret; 494 | } 495 | 496 | static __inline BOOL CreateDirectoryU(const char* lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) 497 | { 498 | BOOL ret = FALSE; 499 | DWORD err = ERROR_INVALID_DATA; 500 | wconvert(lpPathName); 501 | ret = CreateDirectoryW(wlpPathName, lpSecurityAttributes); 502 | err = GetLastError(); 503 | wfree(lpPathName); 504 | SetLastError(err); 505 | return ret; 506 | } 507 | 508 | static __inline BOOL CopyFileU(const char* lpExistingFileName, const char* lpNewFileName, BOOL bFailIfExists) 509 | { 510 | BOOL ret = FALSE; 511 | DWORD err = ERROR_INVALID_DATA; 512 | wconvert(lpExistingFileName); 513 | wconvert(lpNewFileName); 514 | ret = CopyFileW(wlpExistingFileName, wlpNewFileName, bFailIfExists); 515 | err = GetLastError(); 516 | wfree(lpExistingFileName); 517 | wfree(lpNewFileName); 518 | SetLastError(err); 519 | return ret; 520 | } 521 | 522 | static __inline BOOL DeleteFileU(const char* lpFileName) 523 | { 524 | BOOL ret = FALSE; 525 | DWORD err = ERROR_INVALID_DATA; 526 | wconvert(lpFileName); 527 | ret = DeleteFileW(wlpFileName); 528 | err = GetLastError(); 529 | wfree(lpFileName); 530 | SetLastError(err); 531 | return ret; 532 | } 533 | 534 | static __inline BOOL PathFileExistsU(char* szPath) 535 | { 536 | BOOL ret; 537 | wconvert(szPath); 538 | ret = PathFileExistsW(wszPath); 539 | wfree(szPath); 540 | return ret; 541 | } 542 | 543 | static __inline int PathGetDriveNumberU(char* lpPath) 544 | { 545 | int ret = 0; 546 | DWORD err = ERROR_INVALID_DATA; 547 | wconvert(lpPath); 548 | ret = PathGetDriveNumberW(wlpPath); 549 | err = GetLastError(); 550 | wfree(lpPath); 551 | SetLastError(err); 552 | return ret; 553 | } 554 | 555 | // This one is tricky since we can't blindly convert a 556 | // UTF-16 position to a UTF-8 one. So we do it manually. 557 | static __inline const char* PathFindFileNameU(const char* szPath) 558 | { 559 | size_t i; 560 | if (szPath == NULL) 561 | return NULL; 562 | for (i = strlen(szPath); i != 0; i--) { 563 | if ((szPath[i] == '/') || (szPath[i] == '\\')) { 564 | i++; 565 | break; 566 | } 567 | } 568 | return &szPath[i]; 569 | } 570 | 571 | // This function differs from regular GetTextExtentPoint in that it uses a zero terminated string 572 | static __inline BOOL GetTextExtentPointU(HDC hdc, const char* lpString, LPSIZE lpSize) 573 | { 574 | BOOL ret = FALSE; 575 | DWORD err = ERROR_INVALID_DATA; 576 | wconvert(lpString); 577 | if (wlpString == NULL) 578 | return FALSE; 579 | ret = GetTextExtentPoint32W(hdc, wlpString, (int)wcslen(wlpString), lpSize); 580 | err = GetLastError(); 581 | wfree(lpString); 582 | SetLastError(err); 583 | return ret; 584 | } 585 | 586 | // A UTF-8 alternative to MS GetCurrentDirectory() since the latter is useless for 587 | // apps installed from the App Store... 588 | static __inline DWORD GetCurrentDirectoryU(DWORD nBufferLength, char* lpBuffer) 589 | { 590 | DWORD i, ret = 0, err = ERROR_INVALID_DATA; 591 | // coverity[returned_null] 592 | walloc(lpBuffer, nBufferLength); 593 | if (wlpBuffer == NULL) { 594 | SetLastError(ERROR_OUTOFMEMORY); 595 | return 0; 596 | } 597 | ret = GetModuleFileNameW(NULL, wlpBuffer, nBufferLength); 598 | err = GetLastError(); 599 | if (ret > 0) { 600 | for (i = ret - 1; i > 0; i--) { 601 | if (wlpBuffer[i] == L'\\') { 602 | wlpBuffer[i] = 0; 603 | break; 604 | } 605 | } 606 | } 607 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 608 | err = GetLastError(); 609 | } 610 | wfree(lpBuffer); 611 | SetLastError(err); 612 | return ret; 613 | } 614 | 615 | static __inline UINT GetSystemDirectoryU(char* lpBuffer, UINT uSize) 616 | { 617 | UINT ret = 0, err = ERROR_INVALID_DATA; 618 | // coverity[returned_null] 619 | walloc(lpBuffer, uSize); 620 | ret = GetSystemDirectoryW(wlpBuffer, uSize); 621 | err = GetLastError(); 622 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, uSize)) == 0)) { 623 | err = GetLastError(); 624 | } 625 | wfree(lpBuffer); 626 | SetLastError(err); 627 | return ret; 628 | } 629 | 630 | static __inline UINT GetSystemWindowsDirectoryU(char* lpBuffer, UINT uSize) 631 | { 632 | UINT ret = 0, err = ERROR_INVALID_DATA; 633 | // coverity[returned_null] 634 | walloc(lpBuffer, uSize); 635 | ret = GetSystemWindowsDirectoryW(wlpBuffer, uSize); 636 | err = GetLastError(); 637 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, uSize)) == 0)) { 638 | err = GetLastError(); 639 | } 640 | wfree(lpBuffer); 641 | SetLastError(err); 642 | return ret; 643 | } 644 | 645 | static __inline BOOL SHGetSpecialFolderPathU(HWND hwnd, char* pszPath, int csidl, BOOL fCreate) 646 | { 647 | BOOL ret; 648 | DWORD err = ERROR_INVALID_DATA; 649 | // pszPath is at least MAX_PATH characters in size 650 | WCHAR wpszPath[MAX_PATH] = { 0 }; 651 | ret = SHGetSpecialFolderPathW(hwnd, wpszPath, csidl, fCreate); 652 | err = GetLastError(); 653 | wchar_to_utf8_no_alloc(wpszPath, pszPath, MAX_PATH); 654 | SetLastError(err); 655 | return ret; 656 | } 657 | 658 | static __inline DWORD GetTempPathU(DWORD nBufferLength, char* lpBuffer) 659 | { 660 | DWORD ret = 0, err = ERROR_INVALID_DATA; 661 | // coverity[returned_null] 662 | walloc(lpBuffer, nBufferLength); 663 | ret = GetTempPathW(nBufferLength, wlpBuffer); 664 | err = GetLastError(); 665 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 666 | err = GetLastError(); 667 | } 668 | wfree(lpBuffer); 669 | SetLastError(err); 670 | return ret; 671 | } 672 | 673 | static __inline DWORD GetTempFileNameU(char* lpPathName, char* lpPrefixString, UINT uUnique, char* lpTempFileName) 674 | { 675 | DWORD ret = 0, err = ERROR_INVALID_DATA; 676 | wconvert(lpPathName); 677 | wconvert(lpPrefixString); 678 | // coverity[returned_null] 679 | walloc(lpTempFileName, MAX_PATH); 680 | ret = GetTempFileNameW(wlpPathName, wlpPrefixString, uUnique, wlpTempFileName); 681 | err = GetLastError(); 682 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpTempFileName, lpTempFileName, MAX_PATH)) == 0)) { 683 | err = GetLastError(); 684 | } 685 | wfree(lpTempFileName); 686 | wfree(lpPrefixString); 687 | wfree(lpPathName); 688 | SetLastError(err); 689 | return ret; 690 | } 691 | 692 | static __inline DWORD GetModuleFileNameU(HMODULE hModule, char* lpFilename, DWORD nSize) 693 | { 694 | DWORD ret = 0, err = ERROR_INVALID_DATA; 695 | // coverity[returned_null] 696 | walloc(lpFilename, nSize); 697 | ret = GetModuleFileNameW(hModule, wlpFilename, nSize); 698 | err = GetLastError(); 699 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpFilename, lpFilename, nSize)) == 0)) { 700 | err = GetLastError(); 701 | } 702 | wfree(lpFilename); 703 | SetLastError(err); 704 | return ret; 705 | } 706 | 707 | static __inline DWORD GetModuleFileNameExU(HANDLE hProcess, HMODULE hModule, char* lpFilename, DWORD nSize) 708 | { 709 | DWORD ret = 0, err = ERROR_INVALID_DATA; 710 | // coverity[returned_null] 711 | walloc(lpFilename, nSize); 712 | ret = GetModuleFileNameExW(hProcess, hModule, wlpFilename, nSize); 713 | err = GetLastError(); 714 | if ((ret != 0) 715 | && ((ret = wchar_to_utf8_no_alloc(wlpFilename, lpFilename, nSize)) == 0)) { 716 | err = GetLastError(); 717 | } 718 | wfree(lpFilename); 719 | SetLastError(err); 720 | return ret; 721 | } 722 | 723 | static __inline DWORD GetFinalPathNameByHandleU(HANDLE hFile, char* lpszFilePath, DWORD cchFilePath, DWORD dwFlags) 724 | { 725 | DWORD ret = 0, err = ERROR_INVALID_DATA; 726 | walloc(lpszFilePath, cchFilePath); 727 | ret = GetFinalPathNameByHandleW(hFile, wlpszFilePath, cchFilePath, dwFlags); 728 | err = GetLastError(); 729 | if ((ret != 0) 730 | && ((ret = wchar_to_utf8_no_alloc(wlpszFilePath, lpszFilePath, cchFilePath)) == 0)) { 731 | err = GetLastError(); 732 | } 733 | wfree(lpszFilePath); 734 | SetLastError(err); 735 | return ret; 736 | } 737 | 738 | static __inline DWORD GetFileVersionInfoSizeU(const char* lpFileName, LPDWORD lpdwHandle) 739 | { 740 | DWORD ret = 0, err = ERROR_INVALID_DATA; 741 | wconvert(lpFileName); 742 | ret = GetFileVersionInfoSizeW(wlpFileName, lpdwHandle); 743 | err = GetLastError(); 744 | wfree(lpFileName); 745 | SetLastError(err); 746 | return ret; 747 | } 748 | 749 | static __inline BOOL GetFileVersionInfoU(const char* lpFileName, DWORD dwHandle, DWORD dwLen, LPVOID lpData) 750 | { 751 | BOOL ret = FALSE; 752 | DWORD err = ERROR_INVALID_DATA; 753 | wconvert(lpFileName); 754 | if (dwHandle != 0) 755 | SetLastError(ERROR_INVALID_PARAMETER); 756 | else 757 | ret = GetFileVersionInfoW(wlpFileName, dwHandle, dwLen, lpData); 758 | err = GetLastError(); 759 | wfree(lpFileName); 760 | SetLastError(err); 761 | return ret; 762 | } 763 | 764 | static __inline DWORD GetFullPathNameU(const char* lpFileName, DWORD nBufferLength, char* lpBuffer, char** lpFilePart) 765 | { 766 | DWORD ret = 0, err = ERROR_INVALID_DATA; 767 | wchar_t* wlpFilePart; 768 | wconvert(lpFileName); 769 | // coverity[returned_null] 770 | walloc(lpBuffer, nBufferLength); 771 | 772 | // lpFilePart is not supported 773 | if (lpFilePart != NULL) goto out; 774 | 775 | ret = GetFullPathNameW(wlpFileName, nBufferLength, wlpBuffer, &wlpFilePart); 776 | err = GetLastError(); 777 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 778 | err = GetLastError(); 779 | } 780 | 781 | out: 782 | wfree(lpBuffer); 783 | wfree(lpFileName); 784 | SetLastError(err); 785 | return ret; 786 | } 787 | 788 | static __inline DWORD GetFileAttributesU(const char* lpFileName) 789 | { 790 | DWORD ret = 0xFFFFFFFF, err = ERROR_INVALID_DATA; 791 | wconvert(lpFileName); 792 | // Unlike Microsoft's version, ours doesn't fail if the string is quoted 793 | if ((wlpFileName[0] == L'"') && (wlpFileName[wcslen(wlpFileName) - 1] == L'"')) { 794 | wlpFileName[wcslen(wlpFileName) - 1] = 0; 795 | ret = GetFileAttributesW(&wlpFileName[1]); 796 | } else { 797 | ret = GetFileAttributesW(wlpFileName); 798 | } 799 | err = GetLastError(); 800 | wfree(lpFileName); 801 | SetLastError(err); 802 | return ret; 803 | } 804 | 805 | static __inline BOOL SetFileAttributesU(const char* lpFileName, DWORD dwFileAttributes) 806 | { 807 | BOOL ret = FALSE, err = ERROR_INVALID_DATA; 808 | wconvert(lpFileName); 809 | // Unlike Microsoft's version, ours doesn't fail if the string is quoted 810 | if ((wlpFileName[0] == L'"') && (wlpFileName[wcslen(wlpFileName) - 1] == L'"')) { 811 | wlpFileName[wcslen(wlpFileName) - 1] = 0; 812 | ret = SetFileAttributesW(&wlpFileName[1], dwFileAttributes); 813 | } else { 814 | ret = SetFileAttributesW(wlpFileName, dwFileAttributes); 815 | } 816 | err = GetLastError(); 817 | wfree(lpFileName); 818 | SetLastError(err); 819 | return ret; 820 | } 821 | 822 | static __inline int SHCreateDirectoryExU(HWND hwnd, const char* pszPath, SECURITY_ATTRIBUTES *psa) 823 | { 824 | int ret = ERROR_INVALID_DATA; 825 | DWORD err = ERROR_INVALID_DATA; 826 | wconvert(pszPath); 827 | ret = SHCreateDirectoryExW(hwnd, wpszPath, psa); 828 | err = GetLastError(); 829 | wfree(pszPath); 830 | SetLastError(err); 831 | return ret; 832 | } 833 | 834 | static __inline int SHDeleteDirectoryExU(HWND hwnd, const char* pszPath, FILEOP_FLAGS fFlags) 835 | { 836 | int ret; 837 | // String needs to be double NULL terminated, so we just use the length of the UTF-8 string 838 | // which is always expected to be larger than our UTF-16 one, and add 2 chars for good measure. 839 | size_t wpszPath_len = (pszPath == NULL) ? 0 : strlen(pszPath) + 2; 840 | // coverity[returned_null] 841 | walloc(pszPath, wpszPath_len); 842 | SHFILEOPSTRUCTW shfo = { hwnd, FO_DELETE, wpszPath, NULL, fFlags, FALSE, NULL, NULL }; 843 | utf8_to_wchar_no_alloc(pszPath, wpszPath, (int)wpszPath_len); 844 | // FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION, 845 | ret = SHFileOperationW(&shfo); 846 | wfree(pszPath); 847 | return ret; 848 | } 849 | 850 | static __inline BOOL ShellExecuteExU(SHELLEXECUTEINFOA* lpExecInfo) 851 | { 852 | BOOL ret = FALSE; 853 | DWORD err = ERROR_INVALID_DATA; 854 | SHELLEXECUTEINFOW wExecInfo; 855 | 856 | // Because we're lazy, we'll assume that the A and W structs inherently have the same size 857 | if (lpExecInfo->cbSize != sizeof(SHELLEXECUTEINFOW)) { 858 | SetLastError(ERROR_BAD_LENGTH); return FALSE; 859 | } 860 | memcpy(&wExecInfo, lpExecInfo, lpExecInfo->cbSize); 861 | wExecInfo.lpVerb = utf8_to_wchar(lpExecInfo->lpVerb); 862 | wExecInfo.lpFile = utf8_to_wchar(lpExecInfo->lpFile); 863 | wExecInfo.lpParameters = utf8_to_wchar(lpExecInfo->lpParameters); 864 | wExecInfo.lpDirectory = utf8_to_wchar(lpExecInfo->lpDirectory); 865 | if (wExecInfo.fMask & SEE_MASK_CLASSNAME) { 866 | wExecInfo.lpClass = utf8_to_wchar(lpExecInfo->lpClass); 867 | } else { 868 | wExecInfo.lpClass = NULL; 869 | } 870 | ret = ShellExecuteExW(&wExecInfo); 871 | err = GetLastError(); 872 | // Copy the returned values back 873 | lpExecInfo->hInstApp = wExecInfo.hInstApp; 874 | lpExecInfo->hProcess = wExecInfo.hProcess; 875 | sfree(wExecInfo.lpVerb); 876 | sfree(wExecInfo.lpFile); 877 | sfree(wExecInfo.lpParameters); 878 | sfree(wExecInfo.lpDirectory); 879 | sfree(wExecInfo.lpClass); 880 | SetLastError(err); 881 | return ret; 882 | } 883 | 884 | // Doesn't support LPSTARTUPINFOEX struct 885 | static __inline BOOL CreateProcessU(const char* lpApplicationName, const char* lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, 886 | LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, 887 | LPVOID lpEnvironment, const char* lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, 888 | LPPROCESS_INFORMATION lpProcessInformation) 889 | { 890 | BOOL ret = FALSE; 891 | DWORD err = ERROR_INVALID_DATA; 892 | STARTUPINFOW wStartupInfo; 893 | wconvert(lpApplicationName); 894 | wconvert(lpCommandLine); 895 | wconvert(lpCurrentDirectory); 896 | 897 | // Because we're lazy, we'll assume that the A and W structs inherently have the same size 898 | // Also prevents the use of STARTUPINFOEX 899 | if (lpStartupInfo->cb != sizeof(STARTUPINFOW)) { 900 | err = ERROR_BAD_LENGTH; goto out; 901 | } 902 | memcpy(&wStartupInfo, lpStartupInfo, lpStartupInfo->cb); 903 | wStartupInfo.lpDesktop = utf8_to_wchar(lpStartupInfo->lpDesktop); 904 | wStartupInfo.lpTitle = utf8_to_wchar(lpStartupInfo->lpTitle); 905 | ret = CreateProcessW(wlpApplicationName, wlpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, 906 | dwCreationFlags, lpEnvironment, wlpCurrentDirectory, &wStartupInfo, lpProcessInformation); 907 | err = GetLastError(); 908 | sfree(wStartupInfo.lpDesktop); 909 | sfree(wStartupInfo.lpTitle); 910 | out: 911 | wfree(lpApplicationName); 912 | wfree(lpCommandLine); 913 | wfree(lpCurrentDirectory); 914 | SetLastError(err); 915 | return ret; 916 | } 917 | 918 | // NOTE: when used, nFileOffset & nFileExtension MUST be provided 919 | // in number of Unicode characters, NOT number of UTF-8 bytes 920 | static __inline BOOL WINAPI GetOpenSaveFileNameU(LPOPENFILENAMEA lpofn, BOOL save) 921 | { 922 | BOOL ret = FALSE; 923 | DWORD err = ERROR_INVALID_DATA; 924 | size_t i, len; 925 | OPENFILENAMEW wofn; 926 | memset(&wofn, 0, sizeof(wofn)); 927 | wofn.lStructSize = sizeof(wofn); 928 | wofn.hwndOwner = lpofn->hwndOwner; 929 | wofn.hInstance = lpofn->hInstance; 930 | 931 | // No support for custom filters 932 | if (lpofn->lpstrCustomFilter != NULL) goto out; 933 | 934 | // Count on Microsoft to use an moronic scheme for filters 935 | // that relies on NULL separators and double NULL terminators 936 | if (lpofn->lpstrFilter != NULL) { 937 | // Replace the NULLs by something that can be converted 938 | for (i=0; ; i++) { 939 | if (lpofn->lpstrFilter[i] == 0) { 940 | ((char*)lpofn->lpstrFilter)[i] = '\r'; 941 | if (lpofn->lpstrFilter[i+1] == 0) { 942 | break; 943 | } 944 | } 945 | } 946 | wofn.lpstrFilter = utf8_to_wchar(lpofn->lpstrFilter); 947 | // And revert 948 | len = wcslen(wofn.lpstrFilter); // don't use in the loop as it would be reevaluated 949 | for (i=0; ilpstrFilter); 955 | for (i=0; ilpstrFilter[i] == '\r') { 957 | ((char*)lpofn->lpstrFilter)[i] = 0; 958 | } 959 | } 960 | } else { 961 | wofn.lpstrFilter = NULL; 962 | } 963 | wofn.nMaxCustFilter = lpofn->nMaxCustFilter; 964 | wofn.nFilterIndex = lpofn->nFilterIndex; 965 | wofn.lpstrFile = (LPWSTR)calloc(lpofn->nMaxFile, sizeof(wchar_t)); 966 | utf8_to_wchar_no_alloc(lpofn->lpstrFile, wofn.lpstrFile, lpofn->nMaxFile); 967 | wofn.nMaxFile = lpofn->nMaxFile; 968 | wofn.lpstrFileTitle = (LPWSTR)calloc(lpofn->nMaxFileTitle, sizeof(wchar_t)); 969 | utf8_to_wchar_no_alloc(lpofn->lpstrFileTitle, wofn.lpstrFileTitle, lpofn->nMaxFileTitle); 970 | wofn.nMaxFileTitle = lpofn->nMaxFileTitle; 971 | wofn.lpstrInitialDir = utf8_to_wchar(lpofn->lpstrInitialDir); 972 | wofn.lpstrTitle = utf8_to_wchar(lpofn->lpstrTitle); 973 | wofn.Flags = lpofn->Flags; 974 | wofn.nFileOffset = lpofn->nFileOffset; 975 | wofn.nFileExtension = lpofn->nFileExtension; 976 | wofn.lpstrDefExt = utf8_to_wchar(lpofn->lpstrDefExt); 977 | wofn.lCustData = lpofn->lCustData; 978 | wofn.lpfnHook = lpofn->lpfnHook; 979 | wofn.lpTemplateName = utf8_to_wchar(lpofn->lpTemplateName); 980 | wofn.pvReserved = lpofn->pvReserved; 981 | wofn.dwReserved = lpofn->dwReserved; 982 | wofn.FlagsEx = lpofn->FlagsEx; 983 | 984 | if (save) { 985 | ret = GetSaveFileNameW(&wofn); 986 | } else { 987 | ret = GetOpenFileNameW(&wofn); 988 | } 989 | err = GetLastError(); 990 | if ( (ret) 991 | && ( (wchar_to_utf8_no_alloc(wofn.lpstrFile, lpofn->lpstrFile, lpofn->nMaxFile) == 0) 992 | || (wchar_to_utf8_no_alloc(wofn.lpstrFileTitle, lpofn->lpstrFileTitle, lpofn->nMaxFileTitle) == 0) ) ) { 993 | err = GetLastError(); 994 | ret = FALSE; 995 | } 996 | out: 997 | sfree(wofn.lpstrDefExt); 998 | sfree(wofn.lpstrFile); 999 | sfree(wofn.lpstrFileTitle); 1000 | sfree(wofn.lpstrFilter); 1001 | sfree(wofn.lpstrInitialDir); 1002 | sfree(wofn.lpstrTitle); 1003 | sfree(wofn.lpTemplateName); 1004 | SetLastError(err); 1005 | return ret; 1006 | } 1007 | 1008 | extern BOOL WINAPI UpdateDriverForPlugAndPlayDevicesW(HWND hwndParent, LPCWSTR HardwareId, 1009 | LPCWSTR FullInfPath, DWORD InstallFlags, PBOOL bRebootRequired); 1010 | 1011 | static __inline BOOL UpdateDriverForPlugAndPlayDevicesU(HWND hwndParent, const char* HardwareId, const char* FullInfPath, 1012 | DWORD InstallFlags, PBOOL bRebootRequired) 1013 | { 1014 | BOOL ret = FALSE; 1015 | DWORD err = ERROR_INVALID_DATA; 1016 | wconvert(HardwareId); 1017 | wconvert(FullInfPath); 1018 | ret = UpdateDriverForPlugAndPlayDevicesW(hwndParent, wHardwareId, wFullInfPath, InstallFlags, bRebootRequired); 1019 | err = GetLastError(); 1020 | wfree(HardwareId); 1021 | wfree(FullInfPath); 1022 | SetLastError(err); 1023 | return ret; 1024 | } 1025 | 1026 | static __inline BOOL SetupCopyOEMInfU(const char* SourceInfFileName, const char* OEMSourceMediaLocation, DWORD OEMSourceMediaType, 1027 | DWORD CopyStyle, char* DestinationInfFileName, DWORD DestinationInfFileNameSize, 1028 | PDWORD RequiredSize, PTSTR DestinationInfFileNameComponent) 1029 | { 1030 | BOOL ret = FALSE; 1031 | DWORD err = ERROR_INVALID_DATA; 1032 | wconvert(SourceInfFileName); 1033 | wconvert(OEMSourceMediaLocation); 1034 | // coverity[returned_null] 1035 | walloc(DestinationInfFileName, DestinationInfFileNameSize); 1036 | 1037 | // DestinationInfFileNameComponent is not supported 1038 | if (DestinationInfFileNameComponent != NULL) goto out; 1039 | 1040 | ret = SetupCopyOEMInfW(wSourceInfFileName, wOEMSourceMediaLocation, OEMSourceMediaType, CopyStyle, 1041 | wDestinationInfFileName, DestinationInfFileNameSize, RequiredSize, NULL); 1042 | err = GetLastError(); 1043 | if ((ret != FALSE) && ((ret = wchar_to_utf8_no_alloc(wDestinationInfFileName, DestinationInfFileName, DestinationInfFileNameSize)) == 0)) { 1044 | err = GetLastError(); 1045 | } 1046 | out: 1047 | wfree(SourceInfFileName); 1048 | wfree(OEMSourceMediaLocation); 1049 | wfree(DestinationInfFileName); 1050 | SetLastError(err); 1051 | return ret; 1052 | } 1053 | 1054 | static __inline int _chdirU(const char *dirname) 1055 | { 1056 | int ret; 1057 | wconvert(dirname); 1058 | ret = _wchdir(wdirname); 1059 | wfree(dirname); 1060 | return ret; 1061 | } 1062 | 1063 | #if defined(_WIN32_WINNT) && (_WIN32_WINNT <= 0x501) 1064 | static __inline FILE* fopenU(const char* filename, const char* mode) 1065 | { 1066 | FILE* ret = NULL; 1067 | wconvert(filename); 1068 | wconvert(mode); 1069 | ret = _wfopen(wfilename, wmode); 1070 | wfree(filename); 1071 | wfree(mode); 1072 | return ret; 1073 | } 1074 | 1075 | static __inline int _openU(const char *filename, int oflag, int pmode) 1076 | { 1077 | int ret = -1; 1078 | wconvert(filename); 1079 | ret = _wopen(wfilename, oflag, pmode); 1080 | wfree(filename); 1081 | return ret; 1082 | } 1083 | #else 1084 | static __inline FILE* fopenU(const char* filename, const char* mode) 1085 | { 1086 | FILE* ret = NULL; 1087 | wconvert(filename); 1088 | wconvert(mode); 1089 | _wfopen_s(&ret, wfilename, wmode); 1090 | wfree(filename); 1091 | wfree(mode); 1092 | return ret; 1093 | } 1094 | 1095 | static __inline int _openU(const char *filename, int oflag , int pmode) 1096 | { 1097 | int ret = -1; 1098 | int shflag = _SH_DENYNO; 1099 | wconvert(filename); 1100 | // Try to match the share flag to the oflag 1101 | if ((oflag & 0x03) == _O_RDONLY) 1102 | shflag = _SH_DENYWR; 1103 | else if ((oflag & 0x03) == _O_WRONLY) 1104 | shflag = _SH_DENYRD; 1105 | _wsopen_s(&ret, wfilename, oflag, shflag, pmode); 1106 | wfree(filename); 1107 | return ret; 1108 | } 1109 | #endif 1110 | 1111 | static __inline int _unlinkU(const char* path) 1112 | { 1113 | int ret; 1114 | wconvert(path); 1115 | ret = _wunlink(wpath); 1116 | wfree(path); 1117 | return ret; 1118 | } 1119 | 1120 | static __inline int _stat64U(const char *path, struct __stat64 *buffer) 1121 | { 1122 | int ret; 1123 | wconvert(path); 1124 | ret = _wstat64(wpath, buffer); 1125 | wfree(path); 1126 | return ret; 1127 | } 1128 | 1129 | static __inline int _accessU(const char* path, int mode) 1130 | { 1131 | int ret; 1132 | wconvert(path); 1133 | ret = _waccess(wpath, mode); 1134 | wfree(path); 1135 | return ret; 1136 | } 1137 | 1138 | static __inline const char* _filenameU(const char* path) 1139 | { 1140 | int i; 1141 | if (path == NULL) 1142 | return NULL; 1143 | for (i = (int)strlen(path) - 1; i >= 0; i--) 1144 | if ((path[i] == '/') || (path[i] == '\\')) 1145 | return &path[i + 1]; 1146 | return path; 1147 | } 1148 | 1149 | // returned UTF-8 string must be freed 1150 | static __inline char* getenvU(const char* varname) 1151 | { 1152 | wconvert(varname); 1153 | char* ret = NULL; 1154 | wchar_t* wbuf = NULL; 1155 | // _wgetenv() is *BROKEN* in MS compilers => use GetEnvironmentVariableW() 1156 | DWORD dwSize = GetEnvironmentVariableW(wvarname, wbuf, 0); 1157 | wbuf = (wchar_t*)calloc(dwSize, sizeof(wchar_t)); 1158 | if (wbuf == NULL) { 1159 | wfree(varname); 1160 | return NULL; 1161 | } 1162 | dwSize = GetEnvironmentVariableW(wvarname, wbuf, dwSize); 1163 | if (dwSize != 0) 1164 | ret = wchar_to_utf8(wbuf); 1165 | free(wbuf); 1166 | wfree(varname); 1167 | return ret; 1168 | } 1169 | 1170 | static __inline int _mkdirU(const char* dirname) 1171 | { 1172 | wconvert(dirname); 1173 | int ret; 1174 | ret = _wmkdir(wdirname); 1175 | wfree(dirname); 1176 | return ret; 1177 | } 1178 | 1179 | // This version of _mkdirU creates all needed directories along the way 1180 | static __inline int _mkdirExU(const char* dirname) 1181 | { 1182 | int ret = -1, trailing_slash = -1; 1183 | size_t i, len; 1184 | wconvert(dirname); 1185 | len = wcslen(wdirname); 1186 | while (trailing_slash && (len > 0)) { 1187 | if ((wdirname[len - 1] == '\\') || (wdirname[len - 1] == '/')) 1188 | wdirname[--len] = 0; 1189 | else 1190 | trailing_slash = 0; 1191 | } 1192 | for (i = 0; i < len; i++) 1193 | if ((wdirname[i] == '\\') || (wdirname[i] == '/')) 1194 | wdirname[i] = 0; 1195 | for (i = 0; i < len; ) { 1196 | if ((_wmkdir(wdirname) < 0) && (errno != EEXIST) && (errno != EACCES)) 1197 | goto out; 1198 | i = wcslen(wdirname); 1199 | wdirname[i] = '\\'; 1200 | } 1201 | ret = 0; 1202 | out: 1203 | wfree(dirname); 1204 | return ret; 1205 | } 1206 | 1207 | static __inline int _rmdirU(const char* dirname) 1208 | { 1209 | wconvert(dirname); 1210 | int ret; 1211 | ret = _wrmdir(wdirname); 1212 | wfree(dirname); 1213 | return ret; 1214 | } 1215 | 1216 | static __inline BOOL MoveFileU(const char* lpExistingFileName, const char* lpNewFileName) 1217 | { 1218 | wconvert(lpExistingFileName); 1219 | wconvert(lpNewFileName); 1220 | BOOL ret = MoveFileW(wlpExistingFileName, wlpNewFileName); 1221 | wfree(lpNewFileName); 1222 | wfree(lpExistingFileName); 1223 | return ret; 1224 | } 1225 | 1226 | static __inline BOOL MoveFileExU(const char* lpExistingFileName, const char* lpNewFileName, DWORD dwFlags) 1227 | { 1228 | wconvert(lpExistingFileName); 1229 | wconvert(lpNewFileName); 1230 | BOOL ret = MoveFileExW(wlpExistingFileName, wlpNewFileName, dwFlags); 1231 | wfree(lpNewFileName); 1232 | wfree(lpExistingFileName); 1233 | return ret; 1234 | } 1235 | 1236 | static __inline BOOL CreateSymbolicLinkU(const char* lpSymlinkFileName, const char* lpTargetFileName, DWORD dwFlags) 1237 | { 1238 | wconvert(lpSymlinkFileName); 1239 | wconvert(lpTargetFileName); 1240 | BOOL ret = CreateSymbolicLinkW(wlpSymlinkFileName, wlpTargetFileName, dwFlags); 1241 | wfree(lpTargetFileName); 1242 | wfree(lpSymlinkFileName); 1243 | return ret; 1244 | } 1245 | 1246 | // The following expects PropertyBuffer to contain a single Unicode string 1247 | static __inline BOOL SetupDiGetDeviceRegistryPropertyU(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, 1248 | DWORD Property, PDWORD PropertyRegDataType, PBYTE PropertyBuffer, DWORD PropertyBufferSize, PDWORD RequiredSize) 1249 | { 1250 | BOOL ret = FALSE; 1251 | DWORD err = ERROR_INVALID_DATA; 1252 | // coverity[returned_null] 1253 | walloc(PropertyBuffer, PropertyBufferSize); 1254 | 1255 | ret = SetupDiGetDeviceRegistryPropertyW(DeviceInfoSet, DeviceInfoData, Property, 1256 | PropertyRegDataType, (PBYTE)wPropertyBuffer, PropertyBufferSize, RequiredSize); 1257 | err = GetLastError(); 1258 | if ((ret != 0) && (wchar_to_utf8_no_alloc(wPropertyBuffer, 1259 | (char*)(uintptr_t)PropertyBuffer, PropertyBufferSize) == 0)) { 1260 | err = GetLastError(); 1261 | ret = FALSE; 1262 | } 1263 | wfree(PropertyBuffer); 1264 | SetLastError(err); 1265 | return ret; 1266 | } 1267 | 1268 | // NB: This does not support the ERROR_INSUFFICIENT_BUFFER dance to retrieve the required buffer size 1269 | static __inline BOOL GetUserNameU(LPSTR lpBuffer, LPDWORD pcbBuffer) 1270 | { 1271 | BOOL ret; 1272 | DWORD err, size; 1273 | if (lpBuffer == NULL || pcbBuffer == NULL) { 1274 | SetLastError(ERROR_INVALID_PARAMETER); 1275 | return FALSE; 1276 | } 1277 | size = *pcbBuffer; 1278 | // coverity[returned_null] 1279 | walloc(lpBuffer, size); 1280 | ret = GetUserNameW(wlpBuffer, &size); 1281 | err = GetLastError(); 1282 | if (ret) { 1283 | *pcbBuffer = (DWORD)wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, size); 1284 | if (*pcbBuffer == 0) 1285 | err = GetLastError(); 1286 | else 1287 | // Reported size includes the NUL terminator 1288 | (*pcbBuffer)++; 1289 | } 1290 | wfree(lpBuffer); 1291 | SetLastError(err); 1292 | return ret; 1293 | } 1294 | 1295 | static __inline BOOL GetVolumeInformationU(LPCSTR lpRootPathName, LPSTR lpVolumeNameBuffer, 1296 | DWORD nVolumeNameSize, LPDWORD lpVolumeSerialNumber, LPDWORD lpMaximumComponentLength, 1297 | LPDWORD lpFileSystemFlags, LPSTR lpFileSystemNameBuffer, DWORD nFileSystemNameSize) 1298 | { 1299 | BOOL ret = FALSE; 1300 | DWORD err = ERROR_INVALID_DATA; 1301 | wconvert(lpRootPathName); 1302 | // coverity[returned_null] 1303 | walloc(lpVolumeNameBuffer, nVolumeNameSize); 1304 | // coverity[returned_null] 1305 | walloc(lpFileSystemNameBuffer, nFileSystemNameSize); 1306 | 1307 | ret = GetVolumeInformationW(wlpRootPathName, wlpVolumeNameBuffer, nVolumeNameSize, 1308 | lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags, 1309 | wlpFileSystemNameBuffer, nFileSystemNameSize); 1310 | err = GetLastError(); 1311 | if (ret) { 1312 | if ( ((lpVolumeNameBuffer != NULL) && (wchar_to_utf8_no_alloc(wlpVolumeNameBuffer, 1313 | lpVolumeNameBuffer, nVolumeNameSize) == 0)) 1314 | || ((lpFileSystemNameBuffer != NULL) && (wchar_to_utf8_no_alloc(wlpFileSystemNameBuffer, 1315 | lpFileSystemNameBuffer, nFileSystemNameSize) == 0)) ) { 1316 | err = GetLastError(); 1317 | ret = FALSE; 1318 | } 1319 | } 1320 | wfree(lpVolumeNameBuffer); 1321 | wfree(lpFileSystemNameBuffer); 1322 | wfree(lpRootPathName); 1323 | SetLastError(err); 1324 | return ret; 1325 | } 1326 | 1327 | #ifdef __cplusplus 1328 | } 1329 | #endif 1330 | -------------------------------------------------------------------------------- /src/version.rc: -------------------------------------------------------------------------------- 1 | // 2 | // This version file should NOT be edited in Visual Studio 3 | // 4 | #pragma code_page(65001) 5 | 6 | #include 7 | #include 8 | 9 | #ifdef RC_INVOKED 10 | 11 | #ifdef _DEBUG 12 | #define VER_DBG VS_FF_DEBUG 13 | #else 14 | #define VER_DBG 0 15 | #endif 16 | 17 | #define _STRINGIFY(x) #x 18 | #define STRINGIFY(x) _STRINGIFY(x) 19 | 20 | #ifndef APP_FILE_VERSION 21 | #define APP_FILE_VERSION 0,0,0,0 22 | #endif 23 | #define APP_FILE_VERSION_STR STRINGIFY(APP_FILE_VERSION) 24 | #ifndef APP_COMMENTS 25 | #define APP_COMMENTS_STR "" 26 | #else 27 | #define APP_COMMENTS_STR STRINGIFY(APP_COMMENTS) 28 | #endif 29 | 30 | VS_VERSION_INFO VERSIONINFO 31 | FILEVERSION APP_FILE_VERSION 32 | PRODUCTVERSION APP_FILE_VERSION 33 | FILEFLAGSMASK VS_FFI_FILEFLAGSMASK 34 | #ifdef _DEBUG 35 | FILEFLAGS VS_FF_DEBUG 36 | #else 37 | FILEFLAGS 0x0L 38 | #endif 39 | FILEOS VOS_NT_WINDOWS32 40 | FILETYPE VFT_APP 41 | FILESUBTYPE VFT2_UNKNOWN 42 | BEGIN 43 | BLOCK "StringFileInfo" 44 | BEGIN 45 | BLOCK "040904b0" 46 | BEGIN 47 | VALUE "Comments", APP_COMMENTS_STR 48 | VALUE "CompanyName", "Akeo Consulting" 49 | VALUE "FileDescription", "Base Console" 50 | VALUE "FileVersion", APP_FILE_VERSION_STR 51 | VALUE "InternalName", "base-console.exe" 52 | VALUE "LegalCopyright", "Copyright © 2020-2024 Pete Batard " 53 | VALUE "OriginalFilename", "base-console.exe" 54 | VALUE "ProductName", "Base Console" 55 | VALUE "ProductVersion", APP_FILE_VERSION_STR 56 | END 57 | END 58 | BLOCK "VarFileInfo" 59 | BEGIN 60 | VALUE "Translation", 0x9, 1200 61 | END 62 | END 63 | #endif 64 | --------------------------------------------------------------------------------