Jim Jones

Jim Jones

  • NA
  • 10
  • 0

Problem creating a non-fs handle

Mar 9 2010 2:14 PM

Have a function as follows once I forage and translate everything into basic types:

unsigned int DAOpenDocument(HANDLE lphDoc, unsigned int dwSpecType, void* pSpec, unsigned int dwFlags);
The documentation says that lphDoc is "a Pointer to a handle that will be filled with a value uniquely identifying the document to data access. The developer uses this handle in subsequent calls to data access to identify this particular input file. This is not an operating system file handle.
Once I plugged this into P/Invoke Iterop Assistant I get:
 public partial class NativeMethods {
    ///Return type: unsigned int
    ///lphDoc: HANDLE->void*
    ///dwSpecType: unsigned int
    ///pSpec: void*
    ///dwFlags: unsigned int
    {System.Runtime.InteropServices.DllImportAttribute("sccda.dll", EntryPoint="DAOpenDocument")]
    public static extern uint DAOpenDocument(System.IntPtr lphDoc, uint dwSpecType, System.IntPtr pSpec, uint dwFlags);
}
I use lphDoc = (System.IntPtr)GCHandle.Alloc(new Object(), GCHandlerType.Pinned);
I know this allocates memory the sizeof Object, since I don't know what is going in there and I want it to be long lived, this seemed like a reasonable assumption. Is it?
pSpec contains a file path which I've done before  using System.IntPtr path = Marshal.StringToHGlobalAnsi(String filepath);
It turns out that the new Object() does work either. I get "System.ArguementException: Object contains non-primitive or non-blittable data".
Any better alternatives?
Jim