Solved

GetDriveType(..) does not work well with GetModuleFileName(..), why?

Posted on 2006-11-21
4
437 Views
Last Modified: 2013-11-20
I am trying to prevent executing program from other than hard drive.

The following works well:

int CSysInfoDlg::CheckDriveType()
{
      
      GetCurrentDirectory(MAX_PATH,currentDir);

      AfxMessageBox(currentDir); // shows right path!
      
      unsigned int DriveType = GetDriveType(currentDir);

      CString strDriveType;
      strDriveType.Format("%d",DriveType);

      AfxMessageBox(strDriveType);    // returns 2  when launched from floppy !?

      if(DriveType == 1)
            {
            AfxMessageBox("Hard drive.. continue!");
            }
            else if(DriveType > 1)
                  {
                  AfxMessageBox("You must install this application on your hard drive!");
                  return 0;            
                  }

      return (-1);
}



The following does not work right:

int CSysInfoDlg::CheckDriveType()
{
      
      char currentDir[MAX_PATH];
      GetModuleFileName(AfxGetInstanceHandle(),currentDir,MAX_PATH);

      AfxMessageBox(currentDir); // shows right path!
      
      unsigned int DriveType = GetDriveType(currentDir);

      CString strDriveType;
      strDriveType.Format("%d",DriveType);

      AfxMessageBox(strDriveType);    // returns 1  when launched from floppy !?

      if(DriveType == 1)
            {
            AfxMessageBox("Hard drive.. continue!");
            }
            else if(DriveType > 1)
                  {
                  AfxMessageBox("You must install this application on your hard drive!");
                  return 0;            
                  }

      return (-1);
}

Why?

0
Comment
Question by:khlauster
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
Alkali_Guy earned 50 total points
ID: 17989865
"A trailing backslash is required."

Maybe it will work better in example 2 if you pass it only the first 4 characters of currentDir.

A crude example:
char currentDir[MAX_PATH];
GetModuleFileName(AfxGetInstanceHandle(),currentDir,MAX_PATH);
currentDir[4] = '\0';
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 17989881
Try this:
GetModuleFileName(NULL, currentDir,MAX_PATH);

GetDriveType requires root directory name, like C:\, and not full path. Try to use first three characters from currentDir.
0
 

Author Comment

by:khlauster
ID: 17990636
Well.. the results when running exe on my C(HD) and A(floppy):

in a case of:  currentDir[1] = '\0';
produces ether C   or  A
and delivers DriveType  1(wrong)  for drive C  and  DriveType 1(wrong) for drive A

in a case of:  currentDir[2] = '\0';
produces ether C:   or  A:
and delivers DriveType  3(wrong)  for drive C  and  DriveType 3(wrong) for drive A

in a case of:  currentDir[3] = '\0';
produces ether C:\   or  A:\
and delivers DriveType  3(wrong)  for drive C  and  DriveType 2(right) for drive A
//////////////////////////////////////////////////////////////////////////////////////////////
After all my experementation the following only worked:

      char currentDir[MAX_PATH];

      GetModuleFileName(NULL,currentDir,MAX_PATH);
      CString strPath(currentDir);
      strPath=strPath.Left(strPath.ReverseFind('\\')); need to extract directory only with no file name and extention as it's done by GetCurrentDirectory(..)!


//      GetCurrentDirectory(MAX_PATH,currentDir);

      AfxMessageBox(strPath);

      
      unsigned int DriveType = GetDriveType(strPath);

      CString strDriveType;

      strDriveType.Format("%d",DriveType);
      AfxMessageBox(strDriveType);

My last question is why should I use GetModuleFileName instead of GetCurrentDirectory in order to prevent executing  the program from other than a hard drive?

I'll split the points since you put me on a right way.. :)
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17990802
The current directory isn't necessarily the directory your program is in, although it usually is.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now