CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2010
    Posts
    7

    [RESOLVED] dll returns C structure with dynamic arrays, how to get data in c# ???

    Hello

    I have a DLL written in C and I am struggling with using the functions that return a structure in C#.

    C Code that compiles into dll
    Code:
    typedef struct
    {
    	unsigned int Height;        //!< Height of the image in pixels.
    	unsigned int Width;         //!< Width of the image in pixels.
    	unsgined short ***Pixels;           
    } IMAGE;
    
    //! Creates and initializes image structure and returns pointer to new image
    __declspec(dllexport) IMAGE	CreateImage(unsigned int aHeight, unsigned int aWidth);
    I need to use this function in C#, this is what i tried:
    Code:
    public struct IMAGE
    {
    	int Height;
    	int Width;
    	short[,,] Pixels;
    }
    
    [DLLImport("IMAGE.dll")]
    public static extern IMAGE CreateImage(int aHeight, int aWidth);
    But when I call
    Code:
    IMAGE image = new IMAGE();
    image = CreateImage(100,100);
    I get the error: Method's type signature is not PInvoke compatible

    Other functions that return plain integers work just fine. Someone please explain how I can use a structure created by DLL.

    Thanks

  2. #2
    Join Date
    Jun 2010
    Posts
    7

    Re: dll returns C structure with dynamic arrays, how to get data in c# ???

    Solved it .....

    Code:
    public struct IMAGE
    {
           public Int32 Height;
           public Int32 Width;
           public IntPtr Pixels;
    }
    
    [DllImport("Image.dll")]
    public static extern IMAGE CreateImage(Uint32 Height, Uint32 Width);
    after calling the CreateImage function I can use Marshal.ReadIntPrt(Pixels) and Marshal.ReadInt16(IntPtr) to get the data .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured