Best windows questions in March 2011

Why are "Executable files" operating system dependent ?

12 votes

I understand that each CPU/architecture has it's own instruction set, therefore a program(binary) written for a specific CPU cannot run on another. But what i don't really understand is why an executable file (binary like .exe for instance) cannot run on Linux but can run on windows even on the very same machine.

This is a basic question, and the answer i'm expecting is that .exe and other binary formats are probably not Raw machine instructions but they contain some data that is operating system dependent. If this is true, then what this OS dependent data is like? and as an example what is the format of an .exe file and the difference between it and Linux executables?

Is there a source i can get brief and detailed information about this?

In order to do something meaningful, applications will need to interface with the OS. Since system calls and user-space infrastructure look fundamentally different on Windows and Unix/Linux, having different formats for executable programs is the smallest trouble. It's the program logic that would need to be changed.

(You might argue that this is meaningless if you have a program that solely depends on standardized components, for example the C runtime library. This is theoretically true - but irrelevant for most applications since they are forced to use OS-dependent stuff).

The other differences between Windows PE (EXE,DLL,..) files and Linux ELF binaries are related to the different image loaders and some design characteristics of both OSs. For example on Linux a separate program is used to resolve external library imports while this functionality is built-in on Windows. Another example: Linux shared libraries function differently than DLLs on Windows. Not to mention that both formats are optimized to enable the respective OS kernels to load programs as quick as possible.

Emulators like Wine try to fill the gap (and actually prove that the biggest problem is not the binary format but rather the OS interface!).

Simple way to have the GHC API for application deployed on Windows

9 votes

I want to deploy an application on Windows that needs to access the GHC API. Using the first simple example from the Wiki:

http://www.haskell.org/haskellwiki/GHC/As_a_library

results in the following error (compiled on one machine with haskell platform and executed on another clean windows install): test.exe: can't find a package database at C:\haskell\lib\package.conf.d

I'd like to deploy my application as a simple zip file and not require the user to have anything installed. Is there a straightforward way to include the needed GHC things in that zip file so it will work?

This program will copy necessary files to the specified directory (will work only on windows):

import Data.List (isSuffixOf)
import System.Environment (getArgs)
import GHC.Paths (libdir)
import System.Directory
import System.FilePath 
import System.Cmd

main = do
  [to] <- getArgs
  let libdir' = to </> "lib"
  createDirectoryIfMissing True libdir'
  copy libdir libdir'
  rawSystem "xcopy"
    [ "/e", "/q"
    , dropFileName libdir </> "mingw"
    , to </> "mingw\\"]


-- | skip some files while copying
uselessFile f
  = or $ map (`isSuffixOf` f)
    [ "."
    , "_debug.a"
    , "_p.a", ".p_hi"    -- libraries built with profiling
    , ".dyn_hi", ".dll"] -- dynamic libraries


copy from to
  = getDirectoryContents from
  >>= mapM_ copy' . filter (not . uselessFile)
  where
    copy' f = do
      let (from', to') = (from </> f, to </> f)
      isDir <- doesDirectoryExist from'
      if isDir
          then createDirectory to' >> copy from' to'
          else copyFile from' to'

After running it with destination directory as argument you will have a local copy of lib and mingw (about 300 Mb total).

You can remove unused libraries from lib to save more space.

When is memory, allocated by .NET process, released back to Windows

8 votes

The Setup

.NET allocates memory for each generation’s heap (0, 1, 2, LOH) in segments to get a continuous block of memory, on startup, and when it attempts to satisfy an allocation request, after a collection.

This memory allocated for each heap will likely level off as the application “warms up”, except potentially for generation 2, and large object heap. During a garbage collection, each heap (0, 1, 2) is swept and compacted, except for the large object heap (LOH), which is just swept.

I understand the ‘sweep’ part of a collection to mean that the GC identifies which objects are no longer rooted and are available for collection (or finalization) and that ‘compact’ means that the addresses that are still alive in a heap are reorganized so that the available remaining heap has more continuous memory available to it.

As the budget for each segment within the heap is exceeded, .NET will allocate another segment in order to fulfill allocations if it can.

The Question

