DllImport problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrssuper
    New Member
    • Apr 2009
    • 2

    DllImport problem

    Hey All,


    How do I declare a following types:

    Code:
     char** 
     unsigned char**
    in C# DllImport function?

    C++ function signature
    Code:
    -----------------------------------------------------------------------------------------------------------
    extern "C" __declspec(dllimport) int Get
    (int*     pEnrollNumber,                                                          
     char** EnrollNames,                                                          
     int*     EnrollIDs,                                                         
     unsigned char** EnrollTemplates,
     int* EnrollTemplateSizes,                                                       
     int Delay);
    -----------------------------------------------------------------------------------------------------------
    I tried to use an IntPtr instead of both char** and unsigned char**,
    but it throws an AccessViolation Exception.

    If I declare a char** (for char**) and byte** (instead of unsigned char**) I still get the same exception but it seems to me the problem in this case is because of
    parameters i pass to the function(i'm not quite sure how to declare it..)



    C# declarations i tried:
    Code:
     [DllImport("TLib.dll")]
            private static extern int EnrollGet
    ( ref int pEnrollNumber, char** EnrollNames,
      ref int EnrollIDs, 
      byte** EnrollTemplates,
      ref int EnrollTemplateSizes,
      int Delay);
     
    or
    
     [DllImport("TLib.dll")]
            private static extern int EnrollGet
    ( ref int pEnrollNumber, 
     out IntPtr EnrollNames,
     ref int EnrollIDs, 
     out IntPtr EnrollTemplates,
     ref int EnrollTemplateSizes,
    int Delay);
    Can anyone please help me to solve this issue?

    Thanks in advance
    Last edited by PRR; Apr 21 '09, 08:57 AM. Reason: Please post code in [code] [/code] tags.
  • mrssuper
    New Member
    • Apr 2009
    • 2

    #2
    I finally solved the problem.

    I left the declaration as it is in C++:

    [DllImport("TLib .dll")]
    public static extern int Get( int* pEnrollNumber,c har** EnrollNames,int * EnrollIDs,
    byte** EnrollTemplates ,int* EnrollTemplateS izes, int Delay);

    Turns out that all integers point also to arrays so I created the following class:


    unsafe class CUser
    {
    public int m_Number;
    public int m_Index;
    public char*[] m_Names;
    public int[] m_IDs;
    public int[] m_TemplateSizes ;
    public byte*[] m_Templates;

    public CUser()
    {

    // 1000 = db size

    m_Number = 0;
    m_Index = 0;
    m_Names = new char*[1000];
    m_IDs = new int[1000];
    m_TemplateSizes = new int[1000];
    m_Templates = new byte*[1000];

    for (int i = 0; i < 1000; ++i)
    {
    m_Names[i] = (char*)Memory.A lloc(64);
    m_Templates[i] = (byte*)Memory.A lloc(1000);
    }
    }

    ~CUser()
    {


    for (int i = 0; i < MAX_USER_ENTRIE S; ++i)
    {
    Memory.Free(m_T emplates[i]);
    }
    }
    }

    The trick is to allocate the memory for both the array of strings and bytes and pass a correct parameters which I didn't before.
    I did it using the microsoft sample of dinamic Memory allocation.
    The Class found in: http://msdn.microsoft.com/en-us/libr...86(VS.71).aspx


    The function call:




    unsafe
    {

    CUser users = new CUser();




    fixed (int* num = &users.m_Number , ids = &users.m_IDs[0], sizes = &users.m_Templa teSizes[0])
    {
    fixed (byte** temps = &(users.m_Templ ates[0]))
    {
    fixed (char** names = &(users.m_Na mes[0]))
    {
    int result = Helper.Get(num, names, ids, temps, sizes);
    }
    }
    }
    }

    Maybe it a little bit difficult, but it works..

    Comment

    Working...