Trouble with passing handle to managed object using PInvoke

大城市里の小女人 提交于 2019-12-08 06:39:30

GCHandle has no place for that parameter. That's used for operations like pinning managed memory. This handle is provided by the unmanaged code. It is an opaque pointer.

The Delphi code, which incidentally I know from your other question to be a little wonky, declares this as a THandle. That's semantically off because I don't think this handle is really a Win32 HANDLE.

However, it is safe to say that this handle is just an IntPtr. You deal with that parameter exactly as I stated in your previous question:

out IntPtr handle

The function yields a handle to its state, to the thing that the function just created. You remember it and then pass it on to the other functions that need that handle.

After investigation, i found that here handle is a pointer to instance of class( where CreateISCDriverInstance is defined). After passing the handle to Siebel it will be trying to call methods of Driver class by reference.

class Driver 
{

 private static readonly GCHandle gHandle;
 static Driver
{
// ......
   gHandle = GCHandle.Alloc(DriverEntryPoints.Instance, GCHandleType.Pinned);
}

  [STAThread]
        [DllExport("CreateISCDriverInstance", CallingConvention = CallingConvention.Cdecl)]
        public static int CreateIscDriverInstance([MarshalAs(UnmanagedType.LPWStr)] string mediaTypeStr,
            [MarshalAs(UnmanagedType.LPWStr)] string languageCode,
            [MarshalAs(UnmanagedType.LPWStr)] string connectString,
            [In] ref IscKvParamList datasetParams,
            out IntPtr handle)
{
//...
              handle = GCHandle.ToIntPtr(gHandle);
//...
}

}

p.s. Also calling conversions is Cdecl.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!