Can not marshal parameter #1: Invalid managed/unmanaged type combination (this value type must be paired with Struct). 是什么意

Eastunfail 2002-08-31 12:44:36
我在程序中用到了unrar.dll(可以到WinRAR的官方网站下载)来对RAR格式的文件解压,结构是如下定义的:
<StructLayout(LayoutKind.Explicit)> Public Structure RARHeaderData
<FieldOffset(0), MarshalAs(UnmanagedType.AnsiBStr)> Public ArcName() As Char
<FieldOffset(260), MarshalAs(UnmanagedType.AnsiBStr)> Public FileName() As Char
<FieldOffset(520)> Public Flags As Integer
<FieldOffset(524)> Public PackSize As Integer
<FieldOffset(528)> Public UnpSize As Integer
<FieldOffset(532)> Public HostOS As Integer
<FieldOffset(536)> Public FileCRC As Integer
<FieldOffset(540)> Public FileTime As Integer
<FieldOffset(544)> Public UnpVer As Integer
<FieldOffset(548)> Public Method As Integer
<FieldOffset(552)> Public FileAttr As Integer
<FieldOffset(556), MarshalAs(UnmanagedType.LPStr)> Public CmtBuf As String
<FieldOffset(560)> Public CmtBufSize As Integer
<FieldOffset(564)> Public CmtSize As Integer
<FieldOffset(568)> Public CmtState As Integer
Sub New(ByVal Reserved As Integer)
ReDim ArcName(260)
ReDim FileName(260)
End Sub
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure RAROpenArchiveData
<MarshalAs(UnmanagedType.LPStr)> Public ArcName As String
Public OpenMode As Integer
Public OpenResult As Integer
<MarshalAs(UnmanagedType.LPStr)> Public CmtBuf As String
Public CmtBufSize As Integer
Public CmtSize As Integer
Public CmtState As Integer
End Structure

因为总是出错,所以修改了很多遍,于是就成了这个样子,API声明:

Public Declare Auto Function RAROpenArchive Lib "unrar.dll" (<MarshalAs(UnmanagedType.LPStruct)> ByRef ArchiveData As RAROpenArchiveData) As Integer
Public Declare Auto Function RARCloseArchive Lib "unrar.dll" (ByVal hArcData As Integer) As Integer
Public Declare Auto Function RARReadHeader Lib "unrar.dll" (ByVal hArcData As Integer, <MarshalAs(UnmanagedType.LPStruct)> ByRef HeaderData As RARHeaderData) As Integer
Public Declare Auto Function RARProcessFile Lib "unrar.dll" (ByVal hArcData As Integer, ByVal Operation As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal DestPath As String, <MarshalAs(UnmanagedType.LPStr)> ByVal DestName As String) As Integer
Public Declare Auto Sub RARSetChangeVolProc Lib "unrar.dll" (ByVal hArcData As Integer, ByVal Mode As Integer)
Public Declare Auto Sub RARSetPassword Lib "unrar.dll" (ByVal hArcData As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal Password As String)


一旦编译执行的时候就会在DLL中跑出异常:

未处理的“System.Runtime.InteropServices.MarshalDirectiveException”类型异常出现在 Setup.exe 中

其它信息:Can not marshal parameter #1: Invalid managed/unmanaged type combination (this value type must be paired with Struct).

怎么解决?
...全文
675 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eastunfail 2002-09-08
  • 打赏
  • 举报
回复
这是个正规DLL,不是一个COM组建啊,我该如何解决呢?
edreamy 2002-09-01
  • 打赏
  • 举报
回复
参数类型错误,函数的参数在使用之前需要确定参数类型的,不能像以前让系统自动识别转换的
Latitude 2002-09-01
  • 打赏
  • 举报
回复
System.Runtime.InteropServices 命名空间提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。此命名空间中的类型分为下列功能领域:属性、异常、COM 类型的托管定义、包装、类型转换器和 Marshal 类(提供与分配非托管内存、复制非托管内存块以及将托管类型转换为非托管类型有关的方法集合)。
Runtime.InteropServices.MarshalDirectiveException 表示封送器遇到不支持的 MarshalAsAttribute 时报的错
MarshalAsAttribute 是指示应如何在托管代码和非托管代码之间封送数据。

不是太有把握,你试一下先:
这样,你先去网上找一个注册工具,将dll注册一下。(比如用jfwinreg.exe)
如果不行,试着用.net中的帮助中的"托管代码"一节,可试着看看“自定义 COM 可调用包装”

第三方控件的使用确实是个问题。你先试试吧

by Henry

16,720

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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