求:C#动态调用C++写的Dll 代码实现 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是非托管的,就用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 借宝地问个问题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”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的。请问这种嵌套结构如何调用? 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); } }} 你还可以用 delegate.DynamicInvoke 调用 不用想我那样写public delegate int msgbox(System.IntPtr hwnd,string lptext ,string title, uint type); 主要是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]这上面能解释一下吗 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; //从函数指针获取一个委托...让后通过委托调用 升级程序 求winform 中通用的数据效验函数 输入一个自定义时间,让程序去自动执行,用什么办法最好呢? 将excel表格导入datagridview中的一个小问题 选择下拉框,显示不同的内容。 我装的visualstudio2003有问题 请帮帮忙高手近来看看啊 高手请进,做个像tabControl效果的东西 如何用鼠标右键选取listbox的某一项 control类子类怎么实现可拖动边框改变尺寸? 怎么保存DataGridView(显示的表是用代码写出来的)里的内容(经过修改,删除)到数据库中??? new的空间问题 WebBrowser控件如何解决“无法读取爱保护内存”的错误?
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
{
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”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的。请问这种嵌套结构如何调用?
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);
}
}
}
public delegate int msgbox(System.IntPtr hwnd,string lptext ,string title, uint type);
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]
这上面能解释一下吗
System.IntPtr proc = GetProcAddress(hinst, "MessageBoxW"); //通过string 获取那个dll 的messageboxW api的 函数指针
msgbox md = Marshal.GetDelegateForFunctionPointer(proc,typeof(msgbox)) as msgbox; //从函数指针获取一个委托
...
让后通过委托调用