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 / C++ / MFC
  4. Passing an array of structs from c# to unmanaged c++ dll

Passing an array of structs from c# to unmanaged c++ dll

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++data-structuresdebuggingtutorial
3 Posts 3 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.
  • M Offline
    M Offline
    Marek Konieczny
    wrote on last edited by
    #1

    Hi, I need to pass a relativly complicated struct into a dll calling a CORBA Service which has to be written in vc6.0 and it has to contain many elements which are necessary to create an input for a service. Unfortunately I can see only the first array element, other are ignored. Here an example of c# code: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStrStruct3 { public string FirstName; public string FamilyName; public int size; } [DllImport(@"C:\Projects\CorbaLib60\Debug\CorbaLib.dll")] private static extern int TestMe(ref MyStrStruct3[] pstr); MyStrStruct3[] Names = new MyStrStruct3[2]; Names[0].FamilyName = "Konieczny"; Names[0].FirstName = "Marek"; Names[0].size = 3; Names[1].FamilyName = "OtherName"; Names[1].FirstName = "OtherFirst"; Names[1].size = 2; int miki1 = TestMe(ref Names); C++ code look like this: typedef struct _MyStruct2 { char* FirstName; char* FamilyName; UINT size; } MyStruct2; int TestMe(MyStruct2* pStruct[]) { int Size = 2; for (int i=0; isize; char* miki = pStruct[i]->FamilyName; char* miki1 = pStruct[i]->FirstName; } return 42; } I define export : __declspec(dllexport) int TestMe(MyStruct2* pStruct[2]); It works, but only for first element. Marek

    J T 2 Replies Last reply
    0
    • M Marek Konieczny

      Hi, I need to pass a relativly complicated struct into a dll calling a CORBA Service which has to be written in vc6.0 and it has to contain many elements which are necessary to create an input for a service. Unfortunately I can see only the first array element, other are ignored. Here an example of c# code: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStrStruct3 { public string FirstName; public string FamilyName; public int size; } [DllImport(@"C:\Projects\CorbaLib60\Debug\CorbaLib.dll")] private static extern int TestMe(ref MyStrStruct3[] pstr); MyStrStruct3[] Names = new MyStrStruct3[2]; Names[0].FamilyName = "Konieczny"; Names[0].FirstName = "Marek"; Names[0].size = 3; Names[1].FamilyName = "OtherName"; Names[1].FirstName = "OtherFirst"; Names[1].size = 2; int miki1 = TestMe(ref Names); C++ code look like this: typedef struct _MyStruct2 { char* FirstName; char* FamilyName; UINT size; } MyStruct2; int TestMe(MyStruct2* pStruct[]) { int Size = 2; for (int i=0; isize; char* miki = pStruct[i]->FamilyName; char* miki1 = pStruct[i]->FirstName; } return 42; } I define export : __declspec(dllexport) int TestMe(MyStruct2* pStruct[2]); It works, but only for first element. Marek

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      I am surprised it worked at all!    The structure has pointers to memory and unless you are sticking the memory for the strings onto the end of the memory for the structure, I am not seeing how the memory for the strings are getting into the DLL.    This seems like more of a C# problem than a problem with the DLL you are calling - might be better to post this to the C#-related message board.    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      See DeleteFXPFiles

      1 Reply Last reply
      0
      • M Marek Konieczny

        Hi, I need to pass a relativly complicated struct into a dll calling a CORBA Service which has to be written in vc6.0 and it has to contain many elements which are necessary to create an input for a service. Unfortunately I can see only the first array element, other are ignored. Here an example of c# code: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStrStruct3 { public string FirstName; public string FamilyName; public int size; } [DllImport(@"C:\Projects\CorbaLib60\Debug\CorbaLib.dll")] private static extern int TestMe(ref MyStrStruct3[] pstr); MyStrStruct3[] Names = new MyStrStruct3[2]; Names[0].FamilyName = "Konieczny"; Names[0].FirstName = "Marek"; Names[0].size = 3; Names[1].FamilyName = "OtherName"; Names[1].FirstName = "OtherFirst"; Names[1].size = 2; int miki1 = TestMe(ref Names); C++ code look like this: typedef struct _MyStruct2 { char* FirstName; char* FamilyName; UINT size; } MyStruct2; int TestMe(MyStruct2* pStruct[]) { int Size = 2; for (int i=0; isize; char* miki = pStruct[i]->FamilyName; char* miki1 = pStruct[i]->FirstName; } return 42; } I define export : __declspec(dllexport) int TestMe(MyStruct2* pStruct[2]); It works, but only for first element. Marek

        T Offline
        T Offline
        Tim Paaschen
        wrote on last edited by
        #3

        You should look up the MarshalAs attribute, especially with the UnmanagedType.LPTStr and UnmanagedType.LPArray parameters. And I have to agree that this question would fit better into the C# forum.

        Regards, Tim

        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