Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-11-21
4
Medium Priority
?
466 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 150 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 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

810 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