My question comes down to what happens to that memory in each heap, that is not be used by the application (committed) any longer, but is still reserved by .NET? When is it released back to the OS?.

I believe this to be the scenario where a process might appear to be consuming a lot of memory (virtual size is quite large, but private bytes small), but when inspecting its heaps are mostly free space. As another caveat, the total size of the heaps may also be quite small, and not account for the memory being consumed by the process.

There is no blocked finalizer and all looks healthy for a process - it may have been running for weeks before it triggered a monitor alert (e.g.).

Trying for further clarification of the question, if you read Tess .NET Memory Management - A Restaurant Analogy, if the tables are heap segments, does the restaurant ever lose tables (e.g. free heap segments)?

Edit

  1. Removed confusing reference to working set and chickens
  2. Added reference to Tess restaurant analogy

My answer is - it doesn't matter. The OS gives the application (within which the .NET runtime runs) virtual memory. This is not 'real' memory. The OS can put each page of virtual memory wherever it likes - on the processor, in main memory, on the disk. So, it is possible for an app to use more memory than the amount of RAM on the system, and the OS will copy the bits that are needed to/from disk to ensures the app keeps running (modulo certain addressing & technical limitations).

The OS manages the virtual memory of all the processes on the system, and ensures that one program doesn't hog all the RAM on the system to the detriment of other programs. When the .NET runtime asks for memory from the system for use in the heaps, but then doesn't use it, this memory will (if the system is low on free RAM) be moved to disk, because it isn't being accessed.

Clarification via email from Tess: (emphasis mine)

The segment sizes stay the same throughout the course of the application, but there are two things to consider here.

  1. The allocations for a segment is a virtual allocation, meaning that while we reserve virtual memory, we only commit what we actually use, so the private bytes used for a segment is not the same as the segment size, this means that after a GC, your private bytes will go down, while your virtual bytes will stay the same.

  2. When a segment is no longer used, i.e. if you happen to GC everything in a segment so that it no longer contains any .net objects, the virtual alloc is returned back to the OS.

Taking that on faith, then heap segments (restaurant tables) will be returned back to the OS.

How to compile LEX/YACC files on Windows?

7 votes

I'm having Lex and YACC files to parse my files (.l file and .y file)

How to compile those files and how to make equivalent .c file for this

Which tool i've to for this.

I'm using windows 7 , i've seen many tools for unix/linux environment. But is there any tool for windows platform ?

As for today (2011-04-05) you will need the lastest versions of:

  1. flex-2.5.4a-1.exe

  2. bison-2.4.1-setup.exe

  3. After that, do a full install in a directory of your preference without spaces in the name. I suggest C:\GnuWin32. Do not install it in the default (C:\Program Files (x86)\GnuWin32) because bison has problems with spaces in directory names, not to say parenthesis.

  4. Also, consider installing Dev-CPP in the default directory (C:\Dev-Cpp)

  5. After that, set the PATH variable to include the bin directories of gcc (in C:\Dev-Cpp\bin) and flex\bison (in C:\GnuWin32\bin). To do that, copy this: ;C:\Dev-Cpp\bin;C:\GnuWin32\bin and append it to the end of the PATH variable, defined in the place show by this figure:
    step-by-step to set PATH variable under Win-7.
    If the figure is not in good resolution, you can see a step-by-step here.

  6. Open a prompt, cd to the directory where your ".l" and ".y" are, and compile them with:

    1. flex hello.l
    2. bison -dy hello.y
    3. gcc lex.yy.c y.tab.c -o hello.exe

Commands to create lexical analyzer, parser and executable.

You will be able to run the program. I made the sources for a simple test (the infamous Hello World):

Hello.l

  %{

  #include <stdlib.h>
  #include "y.tab.h"

  %}

  %%

  ("hi"|"oi")"\n"       { return HI;  }
  ("tchau"|"bye")"\n"   { return BYE; }
  .                     { yyerror();  }

  %%

  int main(void)
  {
     yyparse();
     return 0;
  }

  int yywrap(void)
  {
     return 0;
  }

  int yyerror(void)
  {
      printf("Error\n");
      exit(1);
  }

Hello.y

%token HI BYE

%%

program: 
         hi bye
        ;

