访问 dll 时尝试读取或写入受保护的内存
当我的代码出现此错误时,我尝试访问 zip32.dll 3.0 版:
System.AccessViolationException: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
at projectName.clsName.ZpArchive(ZCL& zcl, ZPOPT& zopts)
at projectName.clsName.functionName() in <location>:<line>
这就是我对代码所做的操作:
//ZCL Structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
protected struct ZCL
{
public int argc; /* Count of files to zip */
[MarshalAs(UnmanagedType.LPStr)]
public string fzname; /* name of archive to create/update */
public string[] zipnames; /* zip file name */
[MarshalAs(UnmanagedType.LPStr)]
public string fzlist; /* name of archive to create/update */
}
//ZPOPT Structures
[ StructLayout( LayoutKind.Sequential )]
protected struct ZPOPT
{
[MarshalAs(UnmanagedType.LPTStr)]
public string Date; // US Date (8 Bytes Long) "12/31/98"?
[MarshalAs(UnmanagedType.LPTStr)]
public string szRootDir; // Root Directory Pathname (Up To 256 Bytes Long)
[MarshalAs(UnmanagedType.LPTStr)]
public string szTempDir; // Temp Directory Pathname (Up To 256 Bytes Long)
public int fTemp; // 1 If Temp dir Wanted, Else 0
public int fSuffix; // Include Suffixes (Not Yet Implemented!)
public int fEncrypt; // 1 If Encryption Wanted, Else 0
public int fSystem; // 1 To Include System/Hidden Files, Else 0
public int fVolume; // 1 If Storing Volume Label, Else 0
public int fExtra; // 1 If Excluding Extra Attributes, Else 0
public int fNoDirEntries; // 1 If Ignoring Directory Entries, Else 0
public int fExcludeDate; // 1 If Excluding Files Earlier Than Specified Date, Else 0
public int fIncludeDate; // 1 If Including Files Earlier Than Specified Date, Else 0
public int fVerbose; // 1 If Full Messages Wanted, Else 0
public int fQuiet; // 1 If Minimum Messages Wanted, Else 0
public int fCRLF_LF; // 1 If Translate CR/LF To LF, Else 0
public int fLF_CRLF; // 1 If Translate LF To CR/LF, Else 0
public int fJunkDir; // 1 If Junking Directory Names, Else 0
public int fGrow; // 1 If Allow Appending To Zip File, Else 0
public int fForce; // 1 If Making Entries Using DOS File Names, Else 0
public int fMove; // 1 If Deleting Files Added Or Updated, Else 0
public int fDeleteEntries; // 1 If Files Passed Have To Be Deleted, Else 0
public int fUpdate; // 1 If Updating Zip File-Overwrite Only If Newer, Else 0
public int fFreshen; // 1 If Freshing Zip File-Overwrite Only, Else 0
public int fJunkSFX; // 1 If Junking SFX Prefix, Else 0
public int fLatestTime; // 1 If Setting Zip File Time To Time Of Latest File In Archive, Else 0
public int fComment; // 1 If Putting Comment In Zip File, Else 0
public int fOffsets; // 1 If Updating Archive Offsets For SFX Files, Else 0
public int fPrivilege; // 1 If Not Saving Privileges, Else 0
public int fEncryption; // Read Only Property!!!
public int fRecurse; // 1 (-r), 2 (-R) If Recursing Into Sub-Directories, Else 0
public int fRepair; // 1 = Fix Archive, 2 = Try Harder To Fix, Else 0
public byte flevel; // Compression Level - 0 = Stored 6 = Default 9 = Max
[MarshalAs(UnmanagedType.LPTStr)]
public string fSplitSize; //Null for no spliting size
//addition for ZCL
public int m_CountFile; //For total file on backup file.
}
//Import Dll:
[DllImport("zip32.dll", SetLastError=true)]
protected static extern int ZpArchive(ref ZCL zcl,ref ZPOPT zopts);
这是调用 ZpArchive 的函数:
//set the zip options
m_zopts = CreateZPOPTOptions();
ZCL zcl = new ZCL();
zcl.argc = m_CountFile; //Total file
zcl.fzname = m_ZipFileName;
zcl.zipnames = m_FilesToZip;
zcl.fzlist = null;
//zip the files
try
{
ret = ZpArchive(ref zcl,ref m_zopts);
}
catch(Exception e)
{
//<catch>
}
当我尝试调用 ZpArchive 函数时抛出异常。 我使用的是与 .Net 2.0 兼容的 Visual Studio 2010 关于这个问题有什么想法/建议吗?
预先感谢:) -FDI
更新:
这是库源代码的结构:
typedef struct { /* zip options */
LPSTR Date; /* Date to include after */
LPSTR szRootDir; /* Directory to use as base for zipping */
LPSTR szTempDir; /* Temporary directory used during zipping */
BOOL fTemp; /* Use temporary directory '-b' during zipping */
BOOL fSuffix; /* include suffixes (not implemented) */
BOOL fEncrypt; /* encrypt files */
BOOL fSystem; /* include system and hidden files */
BOOL fVolume; /* Include volume label */
BOOL fExtra; /* Exclude extra attributes */
BOOL fNoDirEntries; /* Do not add directory entries */
BOOL fExcludeDate; /* Exclude files newer than specified date */
BOOL fIncludeDate; /* Include only files newer than specified date */
BOOL fVerbose; /* Mention oddities in zip file structure */
BOOL fQuiet; /* Quiet operation */
BOOL fCRLF_LF; /* Translate CR/LF to LF */
BOOL fLF_CRLF; /* Translate LF to CR/LF */
BOOL fJunkDir; /* Junk directory names */
BOOL fGrow; /* Allow appending to a zip file */
BOOL fForce; /* Make entries using DOS names (k for Katz) */
BOOL fMove; /* Delete files added or updated in zip file */
BOOL fDeleteEntries; /* Delete files from zip file */
BOOL fUpdate; /* Update zip file--overwrite only if newer */
BOOL fFreshen; /* Freshen zip file--overwrite only */
BOOL fJunkSFX; /* Junk SFX prefix */
BOOL fLatestTime; /* Set zip file time to time of latest file in it */
BOOL fComment; /* Put comment in zip file */
BOOL fOffsets; /* Update archive offsets for SFX files */
BOOL fPrivilege; /* Use privileges (WIN32 only) */
BOOL fEncryption; /* TRUE if encryption supported, else FALSE.
this is a read only flag */
LPSTR szSplitSize; /* This string contains the size that you want to
split the archive into. i.e. 100 for 100 bytes,
2K for 2 k bytes, where K is 1024, m for meg
and g for gig. If this string is not NULL it
will automatically be assumed that you wish to
split an archive. */
LPSTR szIncludeList; /* Pointer to include file list string (for VB) */
long IncludeListCount; /* Count of file names in the include list array */
char **IncludeList; /* Pointer to include file list array. Note that the last
entry in the array must be NULL */
LPSTR szExcludeList; /* Pointer to exclude file list (for VB) */
long ExcludeListCount; /* Count of file names in the include list array */
char **ExcludeList; /* Pointer to exclude file list array. Note that the last
entry in the array must be NULL */
int fRecurse; /* Recurse into subdirectories. 1 => -r, 2 => -R */
int fRepair; /* Repair archive. 1 => -F, 2 => -FF */
char fLevel; /* Compression level (0 - 9) */
} ZPOPT, _far *LPZPOPT;
typedef struct {
int argc; /* Count of files to zip */
LPSTR lpszZipFN; /* name of archive to create/update */
char **FNV; /* array of file names to zip up */
LPSTR lpszAltFNL; /* pointer to a string containing a list of file
names to zip up, separated by whitespace. Intended
for use only by VB users, all others should set this
to NULL. */
} ZCL, _far *LPZCL;
和函数声明:
int EXPENTRY ZpArchive(ZCL C, LPZPOPT Opts);
一些论坛说这是因为难以管理的代码。我已经使用 MarshalAs 但仍然无法工作..
I try to access zip32.dll version 3.0 when my code got this error :
System.AccessViolationException: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
at projectName.clsName.ZpArchive(ZCL& zcl, ZPOPT& zopts)
at projectName.clsName.functionName() in <location>:<line>
This is what i done with the code :
//ZCL Structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
protected struct ZCL
{
public int argc; /* Count of files to zip */
[MarshalAs(UnmanagedType.LPStr)]
public string fzname; /* name of archive to create/update */
public string[] zipnames; /* zip file name */
[MarshalAs(UnmanagedType.LPStr)]
public string fzlist; /* name of archive to create/update */
}
//ZPOPT Structures
[ StructLayout( LayoutKind.Sequential )]
protected struct ZPOPT
{
[MarshalAs(UnmanagedType.LPTStr)]
public string Date; // US Date (8 Bytes Long) "12/31/98"?
[MarshalAs(UnmanagedType.LPTStr)]
public string szRootDir; // Root Directory Pathname (Up To 256 Bytes Long)
[MarshalAs(UnmanagedType.LPTStr)]
public string szTempDir; // Temp Directory Pathname (Up To 256 Bytes Long)
public int fTemp; // 1 If Temp dir Wanted, Else 0
public int fSuffix; // Include Suffixes (Not Yet Implemented!)
public int fEncrypt; // 1 If Encryption Wanted, Else 0
public int fSystem; // 1 To Include System/Hidden Files, Else 0
public int fVolume; // 1 If Storing Volume Label, Else 0
public int fExtra; // 1 If Excluding Extra Attributes, Else 0
public int fNoDirEntries; // 1 If Ignoring Directory Entries, Else 0
public int fExcludeDate; // 1 If Excluding Files Earlier Than Specified Date, Else 0
public int fIncludeDate; // 1 If Including Files Earlier Than Specified Date, Else 0
public int fVerbose; // 1 If Full Messages Wanted, Else 0
public int fQuiet; // 1 If Minimum Messages Wanted, Else 0
public int fCRLF_LF; // 1 If Translate CR/LF To LF, Else 0
public int fLF_CRLF; // 1 If Translate LF To CR/LF, Else 0
public int fJunkDir; // 1 If Junking Directory Names, Else 0
public int fGrow; // 1 If Allow Appending To Zip File, Else 0
public int fForce; // 1 If Making Entries Using DOS File Names, Else 0
public int fMove; // 1 If Deleting Files Added Or Updated, Else 0
public int fDeleteEntries; // 1 If Files Passed Have To Be Deleted, Else 0
public int fUpdate; // 1 If Updating Zip File-Overwrite Only If Newer, Else 0
public int fFreshen; // 1 If Freshing Zip File-Overwrite Only, Else 0
public int fJunkSFX; // 1 If Junking SFX Prefix, Else 0
public int fLatestTime; // 1 If Setting Zip File Time To Time Of Latest File In Archive, Else 0
public int fComment; // 1 If Putting Comment In Zip File, Else 0
public int fOffsets; // 1 If Updating Archive Offsets For SFX Files, Else 0
public int fPrivilege; // 1 If Not Saving Privileges, Else 0
public int fEncryption; // Read Only Property!!!
public int fRecurse; // 1 (-r), 2 (-R) If Recursing Into Sub-Directories, Else 0
public int fRepair; // 1 = Fix Archive, 2 = Try Harder To Fix, Else 0
public byte flevel; // Compression Level - 0 = Stored 6 = Default 9 = Max
[MarshalAs(UnmanagedType.LPTStr)]
public string fSplitSize; //Null for no spliting size
//addition for ZCL
public int m_CountFile; //For total file on backup file.
}
//Import Dll:
[DllImport("zip32.dll", SetLastError=true)]
protected static extern int ZpArchive(ref ZCL zcl,ref ZPOPT zopts);
And here's the function that call the ZpArchive :
//set the zip options
m_zopts = CreateZPOPTOptions();
ZCL zcl = new ZCL();
zcl.argc = m_CountFile; //Total file
zcl.fzname = m_ZipFileName;
zcl.zipnames = m_FilesToZip;
zcl.fzlist = null;
//zip the files
try
{
ret = ZpArchive(ref zcl,ref m_zopts);
}
catch(Exception e)
{
//<catch>
}
The exception threw when i try to call the ZpArchive function.
I'm using Visual Studio 2010 with compatibility for .Net 2.0
Any idea / advise regarding to this issue?
Thanks in advance :) -FDI
Update :
Here's the structure from library source code :
typedef struct { /* zip options */
LPSTR Date; /* Date to include after */
LPSTR szRootDir; /* Directory to use as base for zipping */
LPSTR szTempDir; /* Temporary directory used during zipping */
BOOL fTemp; /* Use temporary directory '-b' during zipping */
BOOL fSuffix; /* include suffixes (not implemented) */
BOOL fEncrypt; /* encrypt files */
BOOL fSystem; /* include system and hidden files */
BOOL fVolume; /* Include volume label */
BOOL fExtra; /* Exclude extra attributes */
BOOL fNoDirEntries; /* Do not add directory entries */
BOOL fExcludeDate; /* Exclude files newer than specified date */
BOOL fIncludeDate; /* Include only files newer than specified date */
BOOL fVerbose; /* Mention oddities in zip file structure */
BOOL fQuiet; /* Quiet operation */
BOOL fCRLF_LF; /* Translate CR/LF to LF */
BOOL fLF_CRLF; /* Translate LF to CR/LF */
BOOL fJunkDir; /* Junk directory names */
BOOL fGrow; /* Allow appending to a zip file */
BOOL fForce; /* Make entries using DOS names (k for Katz) */
BOOL fMove; /* Delete files added or updated in zip file */
BOOL fDeleteEntries; /* Delete files from zip file */
BOOL fUpdate; /* Update zip file--overwrite only if newer */
BOOL fFreshen; /* Freshen zip file--overwrite only */
BOOL fJunkSFX; /* Junk SFX prefix */
BOOL fLatestTime; /* Set zip file time to time of latest file in it */
BOOL fComment; /* Put comment in zip file */
BOOL fOffsets; /* Update archive offsets for SFX files */
BOOL fPrivilege; /* Use privileges (WIN32 only) */
BOOL fEncryption; /* TRUE if encryption supported, else FALSE.
this is a read only flag */
LPSTR szSplitSize; /* This string contains the size that you want to
split the archive into. i.e. 100 for 100 bytes,
2K for 2 k bytes, where K is 1024, m for meg
and g for gig. If this string is not NULL it
will automatically be assumed that you wish to
split an archive. */
LPSTR szIncludeList; /* Pointer to include file list string (for VB) */
long IncludeListCount; /* Count of file names in the include list array */
char **IncludeList; /* Pointer to include file list array. Note that the last
entry in the array must be NULL */
LPSTR szExcludeList; /* Pointer to exclude file list (for VB) */
long ExcludeListCount; /* Count of file names in the include list array */
char **ExcludeList; /* Pointer to exclude file list array. Note that the last
entry in the array must be NULL */
int fRecurse; /* Recurse into subdirectories. 1 => -r, 2 => -R */
int fRepair; /* Repair archive. 1 => -F, 2 => -FF */
char fLevel; /* Compression level (0 - 9) */
} ZPOPT, _far *LPZPOPT;
typedef struct {
int argc; /* Count of files to zip */
LPSTR lpszZipFN; /* name of archive to create/update */
char **FNV; /* array of file names to zip up */
LPSTR lpszAltFNL; /* pointer to a string containing a list of file
names to zip up, separated by whitespace. Intended
for use only by VB users, all others should set this
to NULL. */
} ZCL, _far *LPZCL;
and the function declaration :
int EXPENTRY ZpArchive(ZCL C, LPZPOPT Opts);
some forum said it's because of umanageable code. I already use MarshalAs but still not working..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论