HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Wormhole - a Windows shim app written in C with no stdlib

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wormholewrittenappwithstdlibwindowsshim

Problem

My application compiles and seems to work correctly, but this is my first C project so I welcome any criticism. I developed this in Pelles C with no standard lib. Efficiency and optimizations are more important to me than readability or conformity. The parts that I'm particularly concerned about are marked with comments including ~~~~~~~~.

The purpose of this program is to run another program somewhere else. Let's say, for example, that you want to be able to run LibreOffice from the run dialog or command line. Your options are to add the LibreOffice folder to your path, install LibreOffice to a folder in your path (really bad idea), or use some other trick like putting a bat file in your path that will run it. With Wormhole, you could copy wormhole.exe to somewhere in your path, rename it to libreoffice.exe, rename wormhole.ini to libreoffice.ini, and edit the ini file to point to the original libreoffice.exe. Now if you run libreoffice.exe, the shim will run libreoffice from its original location.

```
/*
| Wormhole - A windows shim app
|
| Wormhole.c
|
| Copyright 2015 by Tim Byles / x79
| All Rights Reserved
|
| This software is distributed under the GPLv3
| The full text of the license can be found here
| http://www.gnu.org/licenses/gpl.txt
*/

#include
#include

#define WIN32_LEAN_AND_MEAN
#pragma comment(linker,"/merge:.rdata=.data")
#pragma comment(linker,"/merge:.text=.data")
#pragma comment(linker,"/merge:.reloc=.data")

//#define debugme

//wchar_t myitow(int __value, wchar_t __string, int __radix);
//WINBASEAPI HMODULE WINAPI GetProcAddressW(HMODULE, LPCWSTR);

int main(void) {
// Disable Wow64 Redirection
// Found this trick at some google result
// We declare the api function as a pointer so the app doesn't go looking for the entry point until we need it
typedef BOOL WINAPI fn64();
fn64 pfn64 = (fn64)GetProcAddress(GetModuleHandleA("kernel32.dll"),
"Wow64DisableWow64FsRedirection");
if (pfn64)
(*pfn64)(NU

Solution


  • Is there some reason why you aren't using the Win32 API routines to read from an existing INI file? GetPrivateProfileString() and the like?



  • When you're reallocating a buffer where you don't know the actual size, you're supposed to double its size each time instead of increasing by a fixed amount (use iTmp *= 2; instead of iTmp += 8;)



  • Instead of if ((sEndPtr - sStartPtr)



  • Instead of the switch (lPriority)`, put all the strings in an array and just concatenate on the proper one.

Context

StackExchange Code Review Q#83774, answer score: 3

Revisions (0)

No revisions yet.