hi:     
        HI     { printf("Hello World\n");   }
        ;
bye:    
        BYE    { printf("Bye World\n"); exit(0); }
         ;

DUnit Compare Two Text Files and show Diff

6 votes

Is there a way to compare two text files and show the diff if they are not identical in dunit?

The easy start is to read them to TStringList, however the code for comparing two text file is much more complicated, and the gui in the DUnitGui is not sufficient for this.

Any idea? suggestion?

There is a nice little unit that comes with some examples called TDiff, this is available from http://angusj.com/delphi/ and will allow you to compare 2 files and see the differences, it also allows for merging.

It is a very simple Utility that you can download the entire source for.

Detect when Windows rebooted after crash or powerfailure

6 votes

Do anyone know if its possible to detect if the last boot up was preceded by a power-failure in Windows ?

My reason for wanting to detect this is to know when to re-scan files I frequently modify on disk with my service.

If there is a normal startup, I can be quite sure that the shutdown went smooth and the data that was in in-memory file buffers was flushed to disk.

As a simple implementation, would CreateFile with FILE_FLAG_DELETE_ON_CLOSE (immdediately followed by FlushFileBuffers just to be sure) not work?

If the system shuts down cleanly, your application will exit and the file handle will be closed, so the OS will delete the file.

If power fails or the world ends, the OS doesn't get a chance to close any handles or delete any files. Thus, the file will still be present after the system comes up again.

Communication between applications

6 votes

I have 3 choices to use : sockets, activeX, com , in order to communicate between applications on one computer. Which is faster ?

As long as this runs on one machine, interprocess communication is fundamentally throttled by the bus bandwidth. A memory-to-memory copy, whether that's done in the TCP/IP stack, the named pipe support code or shared memory. Which makes them all equally efficient.

One detail matters though, the amount of data that's transferred and the number of software layers you travel through to get the job done. The memory bus bandwidth only throttles when the amount of data is large. That isn't necessarily the case for a remote procedure call protocol like COM. Only the arguments of the function call needs to be serialized, that could be only a handful of bytes if you don't pass arrays. Now the overhead starts to matter, there's a fair amount of it when you use a high-level protocol like COM.

The obvious disadvantage of using sockets is that you'll have to write all the de/serialization code yourself. Nontrivial if the protocol with the component isn't simple. Trading your working hours for convenience is the typical choice, only you can make it.

Bind to 127.0.0.2

6 votes

Hi,

I'm running a client/server application locally on my Windows XP PC and for testing purposes I want to run multiple clients.

The server has a configuration file containing the IP addresses of the clients that can connect; in the real world, these would all be on separate hosts with separate IP addresses.

Currently I am able to test locally with a single client which binds to 127.0.0.1 however because I can only have one client-IP mapping in the server configuration (that's how the system works and can't be redesigned!) I can only run one client on my development PC.

I've tried to start another client application bound to 127.0.0.2 connecting to the server which is bound to 0.0.0.0 however the server thinks that the client is connecting from 127.0.0.1 again and so rejects what it believes is a second connection from the first client.

Can anyone suggest a way to get around this problem? I believe I could run one more client bound to the external IP address of the PC but I'd really like to be able to run multiple.

I know I could use VirtualBox or similar to run new instances but I'd like all of the client applications to be running in the Visual Studio debugger.

Any help greatly appreciated!

Nick.

PS. Not sure if it matters but the applications are written in C++ using standard winsock sockets.

You might be able to create more loopback interfaces. See the chosen answer to How do you create a virtual network interface on Windows?

How to quickly test Windows Gadgets?

6 votes

How should one quickly test a Windows Gadget during development? Creating the archive, installing it and adding it to the desktop every time is extremely tedious...

This application might speed up the process:

http://www.codeproject.com/KB/gadgets/GadgetPacker.aspx

Whilst developing a desktop gadget for Windows 7, it became apparent how much of a pain it was going through the build process. I decided it would be a lot easier to write a simple automation tool, to take care of the necessary steps. This would speed up the overall developing and testing phase.


This article might also be helpfull

http://www.howtogeek.com/howto/windows-vista/how-to-debug-a-windows-vista-sidebar-gadget-with-visual-studio/

