Questions tagged [safehandle]
28 questions
1
vote
1 answer
Does Marshal.Release() have an implicit ReliabilityContract?
I am writing a .NET application where I have to use API calls (really). My application gets COM interfaces by calling Win API's function CoCreateInstance().
Of course, I have to release those interfaces when having finished with them. I think that…

Binarus
- 4,005
- 3
- 25
- 41
1
vote
1 answer
How to properly dispose of pinvoke/unmanaged code
I have code that needs to pinvoke a c dll, it seems to me that this code should implement idisposible since it touches unmanaged code. I could be wrong so please correct me if this is not true.
Reading up on the stuff it seems like I should use…

nportelli
- 3,934
- 7
- 37
- 52
0
votes
0 answers
Can I use SafeHandle to manage GCHandle?
SafeHandle gives a very strong guarantee that the resource it manages will eventually be released, even if an asynchronous exception occurs. Even using and try finally cannot guarantee that.
I am thinking to use this guarantee to free a pinned…

yaskovdev
- 1,213
- 1
- 12
- 22
0
votes
0 answers
How does .NET runtime convert SafeHandle to IntPtr and vice versa?
I have a C++ function that accepts a pointer to a native object. I can call the function from my C# code in two ways:
Pass an IntPtr to it, like this,
Pass the subclass of the SafeHandle to it, like this.
While the first case is quite…

yaskovdev
- 1,213
- 1
- 12
- 22
0
votes
0 answers
Is there a way to use StopSpeakingAsync on SpeechSynthesizer without Dispose hanging?
When trying to use SpeechSynthesizer from the Microsoft.CognitiveServices.Speech sdk, I noticed that Dispose() hangs if it gets called after StopSpeakingAsync has been called.
Is there a way to circumvent this? I did a short console app that…

Martin Kallner
- 1
- 1
0
votes
0 answers
Is there a way to send an array of SafeHandles to a native function from c#?
I have a native (c++) function that takes an array of pointers to images as an argument. In my c# API a pointer to an image is kept as a SafeHandle. Now I want to send an array of SafeHandles to my function but then I get into the following…

AndersG
- 81
- 7
0
votes
1 answer
Using `SafeHandle` for a C API, that includes reference counting
I am working on Python interop. Internally, Python keeps reference counters for all objects via Py_IncRef and Py_DecRef. I want to ensure proper finalization of pointers to Python objects, so I am trying to create a class PyHandle derived from…

LOST
- 2,956
- 3
- 25
- 40
0
votes
0 answers
What to use instead of SafeHandle.DangerousGetHandle?
I was looking at my SonarQube static code analysis and came across a report that says using a SafeTokenHandle DangerousGetHandle method call.
I start by declaring a SafeTokenHandle:
SafeTokenHandle safeTokenHandle;
var returnValue =…

obizues
- 1,473
- 5
- 16
- 30
0
votes
0 answers
P/Invoke with SafeHandle as reference parameter
I have a Function in an unmanaged Dll which expects a pointer to a Handle like that example:
extern "C" __declspec(dllexport) BOOL __stdcall MySetEvent(HANDLE* handle,
int size)
{
if (!handle) return FALSE;
assert(sizeof(*handle) == size);
…

AndiR
- 179
- 10
0
votes
2 answers
How can I prevent the same unmanaged pointer from being encapsulated in two different SafeHandles?
I am writing a managed wrapper for a game engine written in C to challenge myself. I have started to wrap unmanaged pointers in SafeHandle derivatives, but it donned on me that calls to the unmanaged functions that could return the same pointer will…

sonicbhoc
- 434
- 3
- 15
-1
votes
1 answer
ASP.Net SafeHandle - tasks and examples
I want to understand how to work with unmanaged resources and when I need the SafeHandle class. What can be the problem statement when you can say: "Oh, here I need the SafeHandle class!"?
I would be grateful for links to articles, examples,…

Kakao Developer
- 63
- 1
- 1
- 9
-1
votes
2 answers
Sending raw text to printer via usb
I am trying to send ZPL commands to a Zebra ZT230 printer. The printer and drivers are installed and the printer port is "USB003". The PC communicates perfectly with the printer via either Zebra Printer Setup Utilities or ZebraDesign. I tried the…

John Lee
- 317
- 6
- 14
-1
votes
1 answer
safe handle has been closed - numerous options to fix
I have a private function that creates a new serial port and opens it. From time to time, I get the "Safe handle has been closed" exception, that terminates the application. Now, I've been reading a few optional fixes and would like to know from…

NettaD
- 13
- 1
- 7