1

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 safehandles. Great. Except my dll doesn't return any handles, or intptr. So now what?

The signatures are mostly like the following:

HRESULT _XYZFN XYZNewTrip (Trip *pTripID); 

Argument Values:

pTripID: pointer to a 4 byte integer in which the new Trip handle will be placed

Can I some how shoehorn a safehandle in there? It seems like maybe the hard case from this article.

nportelli
  • 3,934
  • 7
  • 37
  • 52

1 Answers1

3

If it doesn't return an handle, then clearly you can't deallocate anything, so the IDisposable pattern would be useless.

Only thing:

pTripID: pointer to a 4 byte integer in which the new Trip handle will be placed

These pTripID how will you deallocate them? Probably there is a

void XYZFreeTrip(Trip tripID);

In this case, you'll have to collect all the tripID you get and free them in the IDisposable.

Now if Trip is an handle, then you have two options:

  • Your code is x86 only (because for example the PInvoke DLL is x86 only): sizeof(int) == sizeof(int*) == IntPtr.Size, so you can use the Wrapping Unmanaged Resources - Defining Level 0 Types for Pointers (The Intermediate Case)

  • Your code is x86 and x64: the Wrapping Unmanaged Resources - Defining Level 0 Types for Non-Pointer Data (The Hard Case) :-( (instead of ushort you have a int)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • The tripID's are just integers. At least how I am currently getting them. But there is a DeleteTrip method. There is also a InitSrv method and CleanupSrv method that should be called once per app start. The trip methods would be called multiple times. I guess I don't understand why idisposible would be useless. It needs to be cleaned up somehow. – nportelli May 04 '15 at 14:54
  • @nportelli What you have to dispose is the `Trip` then. They must be encapsulated in a `SafeHandle` and disposed with `DeleteTrip` – xanatos May 04 '15 at 14:56
  • Hmm, ok starting to make some sense I think. Even though right now we are getting tripID as an int? And it returning a negative number for an error has nothing to do with anything does it? And yes I was assuming I had the hard case and I was trying to implement it with an in instead of ushort. But not around the tripID. But based on the hresult it returns on the initial InitSrv method. Changing it around the tripID makes sense thought...well as much as this will ever make sense to me. – nportelli May 04 '15 at 14:59
  • @nportelli Even if you see them as `int`, there is somewhere a table that for each `int` value associates a pointer to some resouce. When you receive an error, I don't know how you have to handle the `Trip`. "sane" implementations don't return a "valid" object on error (and often zero it), but you'll have to check. – xanatos May 04 '15 at 15:02