none
Call c++ dll function from c# .net RRS feed

  • Question

  • I have dll that was written by c++, and I tried to call its function from my c#.net code. In one function I had no success.

    This is the c++ function:

    TYPE_IssClapi_Status ISSCLAPI_PUBLIC
       IssClapi_OpenScope (      TYPE_IssClapi_ServerHandle       p_hServerHandle,
                                 TYPE_IssClapi_CpuID *            p_pstCPUID,
                                 TYPE_IssClapi_ScopeHandle *      p_phScopeHandle);

    Its call:

    a_eClapiStatus = IssClapi_OpenScope( c_hServerHandle,
                                              &a_astCpuIDs[i],
                                              &a_hScopeHandle );

    while the second parameter is an array of structures:

    It is declared in c++:

    TYPE_IssClapi_CpuID a_astCpuIDs[400];

    and the structure in c++ declared:

    typedef struct
       {
          BYTE CpuID[8];
       }
       TYPE_IssClapi_CpuID;

    How can I convert it to c#?

    This is my try:

     [DllImport("issclapi.dll")]

    public static extern TYPE_IssClapi_Status IssClapi_OpenScope(TYPE_IssClapi_ServerHandle p_hServerHandle, out TYPE_IssClapi_CpuID p_stCPUID, out TYPE_IssClapi_ScopeHandle p_hScopeHandle); 

    Its call:

    a_eClapiStatus = IssClapi.IssClapi_OpenScope(g_hServerHandle, out  a_astCpuIDList[a_lScopeIndex], out a_hScopeHandle);

    The array declaration:

    TYPE_IssClapi_CpuID[] a_astCpuIDList = new TYPE_IssClapi_CpuID[MAX_CPUID_LIST];

    The structure declaration:

    [StructLayout(LayoutKind.Sequential)]
    public struct TYPE_IssClapi_CpuID
    {
           [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7+1)]
            public byte []   CpuID;   
    }   

    What's wrong?

     


    Tuesday, July 26, 2011 6:12 AM

Answers

  • using out is definitely incorrect since IssClapi_OpenScope does not allocate the returning memory block. TYPE_IssClapi_CpuID looks fine, but the calling convention may have problem, the default C++ compiler setting is cdecl (though it is uncertain what is actually compiled into issclapi.dll, you can check the calling convention by writing a C++ client and see which calling convention does not crash), not the stdcall assumed by DllImport.

    since everything is fixed-lengthed the pinvoke assistant suggested by Riqual would be great help here.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Paul Zhou Thursday, August 4, 2011 3:37 AM
    Tuesday, July 26, 2011 9:34 PM
  • Try to change the byte[] parameter to an IntPtr type.

    You can use Marshal.Copy method to copy the byte array to an IntPtr, and marshal to unmanaged code.

    Hope this can help you resolve the problem.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, August 4, 2011 3:37 AM
    Thursday, July 28, 2011 4:44 AM

All replies

  • Do you use this tool to make your life easy? If you don't, try this tool to get managed signature for unmanaged function:

    http://clrinterop.codeplex.com/releases/view/14120

    Try it, then let us know the result. Thanks!

     

     


    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, July 26, 2011 3:23 PM
  • using out is definitely incorrect since IssClapi_OpenScope does not allocate the returning memory block. TYPE_IssClapi_CpuID looks fine, but the calling convention may have problem, the default C++ compiler setting is cdecl (though it is uncertain what is actually compiled into issclapi.dll, you can check the calling convention by writing a C++ client and see which calling convention does not crash), not the stdcall assumed by DllImport.

    since everything is fixed-lengthed the pinvoke assistant suggested by Riqual would be great help here.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Paul Zhou Thursday, August 4, 2011 3:37 AM
    Tuesday, July 26, 2011 9:34 PM
  • Try to change the byte[] parameter to an IntPtr type.

    You can use Marshal.Copy method to copy the byte array to an IntPtr, and marshal to unmanaged code.

    Hope this can help you resolve the problem.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, August 4, 2011 3:37 AM
    Thursday, July 28, 2011 4:44 AM