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
  1. Home
  2. General Programming
  3. C#
  4. struct and pointer question in c#

struct and pointer question in c#

Scheduled Pinned Locked Moved C#
helpquestioncsharpc++json
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.
  • R Offline
    R Offline
    Rulala
    wrote on last edited by
    #1

    Hi all ,Forgive my bad english,please. I'm new user in c#.I have one problem about struct and pointer. I want to create c# API that call DLL file in c++ . //DLL sample as below(c++): struct TESTA { DWORD A; WORD B; WORD C; BYTE D[16]; }; typedef TESTA* LPTESTA; BOOL CBook::InitialButton(LPTESTA pInfo) { //statement return true; } //C# code as below: unsafe public class callDLL { [StructLayout(LayoutKind.Sequential)] public struct TESTA { public int A; public ushort B; public ushort C; [MarshalAs(UnmanagedType.ByValArray, SizeConst= 16)] public byte[] D; } [DllImport("dllname.dll")] unsafe public static extern bool InitialButton(void* pInfo); } public class Initialize_sequence { unsafe static void Main(string[] args) { callDLL.TESTA pInfo=new callDLL.TESTA(); Console.WriteLine(Marshal.SizeOf(pInfo)); callDLL.InitialButton(&pInfo); //error happen :( } } error message show "Can't get position and size of Mananged type" Any one can provide idea for me ? Thanks. hello ALL..^^

    B 1 Reply Last reply
    0
    • R Rulala

      Hi all ,Forgive my bad english,please. I'm new user in c#.I have one problem about struct and pointer. I want to create c# API that call DLL file in c++ . //DLL sample as below(c++): struct TESTA { DWORD A; WORD B; WORD C; BYTE D[16]; }; typedef TESTA* LPTESTA; BOOL CBook::InitialButton(LPTESTA pInfo) { //statement return true; } //C# code as below: unsafe public class callDLL { [StructLayout(LayoutKind.Sequential)] public struct TESTA { public int A; public ushort B; public ushort C; [MarshalAs(UnmanagedType.ByValArray, SizeConst= 16)] public byte[] D; } [DllImport("dllname.dll")] unsafe public static extern bool InitialButton(void* pInfo); } public class Initialize_sequence { unsafe static void Main(string[] args) { callDLL.TESTA pInfo=new callDLL.TESTA(); Console.WriteLine(Marshal.SizeOf(pInfo)); callDLL.InitialButton(&pInfo); //error happen :( } } error message show "Can't get position and size of Mananged type" Any one can provide idea for me ? Thanks. hello ALL..^^

      B Offline
      B Offline
      Bret Mulvey
      wrote on last edited by
      #2

      Instead of using the "unsafe" keyword in C# and using void* in the extern function signature, try this: public static extern bool InitialButton(ref TESTA pInfo); and in the call to the function you would just do callDLL.InitialButton(ref pInfo); The marshaller should automatically treat this as a pointer.

      R 1 Reply Last reply
      0
      • B Bret Mulvey

        Instead of using the "unsafe" keyword in C# and using void* in the extern function signature, try this: public static extern bool InitialButton(ref TESTA pInfo); and in the call to the function you would just do callDLL.InitialButton(ref pInfo); The marshaller should automatically treat this as a pointer.

        R Offline
        R Offline
        Rulala
        wrote on last edited by
        #3

        I modified it as your suggestion. It is ok . Thank you very much.:) hello ALL..^^

        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