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. How to Map a C++ Struct from a DLL into C#

How to Map a C++ Struct from a DLL into C#

Scheduled Pinned Locked Moved C#
csharpc++tutorial
8 Posts 4 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.
  • F Offline
    F Offline
    Felix Nielsen
    wrote on last edited by
    #1

    Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)

    L F J 3 Replies Last reply
    0
    • F Felix Nielsen

      Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      When you have a struct of simple datatypes creating the struct should be fairly easy.

      [StructLayout(LayoutKind.Seqential)]
      public struct Transaction {
      [MarshalAs(UnmanagedType.LPStr, SizeConst=21)] string card_number;
      [MarshalAs(UnmanagedType.U4)] int card_expirymonth;
      [MarshalAs(UnmanagedType.U4)] int card_expiryyear;
      }

      Of course this is untested code, but I believe that is correct attributes used. HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

      L 1 Reply Last reply
      0
      • L Lost User

        When you have a struct of simple datatypes creating the struct should be fairly easy.

        [StructLayout(LayoutKind.Seqential)]
        public struct Transaction {
        [MarshalAs(UnmanagedType.LPStr, SizeConst=21)] string card_number;
        [MarshalAs(UnmanagedType.U4)] int card_expirymonth;
        [MarshalAs(UnmanagedType.U4)] int card_expiryyear;
        }

        Of course this is untested code, but I believe that is correct attributes used. HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hi James, THX - I'll try that :), btw. is it possible to use the sizeof() function on the struct? Felix

        1 Reply Last reply
        0
        • F Felix Nielsen

          Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)

          F Offline
          F Offline
          Filip
          wrote on last edited by
          #4

          I have the same problem i have to pass a pointer to the DOC_INFO_1 structure via the winapi StartDocPrinter(handle, data, *DOC_INFO_1) DOC_INFO_1 { LPTSTR pdocname LPTSTR pdatatype LPTSTR poutputfile } can anyone help me?

          1 Reply Last reply
          0
          • F Felix Nielsen

            Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            Oops, I think there is an error in what I suggested trying.

            [StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
            public struct Transaction {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
            [MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
            [MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
            }

            What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

            F 2 Replies Last reply
            0
            • J James T Johnson

              Oops, I think there is an error in what I suggested trying.

              [StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
              public struct Transaction {
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
              [MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
              [MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
              }

              What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

              F Offline
              F Offline
              Felix Nielsen
              wrote on last edited by
              #6

              Hi James, Thanks, I did some testing Yesterday, and it did'nt work, when I passed the struct to the DLL, it got a the pointer to "card_number", and the DLL was unable to fetch the other fields. Felix

              1 Reply Last reply
              0
              • J James T Johnson

                Oops, I think there is an error in what I suggested trying.

                [StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
                public struct Transaction {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
                [MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
                [MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
                }

                What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                F Offline
                F Offline
                Felix Nielsen
                wrote on last edited by
                #7

                Hi James, It seems to working as a CHARM :) Thanks again :) Felix

                J 1 Reply Last reply
                0
                • F Felix Nielsen

                  Hi James, It seems to working as a CHARM :) Thanks again :) Felix

                  J Offline
                  J Offline
                  James T Johnson
                  wrote on last edited by
                  #8

                  No problem, I learned a bit myself :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                  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