none
Help with API Call RRS feed

  • Question

  • I am trying to use the virtdisk.dll to create and manage VHD files. I am currently using the CreateVirtualDisk call that Microsoft has documented here. Here is how my implementation looks for the call:

    public Boolean CreateVHD(String sDiskPath)
    
     {
    
      VIRTUAL_STORAGE_TYPE vst = new VIRTUAL_STORAGE_TYPE();
    
      CREATE_VIRTUAL_DISK_PARAMETERS cvdp = new CREATE_VIRTUAL_DISK_PARAMETERS();
    
      IntPtr pDiskPointer = new IntPtr() ;
    
      Int32 iResult;
    
    
    
      vst.DeviceId = VIRTUAL_DEVICE_ID.VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
    
      vst.VenderId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;
    
    
    
      cvdp.Version = CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_1;
    
      cvdp.Version1.UniqueID = Guid.Empty;
    
      cvdp.Version1.MaximumSize = 512;
    
      cvdp.Version1.BlockSizeInBytes = VIRTUAL_DISK_PARAMETERS_BLOCK_SIZE.CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE;
    
      cvdp.Version1.SectorSizeInBytes = VIRUTAL_DISK_PARAMETERS_SECTOR_SIZE.CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;
    
      cvdp.Version1.ParentPath = IntPtr.Zero;
    
      cvdp.Version1.SourcePath = IntPtr.Zero;
    
    
    
      iResult = CreateVirtualDisk(ref vst, sDiskPath, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, IntPtr.Zero, CREATE_VIRTUAL_DISK_FLAG.CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION, 0, ref cvdp, IntPtr.Zero, ref pDiskPointer);
    
    
    
      if (iResult == ERROR_SUCCESS)
    
      {
    
       return true;
    
      }
    
      else
    
      {
    
       throw new Exception(new Win32Exception(iResult).Message);
    
      }
    
     }
    
    


    When it hits the line of code where the CreateVirtualDisk is called, I get this error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.".

    You can view my full source here, I didn't want to post it all to reduce clutter of this post. Any help would be greatly appreciated!


    • Moved by Leo Liu - MSFT Tuesday, June 14, 2011 8:30 AM Moved for better support. (From:Visual C# General)
    Monday, June 13, 2011 6:05 PM

All replies

  • It might be something related to your definition of the structs. Can you post here that definition?
    Paulo Morgado
    Tuesday, June 14, 2011 1:12 AM
  • Hi Matt,
    I am moving your thread into the Common Language Runtime Forum Forum for specialized support. Thanks.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 8:29 AM
  • You  might be passing a managed heap object to an unmanaged heap. See if the details provided in this post helps you.

    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/c48f1f16-f027-4d74-8dd3-4f282697b48c/#1e1a388d-a219-460b-be0a-db484385b82c

    Tuesday, June 14, 2011 11:59 AM
  • The parameter types are simple enough for the P/Invoke interop assistant. How did you generate the signature for parameter types and the function signature?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Wednesday, June 15, 2011 1:03 AM
  •  

    Hi Matt,

     

    Welcome to the MSDN form.

     

    As I seen, the source you provided is the code of virtdisk.dll. I have tried to build it, and it successfully built it as a dll. Could you please provide some more code snippet where occurs the error when calling the method?

     

    Any more information that helps us to reproduce this issue is appreciated. Thanks.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 15, 2011 7:20 AM
  • The code does not fail to build, it fails when you make the call to CreateVHD.

     

    Wednesday, June 15, 2011 12:46 PM
  • I generated the signature by looking at the MSDN documentation for the call. I believe it is one of my structues that is typed incorrectly. Just not sure which one.
    Wednesday, June 15, 2011 12:47 PM
  •  

    Hi Matt,

     

    I noticed that you have passed a structure(CREATE_VIRTUAL_DISK_PARAMETERS_Version1) to another structure(CREATE_VIRTUAL_DISK_PARAMETERS_Version1), and pass the reference of the first structure as a parameter to CreateVirtualDisk method. Because we need to pass a reference of managed structure which contains another structure to unmanaged code, I suggest to modify the member of CREATE_VIRTUAL_DISK_PARAMETERS_Version1 structure as a IntPtr:

     

    [StructLayout(LayoutKind.Sequential)]

        private struct CREATE_VIRTUAL_DISK_PARAMETERS

        {

                  public CREATE_VIRTUAL_DISK_VERSION Version;

                 public IntPtr Version1;

        }

     

    Before we pass CREATE_VIRTUAL_DISK_PARAMETERS structure to CreateVirtualDisk  method,  we marshal an IntPtr points to CREATE_VIRTUAL_DISK_PARAMETERS_Version1:

     

    CREATE_VIRTUAL_DISK_PARAMETERS_Version1 cvdp1 = new CREATE_VIRTUAL_DISK_PARAMETERS_Version1();

            cvdp1.UniqueID = Guid.Empty;

            cvdp1.MaximumSize = 512;

            cvdp1.BlockSizeInBytes = VIRTUAL_DISK_PARAMETERS_BLOCK_SIZE.CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE;

            cvdp1.SectorSizeInBytes = VIRUTAL_DISK_PARAMETERS_SECTOR_SIZE.CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;

            cvdp1.ParentPath = IntPtr.Zero;

            cvdp1.SourcePath = IntPtr.Zero;

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(cvdp1));

            Marshal.StructureToPtr(cvdp1, ptr, false);

            cvdp.Version1 = ptr;

     

    After call CreateVirtualDisk  method, we can call Marshal.FreeHGlobal(ptr) to free the unmanaged block of memory.

     

    Moreover, we can try to modify other structure which will be passed as a reference to unmanaged code.

     

    After searching, I have found a MSDN article for The Virtual Disk API.

     

    I hope this can help you.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 16, 2011 7:20 AM
  • I made that change, and it appears I am getting a little further, but now I am getting the following error:

    "A virtual disk support provider for the specified file was not found"

    Not sure what is causing this. This is a Windows 7 workstation and I am able to create VHDs on it with Device Manager.

    Thursday, June 16, 2011 5:37 PM
  • Did you check Process Monitor trace, which file it is trying to find?

     

    Tuesday, June 21, 2011 12:36 AM