如题

解决方案 »

  1.   

    如果是非托管的,就用DllImport,举例 
      using System; 
      using System.Runtime.InteropServices; 
      class MainApp 
          [DllImport("Kernel32")]                    //读取动态库文件 
            public static extern int GetProcAddress(int handle, String funcname);   给你讲一下我的经验: 
      首先 你在C#中调用的 是C++ 写的一个动态库。比如Kernel32.dll 中的 函数; 
      这个函数用C++写 有如下要求: 
    1、 必须为全局函数 
    2、 函数参数 必须为基本类型,也就是C++ 和C#都有的类型,否则你在public static extern int GetProcAddress(int handle, String funcname); 
        这里没有办法声明。 其余的 没什么了; 还有你可以参考这里:
     C#中调用Windows API的要点
    http://blog.csdn.net/jingshuaizh/archive/2009/02/04/3862019.aspx先前用VC写的程序生成的DLL,C#里能够调用么? 
    可以的话,在VC里传入参数为CString,而C#里是string,怎么传参数呢? 再者就是问,用VC写的DLL如果是一个导出类,而不是函数接口,C#里可以直接调用么? 
    第一个问题我遇到过, 可用如下方法解决:   VC++ 中为: int Set(char** str,int n);          //将 String 改成 char** , C# 中没有与String对应的类型 
      C#  中为: int Set(ref string str,int n); 
      
      VC++ 中的 BOOL 类型对应 C# 中的 System.Int32 类型, 建议改动一下参数类型吧.第二个问题,生成的DLL里的函数在一个类里面,这样的话在C#里需要实例化那个类么?怎么做 
    比如说,类solution里有函数int getch(int a); 
    我怎么调用这个getch函数?? 建议在C++中另外写个函数封装一下, 如: 
      int Dllgetch(int a) 

        solution st = //实例化 类solution 
        return st.getch(a); 

    这个 Dllgetch(int a) 就可以提取出来供 C# 调用.
    你的方法可以,我也解决了 
    将CString 改成了LPCTSTR 
    BOOL在C#中对应的是Boolean
      

  2.   

    借宝地问个问题typedef enum { RTU, TCP } type_com_t;struct win32_ser
    {
            HANDLE fd;
            uint8_t buf[PY_BUF_SIZE];
            DWORD n_bytes;
    };typedef struct
    {
            struct win32_ser w_ser;
            int fd;
            type_com_t type_com;
            int debug;
    }void modbus_init_rtu(modbus_param_t* mb_param, const char* device)
    这种结构嵌套的形式如何调用?我是这样写的        public enum type_com_t { RTU, TCP };
    [StructLayout(LayoutKind.Sequential)]
    public struct win32_ser
    {
    public IntPtr fd; // HANDLE fd;
    [MarshalAs(UnmanagedType.U8)]
    public byte[] buf; // uint8_t buf[PY_BUF_SIZE];
    public UInt32 n_bytes; // DWORD n_bytes;
    }; [StructLayout(LayoutKind.Sequential)]
    public struct modbus_param_t
    {
    public win32_ser w_ser; // struct win32_ser w_ser;
    public int fd; // int fd;
    public type_com_t type_com; // type_com_t type_com;
                    public int debug; // int debug;
            }[DllImport("LibModbus.dll")]
    public static extern void modbus_init_rtu(ref modbus_param_t mb_param, ref string device);
    提示如下错误:
    未处理的“System.TypeLoadException”类型的异常出现在 Modbus.exe 中。其他信息: 无法封送处理类型为“Modbus.modbus_param_t”的字段“w_ser”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的。请问这种嵌套结构如何调用?
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Runtime.InteropServices;
    namespace testloadlibrary
    {
        class Program
        {
            [DllImport("Kernel32")]
            public static extern System.IntPtr LoadLibrary( string dllfile);
            [DllImport("Kernel32")]
            public static extern System.IntPtr GetProcAddress( System.IntPtr hModule,   string  lpProcName); 
            public delegate int msgbox(System.IntPtr hwnd,string lptext ,string title, uint type);
            static void Main(string[] args)
            {
                System.IntPtr hinst = LoadLibrary("user32.dll");
                System.IntPtr proc = GetProcAddress(hinst, "MessageBoxW");
                msgbox  md =  Marshal.GetDelegateForFunctionPointer(proc,typeof(msgbox)) as msgbox;
                md(System.IntPtr.Zero, "123", "123", 0);
                
            }
        }
    }
      

  4.   

    你还可以用 delegate.DynamicInvoke 调用 不用想我那样写
    public delegate int msgbox(System.IntPtr hwnd,string lptext ,string title, uint type); 
      

  5.   

    主要是C++ 不是我写的[code=C#]
    System.IntPtr hinst = LoadLibrary("user32.dll"); 
                System.IntPtr proc = GetProcAddress(hinst, "MessageBoxW"); 
                msgbox  md =  Marshal.GetDelegateForFunctionPointer(proc,typeof(msgbox)) as msgbox; 
                md(System.IntPtr.Zero, "123", "123", 0); code]
    这上面能解释一下吗
      

  6.   

    System.IntPtr hinst = LoadLibrary("user32.dll");//通过string 获取dll 的 hinstance 
    System.IntPtr proc = GetProcAddress(hinst, "MessageBoxW"); //通过string 获取那个dll 的messageboxW api的 函数指针
    msgbox md = Marshal.GetDelegateForFunctionPointer(proc,typeof(msgbox)) as msgbox;  //从函数指针获取一个委托
    ...
    让后通过委托调用