C#调用VC的Dll时遇到的IntPtr类型转化成byte[]的问题

orion_04 2004-10-04 08:14:42
我使用VC写的动态链接库的某函数的接口如下:
AUTOCROPPINGDLL_API int aaa(IN BYTE* pBmpData,
IN int nWidth,
IN int nHeight)
我使用C#重新声明的接口:
[DllImport("xxx.dll", EntryPoint="#4")]
public static extern int aaa(
[MarshalAs(UnmanagedType.LPArray)] byte[] pBmpData,
int nWidth,
int nHeight);
但是当我在bitmap的lockbit并想将bmpData.Scan0作为参数传给接口函数的时候却遇到类型转化错误。
BitmapData实例的Scan0是Intptr类型的,说没办法转化成byte[]类型。请问我该如何将bmpData的Scan0作为参数传给接口函数?
...全文
529 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
orion_04 2004-10-07
  • 打赏
  • 举报
回复
谢谢大家。
hivak47 2004-10-06
  • 打赏
  • 举报
回复
把pBmpData定义成IntPtr就可以的.
orion_04 2004-10-06
  • 打赏
  • 举报
回复
请问Wangxt,您能说清楚一些么?
我上面的修改有没有问题?
wangxt 2004-10-06
  • 打赏
  • 举报
回复
我也做过相同的东西,vc里面是字节指针,C#里直接就用字节数组传进去就行了
happyjun2000 2004-10-06
  • 打赏
  • 举报
回复
贴个Api参数转换的参考给你

The following table lists data types used in the Win32 API (listed in Wtypes.h) and C-style functions. Many unmanaged libraries contain functions that pass these data types as parameters and return values. The third column lists the corresponding .NET Framework built-in value type or class that you use in managed code. In some cases, you can substitute a type of the same size for the type listed in the table.

Unmanaged type in Wtypes.h Unmanaged C language type Managed class name Description
HANDLE void* System.IntPtr 32 bits
BYTE unsigned char System.Byte 8 bits
SHORT short System.Int16 16 bits
WORD unsigned short System.UInt16 16 bits
INT int System.Int32 32 bits
UINT unsigned int System.UInt32 32 bits
LONG long System.Int32 32 bits
BOOL long System.Int32 32 bits
DWORD unsigned long System.UInt32 32 bits
ULONG unsigned long System.UInt32 32 bits
CHAR char System.Char Decorate with ANSI.
LPSTR char* System.String or System.StringBuilder Decorate with ANSI.
LPCSTR Const char* System.String or System.StringBuilder Decorate with ANSI.
LPWSTR wchar_t* System.String or System.StringBuilder Decorate with Unicode.
LPCWSTR Const wchar_t* System.String or System.StringBuilder Decorate with Unicode.
FLOAT Float System.Single 32 bits
DOUBLE Double System.Double 64 bits

For corresponding types in Visual Basic .NET, C#, and the Managed Extensions for C++, see the Introduction to the .NET Framework Class Library.

orion_04 2004-10-05
  • 打赏
  • 举报
回复
请问Brunhild
如果使用方法1,是不是要修改VC方面的接口?
我直接修改了C#这边的接口,可以编译,但运行的时候出现错误。
[DllImport("xxx.dll", EntryPoint="#4")]
public static extern int aaa(
IntPtr pBmpData,
int nWidth,
int nHeight);
Brunhild 2004-10-05
  • 打赏
  • 举报
回复
方法一:你可以将pBmpData声明为IntPtr
方法二:你可以用System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement获得byte[]的地址

orion_04 2004-10-05
  • 打赏
  • 举报
回复
upup
usepc 2004-10-04
  • 打赏
  • 举报
回复
可怜,帮顶的人都没有。
orion_04 2004-10-04
  • 打赏
  • 举报
回复
upup

110,538

社区成员

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

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

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