CSharp D3D sample exception

Hi! I've got next error on CSharp_D3D sample (prebuilded exe with OCC installation works fine, while manually builded binaries won't work):

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


It happens here:

IDirect3DSurface9* D3dColorSurface()      { return myD3dSurf; }

on ResizeBridgeFBO(..) method invocation.

I'm using Win10, VS2017, OCC 7.2.0. DirectX SDK is installed.

Any suggestions? Mby I've missed something important to successfully build D3D project?

Kirill Gavrilov's picture

It is difficult to guess what can be a reason, considering that pre-built sample works fine on your configuration, but you may check if you are using consistent OCCT+Sample builds
(e.g. using the same source code, using the same parameters, using the same build mode - Release or Debug, etc.),

Konstantin Chuvilyov's picture

I'm trying to build Debug and Release x64 with no success.

I guess it is not required to install DirectX SDK, because D3D9.lib is used from my WinSDK (Windows Kits\10\Lib\10.0.15063.0).

I've tried to install DirectX SDK and use $(DXSDK_DIR)Lib\x64 path to include lib from this folder - also no success.

Another interesting point is that my size of OCCTProxy_D3D.dll in release mode is 99KB, while size of pre-built dll is 172KB. I guess d3d9.lib is not linked, because I can delete link pragma and successfuly build solution again - no difference in behaviour.

Later I've found next warning message in my build: 

Warning LNK4248 unresolved typeref token (0100001A) for 'IDirect3DSurface9'; image may not run OCCTProxy_D3D path_to\OCCTProxyD3D.obj 1

Also I've tried to reinstall opencascade 7.2.0 - no success. I've tried to include <d3d9.h>: build fails on

Error C2061 syntax error: identifier 'THIS'

Platform Toolset v141 (vs2017), Windows SDK version is 10.0.15063.0

Kirill Gavrilov's picture

Also I've tried to reinstall opencascade 7.2.0 - no success.

Platform Toolset v141 (vs2017), Windows SDK version is 10.0.15063.0

I suppose you have built OCCT 7.2.0 using VS2017 by yourself, because official installation package includes only VS2010 builds, which are incompatible with VS2015+?

Konstantin Chuvilyov's picture

Right now I've tried to build with VS2015 toolset v140 - no success. It crashes in the same way.

Kirill Gavrilov's picture

I've built the sample using VS2015 (x64 Debug target) - Visual Studio has automatically upgraded solution -> .NET Framework 4; though it is better switching to 4.5 in project settings due to bugs in older versions of .NET within System.Windows.Interop.D3DImage.
It starts without issues from Visual Studio (msvc_D3D.bat vc14 win64 Debug).
So it should be something in your environment which causes the issue...

Guido van Hilst not specified's picture

Hi Konstantin,

If you are looking for C# solution to development with Opencascade we have advanced OpenCascade wrappers avalaible.

There a 6 assemblies for each occ module:

  • CCT.OCDotNet.Foundation.dll
  • CCT.OCDotNet.ModelingData.dll
  • CCT.OCDotNet.ModelingAlgorithms.dll
  • CCT.OCDotNet.Visualization.dll
  • CCT.OCDotNet.ApplicationFramework.dll
  • CCT.OCDotNet.DataExchange.dll

Our C# wrappers include:

        //Dynamic casting :
        OCGeom_Plane plane = myFace.Surface() as OCGeom_Plane;

        //Casting to shape type
        OCTopoDS_Face face = myShape.AsFace();

        //Streaming: to any .net stream.
        public static void ShapeToStream(OCTopoDS_Shape shape, Stream stream)
            OCBRepTools.Write(shape, stream);
        public static OCTopoDS_Shape ShapeFromStream(Stream stream)
            OCBRep_Builder builder = new OCBRep_Builder();
            OCTopoDS_Shape shape = new OCTopoDS_Shape();
            OCBRepTools.Read(shape, stream, builder);
            return shape;

        //Extension methods to easy acces the undelaying geometry:
        OCGeom_Curve curve = myEdge.Curve();

        //Extension methods for easy iterating of shapes:
        foreach(OCTopoDS_Edge edge in shape.Edges())

See online demo build with opencacade C# wrappers:


​If you are interested in the C# wrappers , you can contact me at: guidovanhilst at gmail dot com.

Best regards,