locked
[help]FilterSendMessage in C# RRS feed

  • Question

  • Hello Members,

    I'm developing a .net program about minifilter. A sample in DDK called minispy using C language works fine. Now I tried to call it in C# through WindowsAPI.

    The call of FilterConnectCommunicationPort works fine from C# with the follwing code:

    [DllImport("fltlib", SetLastError = false)]
    public static extern int FilterConnectCommunicationPort([MarshalAs(UnmanagedType.LPWStr)]
                string portName,
                uint options,
                IntPtr context,
                uint sizeOfContext,
                IntPtr securityAttributes,
                IntPtr portPtr);

    IntPtr portPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
    string portName = "\\MiniSpyPort";
    int status = FilterConnectCommunicationPort(
                    portName,
                    0,
                    IntPtr.Zero,
                    0,
                    IntPtr.Zero,
                    portPtr);

    Status is zero, so the result is a successful.

    But, after that, I call FilterGetMessage from the clientside it results in a status of -2147024890 by the following code:

    [DllImport("fltlib", SetLastError = true)]
    public static extern int FilterSendMessage(
                IntPtr hPort,
                IntPtr lpInBuffer,
                uint dwInBufferSize,
                IntPtr lpOutBuffer,
                int dwOutBufferSize,
                out int lpBytesReturned
                );

    int status = FilterSendMessage(portPtr, IntPtr.Zero, 0, IntPtr.Zero, 0, out size);

    I think there is something wrong with the parameters, in particular, the incoming buffer.

    Struct of incoming buffer in the sample are as follows:

    typedef enum _MINISPY_COMMAND {

        GetMiniSpyLog,
        GetMiniSpyVersion

    } MINISPY_COMMAND;

    typedef struct _COMMAND_MESSAGE {
        MINISPY_COMMAND Command;
        ULONG Reserved;  // Alignment on IA64
        UCHAR Data[];
    } COMMAND_MESSAGE, *PCOMMAND_MESSAGE;

    What it will be in C#, and how to call FilterSendMessage successful?

    Thanks.

    • Edited by Tale Xu Thursday, February 17, 2011 8:27 AM fix
    Thursday, February 17, 2011 8:24 AM

Answers

  • First, that error code, -2147024890, is really 0x80070006, and means "The handle is invalid". Set your debugger to display hexadecimal.

    From the snippet you posted, you have never opened a handle to hPort. Nomally the Hungarian desginator of "h" on "hPort" is a clue that value is a handle and is returned normally by doing a C++ CreateFile. You'll need to add another PINVOKE for CreateFile, or possibly "CreateNamePipe" handle.

    In addition, this question is really NOT concerned with the development of Windows device drivers and the WDK. You will have better chance of getting help over in a the .NET and C#  forums.


    Gary G. Little NanoTelesis Systems, LLC
    Thursday, February 17, 2011 12:30 PM

All replies

  • First, that error code, -2147024890, is really 0x80070006, and means "The handle is invalid". Set your debugger to display hexadecimal.

    From the snippet you posted, you have never opened a handle to hPort. Nomally the Hungarian desginator of "h" on "hPort" is a clue that value is a handle and is returned normally by doing a C++ CreateFile. You'll need to add another PINVOKE for CreateFile, or possibly "CreateNamePipe" handle.

    In addition, this question is really NOT concerned with the development of Windows device drivers and the WDK. You will have better chance of getting help over in a the .NET and C#  forums.


    Gary G. Little NanoTelesis Systems, LLC
    Thursday, February 17, 2011 12:30 PM
  • I found the interface api difficult to pInvoke-ify so instead I wrapped it in simpler task-oriented apis of my own and pInvoked those. Depends on your use case if this is appropriate to your needs, but I did not need the entire filtManager api, I needed my service to accomplish specific tasks.
    Mark Roddy Windows Driver and OS consultant www.hollistech.com
    Thursday, February 17, 2011 2:24 PM
  • From the snippet you posted, you have never opened a handle to hPort. Nomally the Hungarian desginator of "h" on "hPort" is a clue that value is a handle and is returned normally by doing a C++ CreateFile. You'll need to add another PINVOKE for CreateFile, or possibly "CreateNamePipe" handle.
    That's what the FilterConnectCommunicationPort() call does.  Being that I'm only just beginning to comprehend the P/Invoke stuff, I found it considerably easier to write a thin C++ Interop shim around the filter manager library, no MarshalAs, no explicit offsets in the struct definition, no "unsafe" declarations.  "It just works".
    Thursday, February 17, 2011 5:38 PM
  • Wulll ... That's what I did as well. It was a heck of a lot easier to PINVOKE IntPtr GetFileHandle("<fileName>") from my own DLL than doing the same thing on CreateFile from Win32.
    Gary G. Little NanoTelesis Systems, LLC
    Thursday, February 17, 2011 6:43 PM