Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. help! how can I rewrite struct with union in C#?

help! how can I rewrite struct with union in C#?

Scheduled Pinned Locked Moved C#
questioncsharpc++jsonhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Li kai Liu Angus
    wrote on last edited by
    #1

    Hi, In C or C++,some structures are defined with union. How can we rewrite these structure in C# so that we can use them as parameters and pass them into API functions? And, What's the correspondent type in C# to PVOID type in C? Thanks in advance! Angus Liu

    R 1 Reply Last reply
    0
    • L Li kai Liu Angus

      Hi, In C or C++,some structures are defined with union. How can we rewrite these structure in C# so that we can use them as parameters and pass them into API functions? And, What's the correspondent type in C# to PVOID type in C? Thanks in advance! Angus Liu

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      ykliu wrote: In C or C++,some structures are defined with union. How can we rewrite these structure in C# so that we can use them as parameters and pass them into API functions? e.g. union U { int x; int y; } can be written in C# as [StructLayout(LayoutKind.Explicit)] class U { [FieldOffset(0)] int x; [FieldOffset(0)] int y; } ykliu wrote: And, What's the correspondent type in C# to PVOID type in C? There are two equivalents of PVOID in C# 1. void* this is when you are using C# pointers. Something which you should never do. 2. IntPtr this is used to encapsultae unmanaged pointers. Use this in conjunction with Marshal.StructureToPtr, Marshal.AllocCoTaskMem etc. functions. Look up all the functions in Marshal class it would be very helpful.

      L 1 Reply Last reply
      0
      • R Rama Krishna Vavilala

        ykliu wrote: In C or C++,some structures are defined with union. How can we rewrite these structure in C# so that we can use them as parameters and pass them into API functions? e.g. union U { int x; int y; } can be written in C# as [StructLayout(LayoutKind.Explicit)] class U { [FieldOffset(0)] int x; [FieldOffset(0)] int y; } ykliu wrote: And, What's the correspondent type in C# to PVOID type in C? There are two equivalents of PVOID in C# 1. void* this is when you are using C# pointers. Something which you should never do. 2. IntPtr this is used to encapsultae unmanaged pointers. Use this in conjunction with Marshal.StructureToPtr, Marshal.AllocCoTaskMem etc. functions. Look up all the functions in Marshal class it would be very helpful.

        L Offline
        L Offline
        Li kai Liu Angus
        wrote on last edited by
        #3

        Hi Rama, Thank you very much, though, I don't understand all of them, especially the Marshal part. I will keep reading some document before asking another question. Angus Liu

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups