none
C# 调用 dll函数指针问题~请帮帮写成C#形式,谢谢 RRS feed

  • 问题

  • 函数说明

    typedef void (*UF_load_f_p_t)( void );

    extern UFUNEXPORT int UF_load_library(
    char * library_name ,/* <I>
                         Name of shared library to load.
                         */
    char * symbol_name ,/* <I>
                        Name of symbol (function) to find.
                        */
    UF_load_f_p_t *function_ptr  /* <O>
                                Pointer to symbol (function) in library.
                                */
    )

    =================================

    以下为 UF_load_library()片断

    --===========================

    typedef int (*XMPL_f_p_t)( int argc, char *argv[] );

    static int execute_lib(
      char       *lib,      /* I - Library Name.               */
      char       *sym,      /* I - Symbol Name.                */
      int         argc,     /* I - Argument Count.             */
      char       *argv[],   /* I - Arguments.                  */
      XMPL_f_p_t *fnc )     /* IO - Function Pointer for 'sym'.*/
    {
        int error;

       /* Load library if necessary... */
      if( NULL == *fnc )
      {
          UF_load_f_p_t generic_fnc;

        if( !(error = UF_load_library( lib, sym, &generic_fnc )) )
        {
          *fnc = (XMPL_f_p_t) generic_fnc;
        }

    ==============================

    以下为C调用时例子

    void execute_macro ()
    {

     typedef void (*load_ufusr_f_p_t)(char* param, int* retcode, int paramLen);
     load_ufusr_f_p_t load_ufusr_ptr = NULL;
     char* file_path = "C:\\sdd.dll";

     int rc = UF_CALL (UF_load_library (file_path, "?MACRO_playback_from_usertool@@YAXPBD@Z", (UF_load_f_p_t *) & load_ufusr_ptr));
     
     if (load_ufusr_ptr != NULL)
     {
      int retcode;
      load_ufusr_ptr("d:\\123.macro", &retcode, 1);
     }

     return;

    }

    ===================

    本人刚学不久,能否帮忙下C#下的例子?

    2012年3月10日 5:27

答案

  • HI,sxin-h

    首先我要确认您是否是调用C/c++的DLL函数,如果是的话,调用DLL文件和写类似该函数的C#的方法是没有用的,调用DLL的函数类似

    [DllImport("LogTagIO20.dll", EntryPoint = "LogTag_SetSDKMemory", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
            public static extern uint SetSDKMemory(LOGTAG_HANDLE handle, UInt16 nPageId, [MarshalAs(UnmanagedType.LPArray)] [In, Out] LOGTAG_SDKMEM[] pMemInfo); //NOT TESTED
            [DllImport("LogTagIO20.dll", EntryPoint = "LogTag_SetServer", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
            public static extern uint SetServer(Guid guid, UInt16 nPortNum, string lpszServer); //NOT TESTED

    关于C#DLLImport 特性请参见http://msdn.microsoft.com/zh-cn/library/z0w1kczw.aspx,希望对你有帮助。
    2012年3月11日 12:37