c#调用c++的DLL,接口函数参数有函数指针,怎么解决?

huangweibuct 2009-09-24 11:17:18
要用c#调用c++的DLL,c++的DLL接口函数其中一个参数问函数指针,怎么解决?
接口函数为:


void Connecter(int ServerPort,void(* pF)(char* buf,char* ClientIP,int ClientPort));


再c#中这样调用接口函数不行:

public class RefComm
{
[DllImport("MyServerDLL.dll",
EntryPoint = "Connecter",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void Connecter(int ServerPort,void(* pF)(char* buf,char* ClientIP,int ClientPort));
}

怎么解决这个问题呢?
...全文
1305 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangweibuct 2009-09-25
  • 打赏
  • 举报
回复
我查了一下,c#中的objec是不可复制的,不知道这么处理下
huangweibuct 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wartim 的回复:]
给指针分配一下内存

C# codepublicdelegatevoid FunctionDelegate(refstring buf,refstring ClientIP,int ClientPort);

IntPtr P=Marshal.AllocHGlobal(1024*1024*10);
Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, P,true);
Connecter(1000, P);//。。。// 最后释放内存 Marshal.FreeHGlobal(P);
[/Quote]
指定结构必须能直接复制到本机结构中,或是具有布局信息。
参数名: structure
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentException: 指定结构必须能直接复制到本机结构中,或是具有布局信息。
参数名: structure

源错误:


行 161: IntPtr intPtr = Marshal.AllocHGlobal(1024*1024*10);
行 162:
行 163: Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, intPtr, true);
行 164: //Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, intPtr, true);
行 165: //FunctionDelegate myF = new FunctionDelegate(Callback);


源文件: f:\MyWeb\WebTest\Default.aspx.cs 行: 163


昨天我也这样改过,就一直是这个问题
wartim 2009-09-25
  • 打赏
  • 举报
回复
给指针分配一下内存


public delegate void FunctionDelegate(ref string buf, ref string ClientIP, int ClientPort);

IntPtr P=Marshal.AllocHGlobal(1024 * 1024 * 10);
Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, P, true);
Connecter(1000, P);
//。。。
// 最后释放内存
Marshal.FreeHGlobal(P);

xray2005 2009-09-24
  • 打赏
  • 举报
回复
这样调C++还真没调过.


帮顶!
huangweibuct 2009-09-24
  • 打赏
  • 举报
回复
public class RefComm
{
[DllImport("MyServerDLL.dll",
EntryPoint = "Connecter",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void Connecter(int ServerPort,void(* pF)(char* buf,char* ClientIP,int ClientPort));
}


这里就不符合c#语法
卧_槽 2009-09-24
  • 打赏
  • 举报
回复
用委托试试
huangweibuct 2009-09-24
  • 打赏
  • 举报
回复
自己顶
huangweibuct 2009-09-24
  • 打赏
  • 举报
回复
运行时发生异常:值不能为空。
参数名: ptr
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentNullException: 值不能为空。
参数名: ptr

源错误:


行 154: {
行 155: IntPtr P = new IntPtr();
行 156: Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, P, true);
行 157: Connecter(1000, P);
行 158: }

源文件: f:\MyWeb\WebTest\Default.aspx.cs 行: 156


huangweibuct 2009-09-24
  • 打赏
  • 举报
回复

f(char* buf,char* ClientIP,int ClientPort);

这一句怎么转换?
huangweibuct 2009-09-24
  • 打赏
  • 举报
回复
这样可以了,但是c#中char*用上吗类型?
wartim 2009-09-24
  • 打赏
  • 举报
回复
试试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime .InteropServices;

namespace WindowsApplication67
{
public partial class Form1 : Form
{
[DllImport("MyServerDLL.dll")]
public static extern void Connecter(int ServerPort, IntPtr P);

public delegate void FunctionDelegate(string buf, string ClientIP, int ClientPort);

public Form1()
{
IntPtr P = new IntPtr();
Marshal.StructureToPtr(new FunctionDelegate(Callback).Target, P, true);
Connecter(1000, P);
}

void Callback(string buf, string ClientIP, int ClientPort)
{
// ...
}
}
}
松花皮蛋 2009-09-24
  • 打赏
  • 举报
回复
void(* pF)(char* buf,char* ClientIP,int ClientPort));
}

这两个参数应该是可以用传引用方式REF 解决,(* pF)用委托
huangweibuct 2009-09-24
  • 打赏
  • 举报
回复

public delegate void FunctionDelegate(byte buf, byte ClientIP, int ClientPort);
public static extern void Connecter(int ServerPort, FunctionDelegate fP);

错误提示: 警告 CS0626: 方法、运算符或访问器“RefComm.Connecter(int, RefComm.FunctionDelegate)”被标记为外部对象并且它上面没有任何属性。请考虑添加一个 DllImport 属性以指定外部实现。
错误 CS0592: 属性“DllImport”在该声明类型中无效。它只在“method”声明中有效。


我觉得是指针的问题,在C#里怎么转换?
能具体的写下代码吗?
kugou123 2009-09-24
  • 打赏
  • 举报
回复
函数指针在C#里面是可以用委托来转换的。
wartim 2009-09-24
  • 打赏
  • 举报
回复
[DllImport("MyServerDLL.dll")]
public static extern void Connecter(int ServerPort, FunctionDelegate fP);

public delegate void FunctionDelegate(string buf, string ClientIP, int ClientPort);


不过我怀疑不能直接用c#的委托,毕竟c#的委托和c++的函数指针不是一回事,更像一个函数指针列表
获取你可以先把指针的地址取过来再说
[DllImport("MyServerDLL.dll")]
public static extern void Connecter(int ServerPort, IntPtr P);

huangweibuct 2009-09-24
  • 打赏
  • 举报
回复
delegate void FunctionDelegate(string buf,string ClientIP,int ClientPort);
public static extern void Connecter(int ServerPort,FunctionDelegate fP);

提示:错误 CS0051: 可访问性不一致: 参数类型“RefComm.FunctionDelegate”比方法“RefComm.Connecter(int, RefComm.FunctionDelegate)”的可访问性低

我没怎么用过C#,具体该这么写?
hyblusea 2009-09-24
  • 打赏
  • 举报
回复
用 ref

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