4

A bit of history... I have 3 systems that I spend time on, a DOS 6.22 system, a Windows 95 system, and a modern Windows 7 (64-bit) system. When I upgraded to Win7-64, some of my favorite command line utilities stopped working, so I decided to re-write them myself. The only 2 compilers I have are Borland Turbo C++ 3.0 and Visual Studio 2008, and they worked fine for building 2 versions, a DOS 16-bit, and a Windows 7 32-bit (could have built 64-bit too, I guess.) The problem came with my Win95 system. The DOS version works fine there, but since I spent the time to support LFNs in the Win7 build, I wanted it with my Win95 system. So, after a lot of research, I found and purchased Visual Studio 6 (last one with Win95 support according to what I researched,) copied the code over (had to rewrite sections, of course,) and it compiled just fine, and works :)

The problem occurred the next time I had to boot my Win95 system in DOS mode. The program stopped working (of course,) because Win95 wasn't loaded. I don't really want to have 2 copies of the program installed (needing 2 different file names,) so I was hoping there was a way to link the 2 versions together into one file. If I execute it in DOS, instead of it saying it requires windows, it would just jump to the DOS section of the program. That way, it would be a single program, with LFN support if Win95 is loaded, and without if Win95 isn't loaded. Since the Win95 version also works fine in Win7-64, it would probably also produce a single version that works on all 3 systems (which would be an added bonus.)

I did some web searches, and couldn't find anything germane to what I'm looking for. So I have no idea if it is even possible. I may have to get yet another compiler, but considering how old it would have to be, I could probably afford it. My web searches did result in information that leads me to believe that it "should" be possible, though. It would just require a different exe header than the one Windows compilers put in. It may require that I re-write the DOS version for 32-bit and use a DOS extender (for protected mode, assuming I can't find a way to include it in the file itself.) That would be acceptable (though not ideal.) I would much rather have 16-bit code in the DOS section, and 32-bit code in the Windows section (for the most compatibility.)

Does anyone have any information about something like this? If you could just point me in the right direction it would be greatly appreciated.

5
  • 1
    I don't think DOS has the support for executables of that kind. Apr 9, 2014 at 22:55
  • This is off-topic. Stack Overflow is about programming. Try superuser.com.
    – Adi Inbar
    Apr 9, 2014 at 23:19
  • 3
    Set your DOS program as the stub for the Win32 program. Apr 9, 2014 at 23:33
  • 1
    @AdiInbar: this is a programming question. Apr 10, 2014 at 0:51
  • @AdiInbar: I don't know how this question could be more about programming. You don't have to reference a programming language, or quote some code, to ask a programming question. Apr 10, 2014 at 1:12

3 Answers 3

3

I don't know if it has been continued in Windows 7 executables, but back in Win95 the executable (EXE) actually had two entry points -- one "normal" one that DOS would find, and a second one that Windows would use. The DOS entry point was usually a very simple default that would just print "This is a Windows program" and exit. You can actually override this default, and have the linker use your own code, however it is very limited.

What I'd recommend doing is add logic to your DOS 6.22 version (e.g. "sed") that would check the OS level & if it meets the right criteria, pass the parameters along to a second executable (e.g. "sedx") that uses features from the "newer" OS.

6
  • Limited how? I'm not aware of any limitations on DOS code embedded in a Windows program. Also, the solution you describe won't work on 64-bit versions of Windows, because they can't run DOS programs. Apr 10, 2014 at 0:50
  • When I was working on Win 3.1, the stub was limited to 64k of code, however this may have been an issue with the toolset rather than the OS.
    – Shawn C
    Apr 10, 2014 at 1:08
  • The PE format allows the size of the DOS part to be up to 4GB. However, the DOS executable format limits the executable part of a DOS .EXE to 32K, so I can see that a toolset developer might impose such a restriction if they didn't think too hard about the cases where it would be inconvenient. (If the executable file contains extra data, it would be easier for the DOS code to locate and read it if it immediately follows the DOS code in the file.) Apr 10, 2014 at 3:31
  • Well, none of the programs I'm working on should be more than 64k. The one I'm finishing up right now is only 35K. I'm trying to get the /stub solution working (header problem with TC++ 16-bit build.) Apr 10, 2014 at 3:49
  • Actually "very limited" may be about right. If you're writing all of the code yourself, i.e., in assembler, with a compatible linker, you're OK; but if you're using a high level language you're at the mercy of your build tools - unless they specifically support building DOS stubs, there's no guarantees. +1. Apr 10, 2014 at 4:33
2

The documentation for Visual Studio 6 describes the /STUB option here, simply point this at the DOS version of your program.

I don't have VS6 handy, so I can't be too specific, but in the project settings GUI, there should be an "additional options" setting in the linker section.

6
  • Get a link error because Turbo C doesn't build a proper header. Gives a way to fix it with VS 16-Bit linker. As it is now, it crashes when in DOS mode, so I guess I'm looking for an even older (but not too old, 16-bit linker v5.5 or later,) Visual Studios. TLINK doesn't have a /KNOWEAS option it seems. Apr 10, 2014 at 3:46
  • I don't know whether or not you can use Microsoft's DOS linker on Turbo C object files. Does the DOS executable run properly before you insert it into the Windows executable? Apr 10, 2014 at 4:18
  • It's also possible that Turbo C generates code which reads from the executable file, and is being confused by the extra content. Apr 10, 2014 at 4:31
  • Another option, I suppose, though hardly ideal, would be to write a custom stub in assembler which launches your actual DOS executable. (A more advanced version of this would be for the stub to implement a DOS .EXE loader allowing you to embed the DOS executable into the Windows file, but this may be more trouble than it is worth - unless someone else has already done it. Could be worth looking.) Apr 10, 2014 at 4:40
  • 1) yes it works just fine before becoming a stub. 2) Thanks to MSDN used VC1.52c to re-build the exe (had to compile from the source code not obj, just took a little re-writing,) and the resulting made a stub that installed and worked just fine. 3) While it worked, it now runs out of memory extremely fast, making it useless. So, I would need a way to clear the "windows" code out of memory before the DOS code actually starts, (no memory issue if the windows code runs.) Would rather lose LFN in Win95 than have 2 separate files. Guess I'm just out of luck. Thanks. Apr 10, 2014 at 8:47
