8 Replies - 861 Views - Last Post: 27 July 2011 - 03:09 AM Rate Topic: -----

#1 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

C++ API question

Posted 26 July 2011 - 01:56 PM

hi my friends
i want lock one drive such as C:\ . this is my code but doesnt work please help why ?

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>

int main(){
	char szVolume[] = "\\\\.\\C:";
	HANDLE hVolume;
	DWORD dwBytes = 0;
	PREVENT_MEDIA_REMOVAL pmr;

	pmr.PreventMediaRemoval = TRUE;

	hVolume = CreateFile(szVolume,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if(hVolume==INVALID_HANDLE_VALUE){
		exit(1);
	}
	if(DeviceIoControl(hVolume,IOCTL_STORAGE_EJECTION_CONTROL,(LPVOID)&pmr,(DWORD)sizeof(pmr),NULL,0,&dwBytes,NULL)==FALSE){
		exit(1);
	}
	CloseHandle(hVolume);
	return 0;
}



and i dont want use Registry code please help me how can i fix this code to work it

This post has been edited by ahura_24: 26 July 2011 - 01:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ API question

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6490
  • View blog
  • Posts: 23,579
  • Joined: 12-June 08

Re: C++ API question

Posted 26 July 2011 - 02:03 PM

Well - that's some pretty tired looking code you found on the net. If you are trying to lock a drive - why are you using 'c' as your volume? Isn't that typically the hard drive?

It would be helpful to know *how* it is not working. Is there an exception? If so where? What?
Was This Post Helpful? 0
  • +
  • -

#3 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: C++ API question

Posted 26 July 2011 - 02:14 PM

my problem exactly is dont reference about this topic ... its no matter 'C' or other drive i want lock one drive i search about one day but i cant fine reference about this . i search in .net but nothing .... :( i can lock one drive with "RegSetValueEx" but it has a bug dont work correctly. it cant open handle my windows is 7 (64-bit) . if you know how can i lock one drive or how can i fix this code please help me in C++ API or .Net tnx
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: C++ API question

Posted 26 July 2011 - 02:14 PM

What do you mean "lock a drive"? what does that mean?
Was This Post Helpful? 0
  • +
  • -

#5 dwisecup  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 22
  • Joined: 26-July 11

Re: C++ API question

Posted 26 July 2011 - 02:19 PM

Yeah from what your title suggest and "your" code actually says are two different things. There was another post today concerning locking drives and I'm still confused as to what you really want to do. Maybe the .NET API can help: http://msdn.microsof.../system.io.aspx
Was This Post Helpful? 0
  • +
  • -

#6 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: C++ API question

Posted 26 July 2011 - 02:21 PM

no joys can not access to that . can not open , can not read , can not write , ....
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: C++ API question

Posted 26 July 2011 - 02:33 PM

Well I am no expert on the low-level win32 APi but I think you will need to get into Kernel Programming to lock all read/write access to a disk. You might be able to temporarily do it via VSS API but that exists specifically for creating backups of a disk.

Basically though this is a very low-level thing that you are asking for and you will probably have to write your own driver.
Was This Post Helpful? 0
  • +
  • -

#8 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: C++ API question

Posted 26 July 2011 - 02:43 PM

what is that ? yes i want lock all read/write access to a disk but i dont know VSS !! :(
Was This Post Helpful? 0
  • +
  • -

#9 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: C++ API question

Posted 27 July 2011 - 03:09 AM

ok guys i found one code to eject removealbe drive but i want one code to lock any drive please help me

#include <windows.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h>

// Prototypes

BOOL EjectVolume(TCHAR cDriveLetter);

HANDLE OpenVolume(TCHAR cDriveLetter);
BOOL LockVolume(HANDLE hVolume);
BOOL DismountVolume(HANDLE hVolume);
BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent);
BOOL AutoEjectVolume(HANDLE hVolume);
BOOL CloseVolume(HANDLE hVolume);

LPTSTR szVolumeFormat = TEXT("\\\\.\\%c:");
LPTSTR szRootFormat = TEXT("%c:\\");
LPTSTR szErrorFormat = TEXT("Error %d: %s\n");

void ReportError(LPTSTR szMsg)
{
	_tprintf(szErrorFormat, GetLastError(), szMsg);
}

HANDLE OpenVolume(TCHAR cDriveLetter)
{
	HANDLE hVolume;
	UINT uDriveType;
	TCHAR szVolumeName[8];
	TCHAR szRootName[5];
	DWORD dwAccessFlags;

	wsprintf(szRootName, szRootFormat, cDriveLetter);

	uDriveType = GetDriveType(szRootName);
	switch(uDriveType) {
	   case DRIVE_REMOVABLE:
		   dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
		   break;
	   case DRIVE_CDROM:
		   dwAccessFlags = GENERIC_READ;
		   break;
	   default:
		   _tprintf(TEXT("Cannot eject.  Drive type is incorrect.\n"));
		   return INVALID_HANDLE_VALUE;
	}

	wsprintf(szVolumeName, szVolumeFormat, cDriveLetter);

	hVolume = CreateFile(   szVolumeName,
		dwAccessFlags,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		0,
		NULL );
	if (hVolume == INVALID_HANDLE_VALUE)
		ReportError(TEXT("CreateFile"));

	return hVolume;
}

BOOL CloseVolume(HANDLE hVolume)
{
	return CloseHandle(hVolume);
}

#define LOCK_TIMEOUT        10000       // 10 Seconds
#define LOCK_RETRIES        20

BOOL LockVolume(HANDLE hVolume)
{
	DWORD dwBytesReturned;
	DWORD dwSleepAmount;
	int nTryCount;

	dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES;

	// Do this in a loop until a timeout period has expired
	for (nTryCount = 0; nTryCount < LOCK_RETRIES; nTryCount++) {
		if (DeviceIoControl(hVolume,
			FSCTL_LOCK_VOLUME,
			NULL, 0,
			NULL, 0,
			&dwBytesReturned,
			NULL))
			return TRUE;

		Sleep(dwSleepAmount);
	}

	return FALSE;
}

BOOL DismountVolume(HANDLE hVolume)
{
	DWORD dwBytesReturned;

	return DeviceIoControl( hVolume,
		FSCTL_DISMOUNT_VOLUME,
		NULL, 0,
		NULL, 0,
		&dwBytesReturned,
		NULL);
}

BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPreventRemoval)
{
	DWORD dwBytesReturned;
	PREVENT_MEDIA_REMOVAL PMRBuffer;

	PMRBuffer.PreventMediaRemoval = fPreventRemoval;

	return DeviceIoControl( hVolume,
		IOCTL_STORAGE_MEDIA_REMOVAL,
		&PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
		NULL, 0,
		&dwBytesReturned,
		NULL);
}

BOOL AutoEjectVolume(HANDLE hVolume)
{
	DWORD dwBytesReturned;

	return DeviceIoControl( hVolume,
		IOCTL_STORAGE_EJECT_MEDIA,
		NULL, 0,
		NULL, 0,
		&dwBytesReturned,
		NULL);
}

BOOL EjectVolume(TCHAR cDriveLetter)
{
	HANDLE hVolume;

	BOOL fRemoveSafely = FALSE;
	BOOL fAutoEject = FALSE;

	// Open the volume.
	hVolume = OpenVolume(cDriveLetter);
	if (hVolume == INVALID_HANDLE_VALUE)
		return FALSE;

	// Lock and dismount the volume.
	if (LockVolume(hVolume) && DismountVolume(hVolume)) {
		fRemoveSafely = TRUE;

		// Set prevent removal to false and eject the volume.
		if (PreventRemovalOfVolume(hVolume, FALSE) &&
			AutoEjectVolume(hVolume))
			fAutoEject = TRUE;
	}

	// Close the volume so other processes can use the drive.
	if (!CloseVolume(hVolume))
		return FALSE;

	if (fAutoEject)
		printf("Media in Drive %c has been ejected safely.\n",
		cDriveLetter);
	else {
		if (fRemoveSafely)
			printf("Media in Drive %c can be safely removed.\n",
			cDriveLetter);
	}

	return TRUE;
}

void Usage()
{
	printf("Usage: Eject <drive letter>\n\n");
	return ;
}

void main(int argc, char * argv[])
{
	if (!EjectVolume( 'F' ))
		printf("Failure ejecting drive %c.\n", argv[1][0]);

	return ;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1