Calling C Library DLLs from C Sharp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KYAW KYAW OO
    New Member
    • Jan 2007
    • 30

    Calling C Library DLLs from C Sharp

    Dear All,

    I am trying to call the data type unsigned char * included in the C DLL and whole structure from C Sharp.
    My draft code is below

    Code:
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
            public struct [B]Converted[/B]
            {
                public int width;
                public int height;
                public[B] IntPtr [/B]Buf;  // which is [B]unsigned char* in C[/B] and is it           correct ?
                //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    
            };
    
    Converted c = new Converted();
    [DllImport("Test.dll", CharSet = CharSet.Auto, EntryPoint="Loading")]
    public static extern [B]Converted [/B]Loading([B]ref [/B]IntPtr buf, int size, int type); // shoul I use[B] ref[/B]?
    
    i.data is syste.array
    c.Buf = Loading(ref i.data, i.size, type);
    I encountered
    1. to pass unsigned char * from C to C#
    2. return structure of Converted from C to C#
    3. System.array of C# to IntPtr of C# or unsigned char *

    I hope some one will advise me to get it well for my dll and looking forward to hearign from you soon.


    Thanks in advance
  • KYAW KYAW OO
    New Member
    • Jan 2007
    • 30

    #2
    Calling C Library DLLs from C Sharp and MarshalDirectiv eException Error

    Dear All,

    I am trying to call the data type unsigned char * included in the C DLL and whole structure from C Sharp.

    My draft code is below

    Code:
    // dll.cpp
    // Image Display and BufferMemory
    typedef
    struct ConvertedImage 
    {
    int height; 
    int size; 
    unsigned char *greyBMPBuf; 
    }ConvertedImage; 
    
    MATCH_IMPORTS ConvertedImage Convert2BMPGrey(unsigned char *bufFromDevice, int size, int image_type);
     
    // main.cs
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)] 
    public struct ConvertedImage 
    {
    public int width; 
    public int height; 
    public int size; // size = imagesize is imagewidth * imgaeheight 
    [MarshalAs(UnmanagedType.LPArray)] 
    public byte[] greyBMPBuf; 
    };
    
    ConvertedImage cImage; 
     
    [DllImport("Match.dll", CharSet = CharSet.Auto, EntryPoint = "Convert2BMPGrey")] 
    public static extern ConvertedImage Convert2BMPGrey([MarshalAs(UnmanagedType.LPArray)]byte[] bufImage, int size, int image_type);
    I encountered
    1. to pass unsigned char * from C to C#
    2. return structure of Converted from C to C#
    3. System.array of C# to IntPtr of C# or unsigned char *
    4. the error message"Method' s type signature is not PInvoke compatible"

    Please advise me for my dll and C# InteropServices and looking forward to hearing from you good news.


    Thanks in advance

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged

      Comment

      Working...