English 中文(简体)
土著管理沼泽地的随机价值
原标题:Native to managed marshaling has random values

我对我的缓冲地带的一些价值观来自何方以及我为什么会迷惑不解地理解。 执行。

Here is my case: In my system I have a native application that talks to a managed service through named pipes. The native application uses WriteFile(pipehandle, &msg, sizeof(msg), &cbBytes, NULL) to send data held by the following structs:

struct NotificationMessageHeader
{
    __int32 A;
    __int64 B;
    __int32 MessageType;
    __int32 D;
};

struct NotificationMessageA
{
    NotificationMessageHeader Header;
    unsigned char X;
    wchar_t Y[MAX_PATH];
};

管理服务有这些结构管理的版本,包括:

[StructLayout(LayoutKind.Sequential)]
public struct NotificationMessageHeader
{
    public UInt32 A;
    public UInt64 B;
    public UInt32 MessageType;
    public UInt32 D;
}

[StructLayout(LayoutKind.Sequential)]
public struct NotificationMessageA
{
    public NotificationMessageHeader Header;
    [MarshalAs(UnmanagedType.I1)]
    public byte X;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Y;
}

当我从本土应用中发送数据时,我所做的第一件事就是将缓冲转化为一种通用结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct GenericNotificationMessage
{
    public NotificationMessageHeader Header;
}

在我确信电文类型得到支持之后,确定电文类型是哪一种电文类型,作为利用这一功能的适当结构,将这一缓冲的其余部分编码为:

    T GetMessageAsStructure<T>(object data)
        where T : struct
    {
        T output = default(T);
        GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
        try
        {
            IntPtr dataPtr = handle.AddrOfPinnedObject();
            output = (T)Marshal.PtrToStructure(dataPtr, typeof(T));
        }
        finally
        {
            handle.Free();
        }
        return output;
    }

还有两条电话:<代码> GetMessageAsStructure。 一种类型的论据是Generic notificationMessage,该编码只贴上了标题,而且正确操作——我照预期在标题领域获得数值。 然后,在我发现电文属于我称之为<代码>的类型。 GetMessageAsStructure with the category para amount - in this case NotificationMessageA.

...... 在此,事情开始出现坏。 CLR没有进入例外。 我试图审视我在管理一侧的缓冲地带的价值观,例如,当我寄出像:

    NotificationMessageA msg = { };
    memset(&msg, 0, sizeof(msg));

    msg.Header.A = 2;
            msg.Header.B = 999;
    msg.Header.MessageType = 1;
    msg.Header.D = 3;
    msg.X = 64;
    wcscpy(msg.Y, L"somexec.exe");

    DWORD written = 0;
    WriteFile(_hPipe, &msg, sizeof(msg), &written, NULL);

管理下的缓冲值如下:

[0] 2 <---- This shouldn t be at index 3?
[1] 0
[2] 0
[3] 0
[4] 0
[5] 0
[6] 0
[7] 0
[8] 231 <--- WTF is this? should t it start at index 11?
[9] 3
[10] 0
[11] 0
[12] 0
[13] 0
[14] 0
[15] 0
[16] 1
[17] 0
[18] 0
[19] 0
[20] 3
[21] 0
[22] 0
[23] 0
[24] 64
[25] 0
[26] 115
[27] 0
[28] 111
[29] 0
[30] 109
[31] 0
[32] 101
[33] 0
[34] 101
[35] 0
[36] 120
[37] 0
[38] 101
[39] 0
[40] 99
[41] 0
[42] 46
[43] 0
[44] 101
[45] 0
[46] 120
[47] 0
[48] 101
[49] 0
[50] 0

但是,由于Im使用={><>>>>>>/code,在C++中,Imm使用“使初始化成为违约值,并且不会影响配对加固结构,但情况并非如此,因为即使使用memset(......, 0, ......),tt t 影响收受的tes。

仅加一只头盔就完美地编码,只有在I m试图打上其他结构的编码时,这个结构有一毫升的系统。 执行。

而且,经过管理的缓冲地带没有我期望它通过审视我的结构来做到这一点。

为什么?

更令人费解的是,视像演播室报告说,所投掷的东西是执行评价,而MSDN说,这一例外不是由时间推移而来的,而是过时的。

我在把这扼杀起来时做了什么错误呢?

最佳回答

您正在担任协调人,但在本地法典中,它是一种结构内的缓冲,而不是指点人。 未管理的Type.ByValTStr

问题回答

除Mimo外,还回答:

Index 0 to 3: MessageHeader.A, little endian
Index 4 to 7: padding, since B must be 8 byte aligned
Index 8 to 15: B, little endian
...
Index 24: X
Index 25: padding
Index 26: start of the wchar_t array Y, but will be incorrectly interpreted as a pointer




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...