Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-21
4
Medium Priority
?
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

618 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