Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 128
NotAlone Challenge 20 83
firstChar challenge 13 115
Thin secure Windows 10 5 94
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 …
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.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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