Solved

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

Posted on 2006-11-21
4
442 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

19 Experts available now in Live!

Get 1:1 Help Now