Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# Interoperation Question

Started by Sophic Delusion Oct 26, 2005 at 8:08 PM 3 replies 800+ views
Original Post
Sophic Delusion
Sophic Delusion
Is there a facility to use unions in C#? I'm making a class to work with consoles in Win32 and there's a couple things I'm not quite sure of. Here's an example of something I need to do:


typedef struct _INPUT_RECORD {
    WORD EventType;
    union {
        KEY_EVENT_RECORD KeyEvent;
        MOUSE_EVENT_RECORD MouseEvent;
        WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
        MENU_EVENT_RECORD MenuEvent;
        FOCUS_EVENT_RECORD FocusEvent;
    } Event;
} INPUT_RECORD;



I don't know quite how to convert that into a C# structure. And also, as far as void pointers go, how would I go about importing this function? Could I do it without using unsafe code (void*)?


HANDLE CreateConsoleScreenBuffer(
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  const SECURITY_ATTRIBUTES* lpSecurityAttributes,
  DWORD dwFlags,
  LPVOID lpScreenBufferData
);



-- Sophic Delusion
Washu
Washu
void* == System.IntPtr.

Lookup the StructLayoutAttribte and and the FieldOffsetAttribute for your first structure.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Sophic Delusion
Sophic Delusion
Thanks Washu... But I guess I'm still ignorant. The .NET docs use the SYSTEM_INFO structure as an example for how to use the FieldOffset attribute. This structure uses an anonymous union with one field and a struct with two fields inside it. Now the .NET docs don't give a complete work of that structure in C#, so I wasn't quite sure what they mean't by their example. If you could elaborate on how you use the FieldOffset attribute with anonymous unions I would greatly appreciate it. Once again, I apologize for being ignorant to whatever it is your trying to tell me. I still have lots to learn about things :)
-- Sophic Delusion
mutex
mutex
If all you're doing is P/Invoking, try PInvoke.net for a lot of native definitions. Here's one for INPUT_RECORD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.