• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2017
  • Last Modified:

c# call to FormatEx results in error

Hello,

I am attempting to make a call to FormatEx from a C# application.  The intended result is to format the specified hard drive.

VS2005 returns the following error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I have based my code around what I found at SysInternals http://www.sysinternals.com/SourceCode/fmifs.html.

The original declarations are:

VOID FormatEx ( PWCHAR DriveRoot,
   DWORD MediaFlag ,
   PWCHAR Format,
   PWCHAR Label,
   BOOL QuickFormat,
   DWORD ClusterSize,
   PFMIFSCALLBACK Callback);

where the FMIFSCALLBACK is:

typedef BOOLEAN (__stdcall *PFMIFSCALLBACK)(
   CALLBACKCOMMAND Command,
   DWORD SubAction,
   PVOID ActionInfo );

My code is:

 class Disk
    {
        private enum CallbackCommand
        {
              PROGRESS,
              DONEWITHSTRUCTURE,
              UNKNOWN2,
              UNKNOWN3,
              UNKNOWN4,
              UNKNOWN5,
              INSUFFICIENTRIGHTS,
              UNKNOWN7,
              UNKNOWN8,
              UNKNOWN9,
              UNKNOWNA,
              DONE,
              UNKNOWNC,
              UNKNOWND,
              OUTPUT,
              STRUCTUREPROGRESS
        }
       
        private const int FMIFS_HARDDISK = 0xC;


        private delegate Boolean FormatCallBackDelegate(CallbackCommand callBackCommand, int subActionCommand, IntPtr action);

        [DllImport("fmifs.dll", CharSet = CharSet.Auto)]
        private static extern void FormatEx(
            string driveLetter,
            long mediaFlag,
            string fsType,
            string label,
            bool quickFormat,
            int clusterSize,
            FormatCallBackDelegate callBackDelegate);

        public static void Format()
        {
            FormatCallBackDelegate fcb = new FormatCallBackDelegate(formatCallBack);

            string dl = @"v:\";
            string label = "NEWLABEL";
            string fs = "FAT";
           

            FormatEx(dl, FMIFS_HARDDISK, fs, label, true, 0, fcb);
        }

        private static Boolean formatCallBack(CallbackCommand callBackCommand, int subActionCommand, IntPtr action)
        {
            return true;
        }
    }



I would appreciate any pointers.

Thanks
0
Si-clone
Asked:
Si-clone
1 Solution
 
AlexFMCommented:
bool quickFormat

replace with int. .NET bool is one byte, and Win32 BOOL is int.

What is BOOLEAN which is returned by formatCallBack? If this is 32-bits value, C# formatCallBack function must return int.
0
 
prakash_prkCommented:
can try this plz...

Alex in .NET bool is two byte data type. so we can use bool for BOOL

[DllImport("fmifs.dll", CharSet = CharSet.Auto, EntryPoint = "FormatEx")]
void FormatEx( string driveRoot, uint mediaFlag, string format, string lable, bool quickFormat, uint clusterSize, FormatCallBackDelegate callBack );

private delegate bool FormatCallBackDelegate(CallbackCommand callBackCommand, uint subActionCommand, IntPtr action);

what is the datatype of the CallbackCommand enumeration?
In C# default enum datatype is int (int32)

if the CallbackCommand is 16 bit unsigned integer change the enum declaration to
private enum CallbackCommand :  ushort

regards
prakash
0
 
AlexFMCommented:
Even if bool is two bytes, BOOL is still 4 bytes. .NET bool type should not ne used for interoperability. To be 100% correct, we must use Int32 for Win32 BOOL type. int is also OK.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Si-cloneAuthor Commented:
Hello,

Thank you both for your responses.  I tried what each of you had said, but it did not change the result.

I did however discover that I had declared FMIFS_HARDDISK as int, when the FormatEx mediaFlag was expecting a long.  I then discovered that mediaFlag should be an int (at least, if I change it to int, the API call doesn't generate the same error).

Thanks again.
0
 
Si-cloneAuthor Commented:
Here is the full code, which works:

 class Disk
    {
        private enum CallbackCommand
        {
              PROGRESS,
              DONEWITHSTRUCTURE,
              UNKNOWN2,
              UNKNOWN3,
              UNKNOWN4,
              UNKNOWN5,
              INSUFFICIENTRIGHTS,
              UNKNOWN7,
              DISKLOCKEDFORACCESS, //UNKNOWN8,
              UNKNOWN9,
              UNKNOWNA,
              DONE,
              UNKNOWNC,
              UNKNOWND,
              OUTPUT,
              STRUCTUREPROGRESS
        }
       
        private const int FMIFS_HARDDISK = 0xC;

        // Bool
        private delegate Int32 FormatCallBackDelegate(CallbackCommand callBackCommand, int subActionCommand, IntPtr action);

        [DllImport("fmifs.dll", CharSet = CharSet.Auto)]
        private static extern void FormatEx(
            string driveLetter,
            int mediaFlag,
            string fsType,
            string label,
            Int32 quickFormat,
            Int32 clusterSize,
            FormatCallBackDelegate callBackDelegate);

        public static void Format()
        {
            FormatCallBackDelegate fcb = new FormatCallBackDelegate(formatCallBack);

            string dl = @"v:";
            string label = "NEWLABEL";
            string fs = "NTFS";
           

            FormatEx(dl, FMIFS_HARDDISK, fs, label, 1, 0, fcb);
        }

        private static Int32 formatCallBack(CallbackCommand callBackCommand, int subActionCommand, IntPtr action)
        {
            return 1;
        }
    }
0
 
GranModCommented:
Closed, 250 points refunded.

GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now