c#.net里面如何调用c++.net编译的dll里的静态方法?

kingofmatch 2009-03-01 12:14:47
C++用 类库 做了一个项目,编译为了a.dll
namespace Package {

public ref class Lz77
{
public:
int static encode(BYTE *dst, const BYTE *src, int filesize) {......}
int static add(int a, int b) {......}
};
}

然后我在c#里面添加引用了这个a.dll,然后我在C#的代码里调用:

int ret = Package.Lz77.add(2, 3); //这行可以正常运行,返回a和b相加的结果给ret

但是另外一个encode静态方法,有两个指针类型,不知道C#里面怎么给方法传递指针?另外,a.dll是不能修改的,C#里面应该怎么处理这种情况。如果反过来,C++调用C#的dll,就完全不存在这种问题,C#都没指针类型。
...全文
210 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
li45214521 2009-03-05
  • 打赏
  • 举报
回复
int* fib = stackalloc int[100];
wo789 2009-03-03
  • 打赏
  • 举报
回复
static unsafe void Main(string[] args)
{
byte* dst = stackalloc byte[1000];
byte* src = null;
Package.Lz77.encode(dst, src, 1000);
}
kingofmatch 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cppfaq 的回复:]
/// Return Type: int
///dst: BYTE*
///src: BYTE*
///filesize: int
[System.Runtime.InteropServices.DllImportAttribute(" <Unknown>", EntryPoint="encode")]
public static extern  int encode(ref byte dst, ref byte src, int filesize) ;

[/Quote]

我用了这个后,直接就是
无法在 DLL“Compress.dll”中找到名为“encode”的入口点。

Compress.dll是一个.net托管的DLL,肯定不能这样子做。
kingofmatch 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cppfaq 的回复:]
/// Return Type: int
///dst: BYTE*
///src: BYTE*
///filesize: int
[System.Runtime.InteropServices.DllImportAttribute(" <Unknown>", EntryPoint="encode")]
public static extern  int encode(ref byte dst, ref byte src, int filesize) ;

[/Quote]

这个是调用非托管DLL的做法吧,比如c#.net调用 vc6.0 编译的dll
derek02 2009-03-01
  • 打赏
  • 举报
回复
mark...
goonfighting 2009-03-01
  • 打赏
  • 举报
回复
DllImport
cppfaq 2009-03-01
  • 打赏
  • 举报
回复
/// Return Type: int
///dst: BYTE*
///src: BYTE*
///filesize: int
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="encode")]
public static extern int encode(ref byte dst, ref byte src, int filesize) ;
kingofmatch 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 li45214521 的回复:]
int static encode(BYTE *dst, const BYTE *src, int filesize) {......}
C# 调用:
unsafe{
byte * dst= statalloc(1000);
byte * src= null;
Package.Lz77.encode(dst,src,1000);

}
[/Quote]

错误 1 当前上下文中不存在名称“statalloc”
wo789 2009-03-01
  • 打赏
  • 举报
回复
        //方法用"unsafe"修饰(选择项目属性-->生成-->勾选允许不安全代码)
static unsafe void Main(string[] args)
{
string str_dst = "C:\\test1.jpg\0";//"\0"标识字符串结束
IntPtr pt_dst = System.Runtime.InteropServices.Marshal.AllocHGlobal(str_dst.Length*2);
System.Runtime.InteropServices.Marshal.Copy(str_dst.ToCharArray(), 0, pt_dst, str_dst.Length);
byte* dst = (byte*)pt_dst;

string str_src = "D:\\test2.jpg\0";
IntPtr pt_src = System.Runtime.InteropServices.Marshal.AllocHGlobal(str_src.Length*2);
System.Runtime.InteropServices.Marshal.Copy(str_src.ToCharArray(), 0, pt_src, str_src.Length);
byte* src = (byte*)pt_src;

Package.Lz77.encode(dst,src, 0);
}
天乐 2009-03-01
  • 打赏
  • 举报
回复
c#的指针仅支持值类型
li45214521 2009-03-01
  • 打赏
  • 举报
回复
int static encode(BYTE *dst, const BYTE *src, int filesize) {......}
C# 调用:
unsafe{
byte * dst= statalloc(1000);
byte * src= null;
Package.Lz77.encode(dst,src,1000);

}
龙宜坡 2009-03-01
  • 打赏
  • 举报
回复
不知道C#里面怎么给方法传递指针?


委托!

C++的指针在C#可以用IntPtr代替,

另外C#支持指针!
天乐 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 gui0605 的回复:]
DllImport调用的是非托管的DLL,对于托管的DLL,这样用是不行的。不知道直接引用是否可行,由于没装C++.NET,无法测试
[/Quote]

是这样的,我的示例仅仅说明Intptr是C#中对应指针的类型。

托管C++与C#的交互我也没搞过,不清楚。

似乎可以直接引用后使用,毕竟都是基于CTS、CLR的。
悔说话的哑巴 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 agentianle 的回复:]
例如:
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

之后方法中就可以:
IntPtr progmanHandle = FindWindow("Progman", "Program Manager");


需要:
using System.Windows.Interop;
[/Quote]
gui0605 2009-03-01
  • 打赏
  • 举报
回复
DllImport调用的是非托管的DLL,对于托管的DLL,这样用是不行的。不知道直接引用是否可行,由于没装C++.NET,无法测试
天乐 2009-03-01
  • 打赏
  • 举报
回复
详细的C#类型与c/c++类型关系参见:

http://www.pinvoke.net/index.aspx
zcandyly20211 2009-03-01
  • 打赏
  • 举报
回复
友情up
天乐 2009-03-01
  • 打赏
  • 举报
回复
例如:
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

之后方法中就可以:
IntPtr progmanHandle = FindWindow("Progman", "Program Manager");


需要:
using System.Windows.Interop;
kingofmatch 2009-03-01
  • 打赏
  • 举报
回复
kingofmatch 2009-03-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 agentianle 的回复:]
c#中指针对应Intptr类型
[/Quote]

能告诉个例子吗?
加载更多回复(2)

111,126

社区成员

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

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

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