C# 调用 C 、C++ 编写的dll

dylnuaa 2008-07-22 10:17:51
用c++编写了一个dll,现在要供c#调用,

在c++中调用的格式为:

typedef void (*CALLBACKF) (char **, int, double, char **);

typedef bool (*FUN)(char *, double , double , double , CALLBACKF, int);

dll=LoadLibrary(dllLocation);
FUN fun1=(FUN)GetProcAddress(dll, "fun");

在c#中如何实现?有没有相关资料?

谢谢~
...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
GhostAdai 2008-07-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dylnuaa 的回复:]
谢谢各位,问题已经解决,并不是参数引用方面的问题,而是主要是callback函数引起:)
[/Quote]

C++就是这些容易出问题。
睡神在睡觉 2008-07-22
  • 打赏
  • 举报
回复
还少一行
public static extern int 函数名(参数, 参数 );
睡神在睡觉 2008-07-22
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;

[DllImport("调用DLL文件名.DLL",
EntryPoint = "函数名",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Winapi)]
gomoku 2008-07-22
  • 打赏
  • 举报
回复
我需要的是如何输入输出 数组,题中的是char数组,包括一维和多维,我是这样写的

delegate void CALLBACK(char[][] text1, int num, double double1, char [][] text2);

public void callfun(char[][] text, int num, double double1, char[][] text2)
{

}


1. c++ char == C# byte
2. char[][] is usually wrong.
You must first make sure char** in c++ is a reference to a string [usually out], or an array of strings [usually in/out]
dylnuaa 2008-07-22
  • 打赏
  • 举报
回复
我需要的是如何输入输出 数组,题中的是char数组,包括一维和多维,我是这样写的

delegate void CALLBACK(char[][] text1, int num, double double1, char [][] text2);

public void callfun(char[][] text, int num, double double1, char[][] text2)
{

}

[DllImport(dllLocation, CallingConvention = CallingConvention.Cdecl, EntryPoint = "fun")]
private static extern bool fun(char[] text, double double1, double double2, double double3, CALLBACK callfun, int deb);

//以下为调用

fun("hello.txt", 0, 0, 0.5, new CALLBACK (callfun), 1);

callback 函数具体对不对还不知道,现在连fun的声明都通不过,提示:
"Cannot marshal 'parameter #1': There is no marshaling support for nested arrays."
把第一个参数换成string类也不行
gomoku 2008-07-22
  • 打赏
  • 举报
回复
1. use C# delegate for C++ callback
2. you don't need to use LoadLibrary(), use DllImport("YourDll") directly
3. char** is a bit tricky, try using ref StringBuilder
aimeast 2008-07-22
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern int function(ref PONITAPI p);
dylnuaa 2008-07-22
  • 打赏
  • 举报
回复
我知道是用dllimport,但具体怎么写不知道
greystar 2008-07-22
  • 打赏
  • 举报
回复
dllimport
dylnuaa 2008-07-22
  • 打赏
  • 举报
回复
谢谢各位,问题已经解决,并不是参数引用方面的问题,而是主要是callback函数引起:)

110,533

社区成员

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

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

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