Setup AutoPlay CDs
Site Map Feedback
Up Controls Files Moving Data

Make CDs open web pages automatically!

In the root of the CD you create a text file called AutoRun.inf - This file can specify an executable file to run when the CD is inserted. You can make an Icon from the executable file replace the CD Icon in Windows Explorer (the "ICON=Start.exe,0" means that the icon is the first icon in Start.exe). You can also add commands that are visible if the user right-clicks the CD in Windows Explorer: the context Menu contains the text in the shell\start line; and if the menu entry is clicked the executable file in the shell\start\command line is run.

[autorun]
OPEN=Start.exe
ICON=Start.exe,0

shell\Start=&View PSL CD...
shell\Start\command=Start.exe

That's wonderfully easy, until you want to give out a CD which contains a web site or at least starts from a web page! You have to write a program (which becomes "Start.exe" which will open your web page. If the web page is in the root of the CD too and is called "index.htm", Start.exe should open the default Browser and load index.htm

Finding the default browser requires us to look up a couple of values in the registry. HKEY_CLASSES_ROOT\.htm tells us where in the registry to get the browsers path from. So you look up the key you are given (still within HKEY_CLASSES_ROOT) and the browser (full path) is given under "shell\open\command".

Next you need to find the URL of the file you are trying to open to pass to the Browser as a parameter. This is found by calling GetCurrentDirectory and turning what you get into a valid URL. All that's left to do is run the browser with the file and the easiest way (there are several) is WinExec.

So if you just want a CD to run \index.htm automatically, create a new application with
[File Menu][New...][Projects Tab][Win32 Application][Simple Win32 Applcation][Finish]
Paste in the following code and insert an Icon of your choice in the resources and you're done!

#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  HKEY Key;
  if(RegOpenKeyEx(HKEY_CLASSES_ROOT,".htm",0,KEY_READ,&Key)==ERROR_SUCCESS) {
    DWORD dwSize=2*MAX_PATH;
    char Browser[2*MAX_PATH];
    if(RegQueryValueEx(Key,"",NULL,NULL,(BYTE*)&Browser,&dwSize)==ERROR_SUCCESS) {
      RegCloseKey(Key);
      strcat(Browser, "\\shell\\open\\command");
      if(RegOpenKeyEx(HKEY_CLASSES_ROOT,Browser,0,KEY_READ,&Key)==ERROR_SUCCESS) {
        dwSize=2*MAX_PATH;
        if(RegQueryValueEx(Key,"",NULL,NULL,(BYTE*)&Browser,&dwSize)==ERROR_SUCCESS) {
          strcat(Browser," \"file://");
          char* ptr=Browser+dwSize+9-1; // Find the end of the string
          GetCurrentDirectory(MAX_PATH, ptr--);
          while(char c=*++ptr) if(c=='\\') *ptr='/';
          if(*(ptr-1)!='/') {
            *ptr++='/';
            *ptr=0;
          }
          strcat(Browser, "index.htm\"");
          WinExec(Browser, SW_SHOWNORMAL);
    } } }
    RegCloseKey(Key);
  }
  return 0;
}

Of course you can use the same code to open a Web Page from an Application, for example on clicking a Button:
void CMyDlg::OnButton1() {
  HKEY Key;
  if(RegOpenKeyEx(HKEY_CLASSES_ROOT,".htm",0,KEY_READ,&Key)==ERROR_SUCCESS) {
    DWORD dwSize=2*MAX_PATH;
    char Browser[2*MAX_PATH];
    if(RegQueryValueEx(Key,"",NULL,NULL,(BYTE*)&Browser,&dwSize)==ERROR_SUCCESS) {
      RegCloseKey(Key);
      strcat(Browser, "\\shell\\open\\command");
      if(RegOpenKeyEx(HKEY_CLASSES_ROOT,Browser,0,KEY_READ,&Key)==ERROR_SUCCESS) {
        dwSize=2*MAX_PATH;
        if(RegQueryValueEx(Key,"",NULL,NULL,(BYTE*)&Browser,&dwSize)==ERROR_SUCCESS) {
          strcat(Browser," \"http://www.lumpylumpy.com/Electronics/Computers/Software/Cpp/MFC/AutoPlay.php\"");
          WinExec(Browser, SW_SHOWNORMAL);
    } } }
    RegCloseKey(Key);
} }

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1700) Last modified: Mon, 18 May 2009 18:23:52 +0100