Create a file in C # and pass it as IntPtr to a COM object

I work with some COM objects in C #, one of them has a SetLogFile function that accepts IntPtr. How can I create a file in C # and then pass it as IntPtr for this COM function?

EDIT: function requires an open file descriptor: http://msdn.microsoft.com/en-us/library/aa915939.aspx

+3
source share
3 answers

You are a bit vague about what you need to pass as a “file” - if you have a FileStream, you can pass FileStream.Handle as IntPtr (assuming it expects a HANDLE value)

. " Type HANDLE # ++ DLL".

+4

, SetLogFile IntPtr. COM- # , .

var ptr = Marshal.GetIUnknownForObject(theParameter);
try {
  theComObject.SetLogFile(ptr);
} finally {
  Marshal.Release(ptr);
}

Win32. API , , .

+2

, SafeFileHandle FileStream. , , P/Invoke CreateFile.

0

Source: https://habr.com/ru/post/1730567/


All Articles