1

Well the answer is the /stub option in the Linker you are using for your Windows code. Some additional information for anyone who finds the question later.... I had to do several days of web searches to find that there doesn't appear to be another answer to my particular problem.

Stub requires that the DOS mode executable have a header of at least 40 bytes. After fighting with multiple compilers that "DO" give you a header of the right size (Borland Turbo C++ won't,) and not being able to convert my code, I had to get sneaky/fancy. BTW - Visual C 1.52c (last Visual C that supports DOS,) will make a correct header, as will Open WatCom.

If you are faces with the same issue I was - the compiler you used won't make the correct size header, and your code is too compiler specific to convert easily, you can do what I ended up doing. I used Open WatCom to write a tiny ("Hello World") Windows program using my exe with the short (Borland created,) header as the stub. Open WatCom will adjust the header automatically. I then used a Hex Editor to read the header information to get the ending address of the stub and a partial file copier to copy only that part of the program to a file I named "stub.exe" (stripping of the Windows code.) Using the same Hex Editor I zeroed out the PE pointer in the header. I now had a working DOS exe that would also work as a stub. Took my stub to my Windows compiler, and linked it in. It works great, all features fully realized :)

FYI - Information needed to strip the Windows portion and zero the PE pointer.

first byte is offset 0 (of course, but some people may not realize that, and think it's byte 1.) Also remember, that most Hex Editors (by their very name,) are giving you numbers in hexadecimal format.

offset 2 & 3, number of bytes in the last block of the DOS portion of the file in low byte - high byte format. That is, offset 2 is low, 3 is high. So take them, reverse them, and you will get a number from 0 - 511 (0 - 1ff in hex.) 0 means the entire block of 512 (200 in hex) bytes is used.

offset 4 & 5 (again in low/high format,) is the number of 512 (200 in hex) byte block in the DOS portion. Remember to reverse the number, and that the last block may only be a partial block. So, subtract one, multiply by 512 (200 hex,) add the number from 2-3, and you have how many bytes are in the DOS portion. Since you are starting from 0, subtract 1, and you now know to only copy bytes 0 - "whatever the total is" to your stub exe.

offset 60-61 (hex 3C-3D) is the pointer to the start of the PE (or Portable Executable,) portion of the code (the part that Windows jumps to.) It should be just past (mine was padded with a few zeroes,) the end of the DOS portion of the code. This isn't important at this time, as we are just turning those into 0's anyway (the PE portion has been stripped.) You can use this as confirmation that you have the correct "end of DOS" offset selected though.

The tools I used are: Open WatCom at http://www.openwatcom.org/index.php/Main_Page and Part Copy at http://www.virtualobjectives.com.au/utilitiesprogs/partcopy.htm

I have no idea where to find the Hex Editor I used. I used CEdit, a DOS program I really like, but have been unable to find on the net. Have to use DOSBox with it as Win7 won't run it, though. There are probably other compilers that do the same thing, and probably tons of partial file copiers available. These are the tools I used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.