SharpDX SwapChain1 AccessViolationException - c#

So I try to create a SwapChain1 in SharpDX.
Here is the Code:
SwapChainDescription1 desc = new()
{
Width = toSetSize.Value.Width,
Height = toSetSize.Value.Height,
Format = Format.R8G8B8A8_UNorm,
Stereo = false,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
BufferCount = 1,
Scaling = Scaling.Stretch,
SwapEffect = SwapEffect.Discard
};
SwapChainFullScreenDescription scd = new SwapChainFullScreenDescription()
{
RefreshRate = new Rational(60, 1),
Scaling = DisplayModeScaling.Stretched,
Windowed = false
};
using (var dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>())
using (var dxgiAdapter = dxgiDevice2.Adapter)
using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>())
{
res = new SwapChain1(dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>(), device, Window.Handle, ref desc, scd);
return res;
}
my issue is, this code throws a AccessViolationException with the Attempted to read or write corrupted memory stuff.
What am i doing wrong?
How can i fix this?
I tried:
Changing the Parameters inside of "desc" and "scd"
Edit: So i got a step further, seemingly factory.NativePointer was IntPtr.Zero, i have updated the code, hope this helps...
Edit2: Okay one step closer, again, i enabled Debugging from native code and got this:
Exception called at 0x759CE292 in Demo.exe: Microsoft C++ Exception: _com_error at location 0x0297C5B8.
Exception called at 0x6BA81D22 (dxgi.dll) in Demo.exe: 0xC0000005: Access violation at position 0x000000B8.
Untranslated (german):
Ausnahme ausgelöst bei 0x759CE292 in Demo.exe: Microsoft C++-Ausnahme: _com_error bei Speicherort 0x0297C5B8.
Ausnahme ausgelöst bei 0x6BA81D22 (dxgi.dll) in Demo.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x000000B8.

Fixed it by doing this before returning the new SwapChain:
Window.TopMost = false;
Window.FormBorderStyle = FormBorderStyle.Fixed3D;

Related

Ghostscript.NET 1.2.3 'Method not found' running as a Function App

I am not sure why this is failing at var img = rasterizer.GetPage(dpi, 1); with the following error:
Method not found: 'System.Drawing.Image Ghostscript.NET.Rasterizer.GhostscriptRasterizer.GetPage(Int32, Int32)'
My path to the dll is correct and the 64-bit version. The GhostScript Rasterizer can open the memory stream and then fails on GetPage(). It bombs out the debugger without hitting a breakpoint if I have that line uncommented.
Here is the code:
var dpi = 90;
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(new Version(0, 0, 0), workingDirectory + #"\gsdll64.dll", string.Empty, GhostscriptLicense.GPL);
using (var rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(ms, gvi, false);
if (rasterizer.PageCount > 0)
{
var img = rasterizer.GetPage(dpi, 1);
It seems that the GetPage method only works accepting 3 arguments, as you can verify on the following link:
https://csharp.hotexamples.com/examples/Ghostscript.NET.Rasterizer/GhostscriptRasterizer/GetPage/php-ghostscriptrasterizer-getpage-method-examples.html
Please, check if using all the arguments works for you, as this could be the cause of the "Method not found" error.

UWP AudioGraph setup not working in all devices, HRESULT: 0x88960001 thrown on AddOutgoingConnection

I'm having trouble with a AudioGraph in a UWP app. My trouble is that when I'm adding adding an Outgoing Connection to my input device node I get a Exception thrown with HRESULT: 0x88960001.
Using trial and error I figured out that the problem is with the MediaEncoding profile I set on both the input and output node... but I have now 2 settings only work on one machine and the other only works on another machine... I needed the audio to be Single Channel, 16bit sample with a 16K sample in PCM.
The code I'm using is fairly simple and was based on samples that exist online, the thing is it works only on some microphones and not others... I needed it to be generic and have always the same output so that I can input that to my service endpoint.
I left the 2 AudioEncodingProperties settings there (one of them commented out)
var result = await AudioGraph.CreateAsync(
new AudioGraphSettings(AudioRenderCategory.Media));
if (result.Status == AudioGraphCreationStatus.Success)
{
this.graph = result.Graph;
var microphone = await DeviceInformation.CreateFromIdAsync(
MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Default));
// Low gives us 1 channel, 16-bits per sample, 16K sample rate.
var outProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low);
//outProfile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);
outProfile.Audio = AudioEncodingProperties.CreatePcm(44100, 2, 32);
var inProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low);
//inProfile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);
inProfile.Audio = AudioEncodingProperties.CreatePcm(44100, 2, 32);
var outputResult = await this.graph.CreateFileOutputNodeAsync(file,
outProfile);
if (outputResult.Status == AudioFileNodeCreationStatus.Success)
{
this.outputNode = outputResult.FileOutputNode;
var inputResult = await this.graph.CreateDeviceInputNodeAsync(
MediaCategory.Speech,
inProfile.Audio,
microphone);
if (inputResult.Status == AudioDeviceNodeCreationStatus.Success)
{
//EXEPTION IS THROWN HERE
inputResult.DeviceInputNode.AddOutgoingConnection(
this.outputNode);
this.graph.Start();
}
}
}
else
{
throw new Exception("Could not create AudioGraph");
}
Thank you guys for our help
Instead of constructing the inputResult with the inProfile.Audio object, try using the default encoding properties for the graph:
var enc = graph.EncodingProperties;
CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Media, enc, this.outputNode);
I had a similar issue and this cleared things up, and allowed the audio to be input from any device without specifying the encoding profile.

