-1

I'm using SlimDX in a managed Windows application ( Win. XP, .Net 3.5 ). SlimDX handles all unmanaged DirectX API calls. I'm trying to call the following method found in SlimDX framework: SlimDX.Direct3D9.XFile.CreateEnumerationObject(Stream stream). Here is implementation:

XFile xfile = new XFile();
XFileEnumerationObject enumObj = null;
string Filename = @"MyDocs\Test.x";
using (FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read))
{
    enumObj = xfile.CreateEnumerationObject(fs);
}

When I call this method I get the following exception:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at SlimDX.Direct3D9.XFile.CreateEnumerationObject(Byte[] memory)
   at SlimDX.Direct3D9.XFile.CreateEnumerationObject(Stream stream)
   at SceneGenerator.ContentManager.XParser.Parse(XParserMode Mode, MeshFlags options, List`1& materialPartitions, GDEAssetConfig& cfg, _XData& userContext, Byte[] filedata) in D:\Documents and Settings\GDEAuxiliary\My Documents\Visual Studio 2010\Projects\SceneGenerator\SceneGenerator\XParser.cs:line 191

SlimDX is to handle the conversion of this memory stream to byte array, and byte array to unmanaged memory. I imagine that the file data was not loaded in the expected text encoding...

I have validated the file by using an alternate method found in SlimDX framework that creates an enumeration object using a filename and a specified character set...here is implementation:

XFile xfile = new XFile();
XFileEnumerationObject enumObj = null;
string Filename = @"MyDocs\Test.x";
enumObj = xfile.CreateEnumerationObject(cfg.Filename, System.Runtime.InteropServices.CharSet.Auto);

This method works fine...

What can I do to prepare the memory stream for SlimDX...unfortunately I'm not entirely sure what is expected of the memory stream by SlimDX...

P. Avery
  • 779
  • 1
  • 16
  • 34

1 Answers1

1

I have been there. I had to use HBA APIs from my c# application. Passing a byte array gave me similar error no matter what I did.

The solution that worked for me was to encapsulate the Byte[] array in a stuct and then pasing the struct. Make sure that the byte array in the struct is defined as Marshal.ByValArray so that c# knows its size and everything.

From my experience the rule of thumb is when passing things to unmanaged memory make sure you have allocated managed memory to it.

Also I am sure you have alreay checked, but make sure that your FileStream object is valid it has access to file, file is not empty.

Below is the example of the struct:

    /*
    typedef struct HBA_wwn {
    HBA_UINT8 wwn[8];
    } HBA_WWN, *PHBA_WWN;
    */
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct HBA_WWN
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public Byte[] wwn;

        public static HBA_WWN getInstance()
        {
            HBA_WWN wwn = new HBA_WWN();
            wwn.wwn = new Byte[8];

            return wwn;
        }
    }
iamtheone
  • 105
  • 1
  • 8
  • thanks for suggestions...I have also tried streaming the memory in from file, read()ing the memory into a byte array(after allocating an array w/size indicated by filestream(Stream.Length) and then calling the SlimDX method with the byte[] to no avail...how might encapsulating the array be implemented? I'm not sure how it applies to this situation... – P. Avery May 01 '14 at 20:53
  • what happened in my case was just passing the byte[] I was getting accessviolation. I created a struct which just has the array I updated my answer with an example of the struct – iamtheone May 02 '14 at 00:14
  • I see thanks...in my case the SlimDX method is fixed and can only take a stream or byte[]...I will not be able to pass a struct...also...the file size will vary so I cannot Marshal the file data... – P. Avery May 02 '14 at 02:01