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. Unmanaged C++ DLL Calling

Unmanaged C++ DLL Calling

Scheduled Pinned Locked Moved C#
helpcsharpc++
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.
  • S Offline
    S Offline
    skinnyreptile168
    wrote on last edited by
    #1

    I have an unmanaged C++ DLL which has the following structures, typedef struct {char str[200]; } MYARRAY1; typedef struct {MYARRAY1 myarray1struct;} MYARRAY2; typedef struct {MYARRAY2 myarray1struct[5];} MYARRAY3; To return MYARRAY2, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray2( MYARRAY2 *pStruct) To return MYARRAY3, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray3( MYARRAY3 *pStruct) In C#, I declared the following structures, [StructLayout(LayoutKind.Sequential)] public struct MyArray1 { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)] public String str; } [StructLayout(LayoutKind.Sequential)] public struct MyArray2 { public MyArray1 tempArray1; } [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; } I do not have any problem using DLLImport call to C++ DLL to get data in MyArray2, however when I try to get data in MyArray3 with the following code, all data in testArray3 are empty. MyArray3 testArray3 = new MyArray3(); testArray3.tempArray2 = new MyArray2[ 5 ]; CallTestArray3(ref testArray3); Any help is appreciated!!!

    N 1 Reply Last reply
    0
    • S skinnyreptile168

      I have an unmanaged C++ DLL which has the following structures, typedef struct {char str[200]; } MYARRAY1; typedef struct {MYARRAY1 myarray1struct;} MYARRAY2; typedef struct {MYARRAY2 myarray1struct[5];} MYARRAY3; To return MYARRAY2, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray2( MYARRAY2 *pStruct) To return MYARRAY3, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray3( MYARRAY3 *pStruct) In C#, I declared the following structures, [StructLayout(LayoutKind.Sequential)] public struct MyArray1 { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)] public String str; } [StructLayout(LayoutKind.Sequential)] public struct MyArray2 { public MyArray1 tempArray1; } [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; } I do not have any problem using DLLImport call to C++ DLL to get data in MyArray2, however when I try to get data in MyArray3 with the following code, all data in testArray3 are empty. MyArray3 testArray3 = new MyArray3(); testArray3.tempArray2 = new MyArray2[ 5 ]; CallTestArray3(ref testArray3); Any help is appreciated!!!

      N Offline
      N Offline
      NrmMyth
      wrote on last edited by
      #2

      vitowong wrote:

      [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; }

      Underlined should be also marshaled.

      S 1 Reply Last reply
      0
      • N NrmMyth

        vitowong wrote:

        [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; }

        Underlined should be also marshaled.

        S Offline
        S Offline
        skinnyreptile168
        wrote on last edited by
        #3

        MyArray2 is based on MyArray1, and MyArray1 has been marshaled. Is it need for MyArray2[] be marshaled? I tried to marshal as AsAny, but it doesn't work. What do I be marshal as? Thanks.

        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