Windows SDK parameter annotations

6 votes

I am wondering if its important to use the the annotations when doing Windows development in C++? For instance,

#include <windows.h>
int WINAPI WinMain(
    __in HINSTANCE hInstance,
    __in HINSTANCE hPrevInstance,
    __in LPSTR lpCmdLine,
    __in int nCmdShow
){
    return 0;
}

This can be written as:

#include <windows.h>
int WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
){
    return 0;
}

What am I losing by not using them? I am having a difficult time figuring out what these are for, there doesn't seem to be any guides written for mortals around.

You must be working from an older version of the SDK. Version 7.0 declares the arguments like this:

WinMain (
    __in HINSTANCE hInstance,
    __in_opt HINSTANCE hPrevInstance,
    __in LPSTR lpCmdLine,
    __in int nShowCmd
    );

Note the __in_opt annotation, it marks the argument as optional, indicating that passing NULL is acceptable. These are an early version of SAL annotations, an acronym for Source code Annotation Language. There's an MSDN article for them, it however documents the syntax that's used in the C/C++ library #include files. Not quite sure why the SDK group doesn't use the same, they tend to be a bit slow to catch up.

Short from making the declarations more readable, removing the ambiguity of C declarations, the annotations are also useful to tools. Good examples are the Code Analyzer that's built into the higher SKUs for VS2008 and VS2010 (it catches programming bugs). And the P/Invoke Interop Assistant, a tool that generates C# or VB.NET p/invoke declarations using a dbase that was generates from the annotated SDK header files. The annotations are essential to generate good C# declarations.

You can use these annotations in your own code as well, it will automatically be verified by the Code Analyzer if you do. Do use the modern syntax as documented in the MSDN article. I think the required sal.h header gets pulled in on just about any source file that #includes CRT headers.

Read data from Wii-balanceboard

5 votes

Im trying to get the output from a Wii Fit (balance board). I can find the device via the C++ Bluetooth enumerators but attempts to connect via a windows socket are failing.

Im wondering if anyone has had success in such a direction (C++, windows) I'd love to hear how you did it.

The Wii Balance Board is a HID device. To understand communications with the Balance Board, you need to know a little bit about Bluetooth HID. There's good information on WiiBrew about the Wiimote and the Wii Balance Board.

I don't think you can use Windows Sockets to connect to a HID device. The Windows Socket Bluetooth interface only allows you to connect to RFCOMM-based services. HID is based on L2CAP which is further down the Bluetooth protocol stack. There's some information at MSDN about that limitation.

If you're just looking to hack around with it, you can try Bluecove for Java. You may be able to do something using another stack (for example, Widcomm). If you're locked in to Windows and C++, you may have to look into writing an custom HID driver for the Balance Board.

How does Windows Hibernation works

5 votes

Out of curiosity, I was looking for an article/documentation on "how windows hibernate option work", i.e. when one selects "Hibernate" option in windows shutdown dialog. The reply I got from some sources was that, its mere serialization of memory and registers.

Pardon me if I am wrong here. If windows could serialize any applications, process or objects regardless of whether its serializable or non-serializable, how come .NET limits serializable objects to those with [Serializable] attribute or ISerializable interface?

Inside a process address-space, everything is just bytes; some stack, some managed heap, etc. Bytes are inherently serializable - they are just bytes. All hibernate has to do is suspend the threads and write the entire address space to disk.

With objects, you want to save them to some out-of-memory structure. Unfortunately, it makes no sense to store addresses etc, as it is exceptionally unlikely to rehydrate into exactly the same point in memory. Additionally, many things like unmanaged object handles will make no sense when rehydrated. It is also extremely likely that you want to save just a small block of objects, not an entire process space. And even in a small graph, those objects could be scattered all over the place - so you can't just copy out a few pages of memory.

Also keep in mind that a common use of serialization is to deep-clone objects; if you relied on the in-memory representation of objects, you would have to deserialize to exactly the same place in memory - so you can't have cloned anything. And that is *before you touch on concepts such as compacting garbage collectors, which move objects around in memory while you aren't looking.

