简体   繁体   中英

C# How to define new pointer struct?

This structure is define at http://msdn.microsoft.com/en-us/library/windows/hardware/ff541621%28v=vs.85%29.aspx

typedef struct _FILTER_MESSAGE_HEADER {
  ULONG     ReplyLength;
  ULONGLONG MessageId;
} FILTER_MESSAGE_HEADER, *PFILTER_MESSAGE_HEADER;

I defined it in C# code as below:

[StructLayout(LayoutKind.Sequential)]
public struct FILTER_MESSAGE_HEADER {
      public uint replyLength;
      public ulong messageId;
};

I only define FILTER_MESSAGE_HEADER in C# code , PFILTER_MESSAGE_HEADER isn't.

How should I do to define PFILTER_MESSAGE_HEADER ??

P/S: I want to define PFILTER_MESSAGE_HEADER to use this struct in a function.

You don't have to (can't) define PFILTER_MESSAGE_HEADER . Just specify it as either out or ref as appropriate.

[DllImport("foo")]
void SomeMethod(ref FILTER_MESSAGE_HEADER lpMessageBuffer);


If you are specifically interested in FilterGetMessage , I'm not sure what if any dll it is exported from, but one possible signature would be as below:

[DllImport(fltmgr, CharSet=CharSet.Unicode, ExactSpelling=true, PreserveSig=false)]
void FilterGetMessage(
    CommunicationPortSafeHandle hPort, 
    ref FILTER_MESSAGE_HEADER lpMessageBuffer,
    uint dwMessageBufferSize,
    IntPtr lpOverlapped);

I used PreserveSig to automatically translate the HRESULT to an exception in the event of failure, the CharSet specification is defensive and results in the need for ExactSpelling . CommunicationPortSafeHandle would be a class which inherits from SafeHandleMinusOneIsInvalid based off of the documentation on FilterConnectCommunicationPort .

You would use this signature as:

FILTER_MESSAGE_HEADER header;
FilterGetMessage(hFilter, ref header, 
    Marshal.SizeOf(typeof(FILTER_MESSAGE_HEADER)), IntPtr.Zero);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM