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. interop struct types

interop struct types

Scheduled Pinned Locked Moved C#
questioncsharpc++comdata-structures
2 Posts 1 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.
  • T Offline
    T Offline
    Tym
    wrote on last edited by
    #1

    I have a struct to mimic a c++ struct...I have converted types as follows: C++ C# ---- ---- WORD ushort DWORD uint char[] string the imported function passes a pointer to the struct, fills it with fun-facts and gives it back more useful than before. The only thing I currently want out of this is the char[] member of the struct and things are not working out. Here's what I have done: - I am using CharSet.Auto in the structLayout and dllImport attributes. - in the function prototype : "ref STRUCTTYPE structname" - before calling the function, initializing struct by using "new" - before calling the function, initializing the string by creating a StringBuilder buffer of appropriate size and "ToString-ing" it to the struct.string member. problem is, when I call the function and access the string, it still contains the text that I used to initialize it in the first place. I haven't done enough testing to determine the problem, but I wanted to see if you have any ideas. My first question is to verify that using a c# string to represent the c++ char[] is all cool. I think it should be. [Edit]:sigh: Almost, need [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256] because it is a fixed char array [/Edit] What I was thinking of while typing right now, is this: in c++ structs are reference variables. in c# they are value variables. if i wanted to pass a c# struct to a c++ function, i would use the ref modifyer. Since my function wants a pointer to the struct, should I be using something like "ref IntPtr structname" ???? is this my problem and my solution? [Edit]:doh: not quite, should work with "ref STRUCTTYPE structname"... if the struct were represented using a class, then would need [In,Out] instead of ref. See link in next post for details. Can't wait to get home and try it![/Edit] Tym!

    T 1 Reply Last reply
    0
    • T Tym

      I have a struct to mimic a c++ struct...I have converted types as follows: C++ C# ---- ---- WORD ushort DWORD uint char[] string the imported function passes a pointer to the struct, fills it with fun-facts and gives it back more useful than before. The only thing I currently want out of this is the char[] member of the struct and things are not working out. Here's what I have done: - I am using CharSet.Auto in the structLayout and dllImport attributes. - in the function prototype : "ref STRUCTTYPE structname" - before calling the function, initializing struct by using "new" - before calling the function, initializing the string by creating a StringBuilder buffer of appropriate size and "ToString-ing" it to the struct.string member. problem is, when I call the function and access the string, it still contains the text that I used to initialize it in the first place. I haven't done enough testing to determine the problem, but I wanted to see if you have any ideas. My first question is to verify that using a c# string to represent the c++ char[] is all cool. I think it should be. [Edit]:sigh: Almost, need [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256] because it is a fixed char array [/Edit] What I was thinking of while typing right now, is this: in c++ structs are reference variables. in c# they are value variables. if i wanted to pass a c# struct to a c++ function, i would use the ref modifyer. Since my function wants a pointer to the struct, should I be using something like "ref IntPtr structname" ???? is this my problem and my solution? [Edit]:doh: not quite, should work with "ref STRUCTTYPE structname"... if the struct were represented using a class, then would need [In,Out] instead of ref. See link in next post for details. Can't wait to get home and try it![/Edit] Tym!

      T Offline
      T Offline
      Tym
      wrote on last edited by
      #2

      I found this on MSDN (of all places) that I think will fix my problem. Maybe it will help someone else with a similar prob: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconmarshalingclassesstructuresunions.asp[^]

      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