Also consider that you might be loading the data into a different platform / architecture, or want to write a specific format (xml, json, etc).

So instead of just copying raw memory, serialization code must look at individual objects, traversing references and writing an object graph in a way that allows rehydration from a source that has nothing at all to do with raw memory. Much harder.

Least annoying way to check auto updates in native app

5 votes

What would be the best approach?

So far I can think of:

  • Super small WinMain exe in HKLM\Run that checks, say, twice a day

  • Windows service that checks, say, twice a day

  • Scheduled tasks (can't seem to find a way to start GUI task via user account, desktop isolation and stuff)

  • Application itself (doesn't work if there are multiple exes, for example, as in Sysinternals Suite, many, many applications, one large suite)

Anything else? I want it to stay native and avoid being obtrusive or delay startup of the cold boot.

Larry Osterman posted something about this on his blog awhile back. The basic points:

  • Twice a day is WAY too often to check for updates
  • Windows Service is a terrible idea
  • So is a small constantly running WinMain
  • Make sure the user can turn it off easily. Otherwise you're just going to annoy a large subset of your users

Crashed "In An Unusual Way"

5 votes

Over the years, I've seen C++ applications the employ the "unusual way" language in a crash. For example:

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

The first of these I debugged, it had something to do with a destructor being fired for a class, in an inheritance chain, that already had been deleted. I can't remember the particulars better than that. When I google this topic, I find one or two other suggested causes. For example, that the binaries themselves have become corrupt and must be re-written.

Overall, I find the information on this kind of crash to be much too thin. I'd like to see two things:

  1. A formal explanation of why this type of termination exists (and which Windows platforms, if relevant).
  2. A list of all reasons, or at least the common reasons, why this type of crash occurs.

Anyone know where this information can be found? Can anyone provide this information?

This dialog is produced by the visual studio runtime, in response to abort(). abort() is by default called by e.g. terminate(). You'll get this from e.g. unhandled c++ exceptions, call to pure virtuals, failed assertions.

So, it's not platform dependent, but run-time library dependent. abort() is, by the c++ standard, required to terminate the program without executing destructors for automatic and static storage objects, and without calling atexit() handlers.

Can you cast a LPTSTR to a BSTR?

4 votes

Is it legal to cast a LPTSTR directly to a BSTR?

Based on my understanding of BSTR, casting a LPTSTR to a BSTR directly will leave you with a corrupted length prefix. The example code explicitly states that a string literal cannot be stored to a BSTR. Can anyone confirm for me that a LPTSTR/LPCTSTR cannot be cast directly to a BSTR without corrupting the length prefix?

EDIT:

My confusion is from seeing this used in a call to a COM object. It turns out that when compiling the COM dll, a .tli file is generated that creates an intermediate method. This method takes type _bstr_t. The _bstr_t can take LPTSTR in its constructor, so everything works smoothly.

If your program is unicode and your LPTSTR therefore is a LPWSTR, you can use SysAllocString to convert from a pointer to a wide character string to BSTR.

A direct cast is not possible because the two have different memory representations.

If you use C++, you can use the _bstr_t class to simplify the usage of BSTR strings.

Getting CD Drive letter in VB.Net

4 votes

I am using the following code to get a list of the letters for each drive on my computer. I want to get the drive letter of CD Drive from this list. Please advise how to check it.

The code I am using to get list is as below:

In the Form.Load event:

    cmbDrives.DropDownStyle = ComboBoxStyle.DropDownList
    Dim sDrive As String, sDrives() As String
    sDrives = ListAllDrives()
    For Each sDrive In sDrives
    Next
    cmbDrives.Items.AddRange(ListAllDrives())

Public Function ListAllDrives() As String()
    Dim arDrives() As String
    arDrives = IO.Directory.GetLogicalDrives()
    Return arDrives
End Function

Tested, and returns the correct results on my computer:

Dim cdDrives = From d In IO.DriveInfo.GetDrives() _
                Where d.DriveType = IO.DriveType.CDRom _
                Select d

For Each drive In cdDrives
    Console.WriteLine(drive.Name)
Next

Assumes 3.5, of course, since it's using LINQ. To populate the list box, change the Console.WriteLine to ListBox.Items.Add.