DirectX 11 initialisation "works" and fails on rendering only

Does anybody know why device creation for using DirectX11 works, even though the hardware doesn't support it? I have the following simplified code (this uses SlimDx, but I guess the language is of second importance here:
var form = frm;
form.Resize += form_Resize;
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);
device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
Resize(form.ClientSize);
sprite = new SpriteRendererSlimDx(device, m_lock);
var textBlock = new TextBlockRendererSlimDx(sprite, "Arial", FontWeight.Bold, SlimDX.DirectWrite.FontStyle.Normal, FontStretch.Normal, 16);
MessagePump.Run(form, () =>
{
device.ImmediateContext.ClearRenderTargetView(renderView, Color.DarkBlue);
textBlock.DrawString("Hello World", Vector2.Zero, Color.Red);
sprite.Flush();
swapChain.Present(0, PresentFlags.None);
});
The code opens a window and draws some text. It works fine on Graphics Cards supporting Feature Level 11.0, but if I run the same code on my aging ATI Radeon HD 4550, which only supports Feature Level 10.1, the device driver locks up and windows restarts the display driver.
The issue is not with the (in the example not shown) text rendering, but happens for rendering triangles, too. Debugging shows that the lockup happens on the swapChain.Present() call.
Directx 11 Compatiblilty suggests that I might be having a problem with using Shader Level 5 (which I'm trying to use), but generally my question is: Why don't I get an ordinary error message if something is not supported? The driver shouldn't attempt to compile Shader Level 5, if the card cannot do it, should it?
Update
The error also seems to happen if I only use shader level 4.

SharpDX debugging: "Device.CreateWithSwapChain()" versus "new SwapChain()"?

If I create my Device and my SwapChain like this:
SwapChain _swapChain;
Device _device;
// SwapChain description
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(500, 300, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = _windowHandle,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug,
desc, out _device, out _swapChain);
I get the expected debugging text:
Now if I create my Device and my SwapChain like this:
Factory _factory = new Factory();
Adapter adapter = _factory.GetAdapter(0);
SwapChain _swapChain;
Device _device = new Device(adapter, DeviceCreationFlags.Debug);
// SwapChain description
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(500, 300, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = _windowHandle,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.RenderTargetOutput
};
_swapChain = new SwapChain(_factory, _device, desc);
I don't get the expected debugging text:
In addition to not getting the expected debugging text, I get a load of new messages in my output:
First-chance exception at 0x7631C42D in Tester.exe: Microsoft C++ exception: _com_error at memory location 0x064EEC28.
First-chance exception at 0x7631C42D in Tester.exe: Microsoft C++ exception: _com_error at memory location 0x064EED6C.
First-chance exception at 0x7631C42D in Tester.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
D3D11 ERROR: ID3D11Device::OpenSharedResource: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #381: DEVICE_OPEN_SHARED_RESOURCE_INVALIDARG_RETURN]
D3D11 WARNING: ID3D11DeviceContext::OMSetRenderTargets: Resource being set to OM RenderTarget slot 0 is inaccessible because of a previous call to ReleaseSync or GetDC. [ STATE_SETTING WARNING #9: DEVICE_OMSETRENDERTARGETS_HAZARD]
D3D11 WARNING: ID3D11DeviceContext::Draw: The Pixel Shader expects a Render Target View bound to slot 0, but none is bound. This is OK, as writes of an unbound Render Target View are discarded. It is also possible the developer knows the data will not be used anyway. This is only a problem if the developer actually intended to bind a Render Target View here. [ EXECUTION WARNING #3146081: DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET]
D3D11 WARNING: ID3D11DeviceContext::OMSetRenderTargets: Resource being set to OM RenderTarget slot 0 is inaccessible because of a previous call to ReleaseSync or GetDC. [ STATE_SETTING WARNING #9: DEVICE_OMSETRENDERTARGETS_HAZARD]
Where the last message repeats itself every frame...
Why is this happening? What's the difference between the 2 creation methods? How can I use the second creation method properly?
PS: I want to use the second creation method because I need the device to determine anti-aliasing settings...
PPS: In case it's needed, here's the code that creates the RenderTargetView:
using (Texture2D backBuffer = _swapChain.GetBackBuffer<Texture2D>(0))
{
_renderTargetView = new RenderTargetView(_device, backBuffer);
}
_context = _device.ImmediateContext;
_context.OutputMerger.SetRenderTargets(_renderTargetView);
_context.Rasterizer.SetViewport(0, 0, 500, 300);
Using the second method of Device creating, replacing
Device _device = new Device(adapter, DeviceCreationFlags.Debug);
with
Device _device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug);
makes the debug text appear again. It also seems to remove the warning messages and the first-chance exceptions
I must say this in an extremely subtle difference, which I only found through comparing the 2 Graphics Event Lists...

Drag/drop from outlook to internet explorer via BHO doesn't work on x32/86 machines

I'm currently implementing a browser helper object which would allow dragging emails from the outlook to the internet explorer's page.
I'm following the approach described in the following post: Implementing a Drag-and-Drop function from MS Outlook into our web application. I've got it working but only on x64 machines. On the x32/86 machines i'm getting the exception in the following piece of code (obviously i've replaced real filename inserting with fake one for simplicity):
DropFiles df = new DropFiles();
string filename = #"D:\projects\hello.txt";
byte[] binaryData = Encoding.Unicode.GetBytes(filename);
binaryData = binaryData.Concat(new byte[] { 0, 0 }).ToArray();
IntPtr pointerToGlobalMemory = Marshal.AllocHGlobal(Marshal.SizeOf(df) + binaryData.Length);
df.Files = Marshal.SizeOf(df);
df.Wide = true;
Marshal.StructureToPtr(df, pointerToGlobalMemory, true);
IntPtr newPointer = new IntPtr(pointerToGlobalMemory.ToInt32() + Marshal.SizeOf(df));
Marshal.Copy(binaryData, 0, newPointer, binaryData.Length);
var descriptorFormat = new COMInterop.FORMATETC();
descriptorFormat.cfFormat = HdropDescriptorId; // 15
descriptorFormat.ptd = IntPtr.Zero;
descriptorFormat.dwAspect = COMInterop.DVASPECT.DVASPECT_CONTENT;
descriptorFormat.lindex = -1;
descriptorFormat.tymed = COMInterop.TYMED.TYMED_HGLOBAL;
var td = new COMInterop.STGMEDIUM();
td.unionmember = pointerToGlobalMemory;
td.tymed = COMInterop.TYMED.TYMED_HGLOBAL;
dataObject.SetData(ref descriptorFormat, ref td, true);
On the executing the last ling of this code (actually setting the fake HDROP descriptor) i'm getting the following exception:
"Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))".
Did someone experienced described problem or have an idea what can be the reason of this issue?
To be more specific about environment - i'm having this trouble on win7 32 bit with IE 10 but i'm pretty sure that the reason especially in that machine is 32 bit.
You need to implement your own IDataObject and pass it to the original IDropTarget.Drop instead of hijacking an existing IDataObject coming from Outlook.

Categories

Resources