0

I'am trying to create new partition and mount a volume to this new, I thought that CreateFile let me do this, with this code :

LPCTSTR lpFileName=L"\\\\.\\Device\\Harddisk0\\Partition3";
HANDLE handl=CreateFile( lpFileName,
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL,
                            CREATE_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL  );
   if (handl==INVALID_HANDLE_VALUE)
   {
   qDebug()<<"handl invalid"<<" error"<<GetLastError();}


   bool success = DefineDosDevice(DDD_RAW_TARGET_PATH,L"I:",L"\\Device\\Harddisk0\\Partition3");
   if(!success)
       qDebug()<<" DefineDosDevice failed "<<GetLastError();


   bFlag = GetVolumeNameForVolumeMountPoint(
               L"I:\\", // input volume mount point or directory
               /** what u do in this directory u find it in th mount piont and vice versa**/
                  Buf, // output volume name buffer
              BUFSIZE  // size of volume name buffer
           );
   if (bFlag != TRUE)
   {
      //_tprintf( TEXT("Retrieving volume name for %s failed.\n"), argv[2] );
      qDebug()<<"Retrieving volume name failed.      "<<GetLastError();
      return (-2);
   }
    qDebug()<<"Volume name"<<QString::fromWCharArray(Buf);



    bool fResult = DefineDosDevice (
                         DDD_RAW_TARGET_PATH|DDD_REMOVE_DEFINITION|
                         DDD_EXACT_MATCH_ON_REMOVE, L"I:",
                         L"\\Device\\Harddisk0\\Partition3");
    if (!fResult)
                qDebug()<<"DefineDosDevice failed  "<< GetLastError();



   bFlag = SetVolumeMountPoint(L"D:\\myDirExample\\example\\", // mount point
                               /** should b empty**/
                                Buf // volume to be mounted
                               );

   if (!bFlag)
      {
        qDebug()<<"Attempt to mount failed";
        qDebug()<<"error "<<GetLastError();
      }

   return (bFlag);


   CloseHandle(handl);

I have:

handl invalid  error 3 
Retrieving volume name failed  erorr  2

The first error is ERROR_PATH_NOT_FOUND: The system cannot find the path specified. So how can I fix lpFileName to make it works

thanks in advance, any help will be appreciated.

Oumaya
  • 655
  • 4
  • 18
  • 43
  • 1
    There is no `\\.\Device`, that's why you're getting ERROR_PATH_NO_FOUND. There is no way to access NT's `\Device` from win32 (you would have to use NtCreateFile instead). Even if there was, you can only create files on devices (e.g. if `\Device\Harddisk0\Partition3` were a device, you could create `\Device\Harddisk0\Partition3\something`). `\Device\Harddisk0` is not part of any device (both are simple directories in the NT namespace). – avakar Sep 27 '12 at 12:45
  • thank you for your answers , that is my point `\Device\Harddisk0\Partition3` is the partition that I want create, it does not exist. – Oumaya Sep 27 '12 at 13:04
  • I don't believe there is an API for creating partitions, etc. You can control the [diskpart tool](http://technet.microsoft.com/en-us/library/26a4a166-95fa-4faf-95bc-2d5345f4a57a) with a script. – arx Sep 27 '12 at 13:46
  • Really !!! so how I can insert scripts in my code (c++)? how can I proceed? PS: my purpose here to add a volume with letter" I:\\" in myharddisk0. – Oumaya Sep 27 '12 at 14:13
  • You need to use something like [this](http://msdn.microsoft.com/en-us/library/aa365189%28VS.85%29.aspx). – Luke Oct 01 '12 at 14:38
  • thank you for your responses, I solved it by different way, I decided to assign a letter drive to a folder and mount it using `subst` command , thank you a lot any way. – Oumaya Oct 02 '12 at 07:44

1 Answers1

0

There is, I used it for my shredder. The only tricky part is managing the LARGE_INTEGER for filepointer (used to choose the sector). You need to use the handle to the device using CreateFile() first. After you have done that lock the drive with FSCTL_LOCK_VOLUME, use SetFilepointerEx to set the sector and create new one with IOCTL_DISK_SET_PARTITION_INFO_EX.

Cheers ;)

Edited few times - wasn't sure if IOCTL_DISK_SET_PARTITION_INFO is needed too, but it is not, you can set the type with IOCTL_DISK_SET_PARTITION_INFO_EX as well.

MisteRious
  • 1
  • 1
  • 1