none
p/invoke的异常:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 RRS feed

  • 问题

  • ----------------------------------c#-------------------------------------------------------------------------

    class Program
        {
            static void Main(string[] args)
            {
                GeneralizedTime gt = new GeneralizedTime();
               
                SToken stoken = new SToken();
                PSecurityAttribute psa = new PSecurityAttribute();
                PLogConfig pl = new PLogConfig();

                SFF_CreateSecurityFile(stoken, @"D:\My Documents\Desktop\新建文本文档.txt", @"D:\My Documents\Desktop\新建文本文档.sff", null, psa, pl, "12345678", "SFFTestor", gt);
            }

            [DllImport("SFF.dll")]
            public static unsafe extern UInt32 SFF_CreateSecurityFile(SToken sid, string sourceFilePath, string dstsffFilePath, string dstsflFilePath, PSecurityAttribute psa, PLogConfig plc, string fileID, string szCreator, GeneralizedTime expiredDate);
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SToken
        {
            public Certificate exCertificate;
            public Certificate sigCertificate;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct Certificate
        {
            public UInt32 uCertLen;
            public object certInfo;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct GeneralizedTime
        {
            public UInt32 dwLowDateTime;
            public UInt32 dwHighDateTime;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct SecurityAttribute
        {
            public EncryptAttribute encryptAttribute;
            public SignAttribute signAttribute;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct SignAttribute
        {
            public CipherMethod cipherMethod;
            public Certificate Signer;
            public UInt32 nSignMessageLen;
            public object signMessage;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct EncryptAttribute
        {
            public bool bEncrypt;
            public CipherMethod cipherMethod;
            public ReceiverList receiverList;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct CipherMethod
        {
            public UInt32 uSymAlg;
            public UInt32 uAsymAlg;
            public UInt32 uHashAlg;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct ReceiverList
        {
            public UInt32 nReceiverCount;
            public Receiver receiver;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct Receiver
        {
            public SFACE ace;
            public UInt32 nSessionKeyLen;
            public object sessionKey;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct SFACE
        {
            public Certificate user;
            public bool bRead;
            public UInt32 nTotalReadCount;
            public UInt32 nRemainReadCount;
            public bool bModify;
            public bool bDelete;
            public bool bPrint;
            public UInt32 nTotalPrintCount;
            public UInt32 nRemainPrintCount;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct PLogConfig
        {
            public bool bReadLog;
            public bool bWriteLog;
            public bool bSignLog;
            public bool bEncrypt;
            public bool bDecrypt;
            public bool bSeal;
            public bool bAddWaterMark;
            public bool bAddFingerPrint;
            public bool bPrint;
            public bool bSend;
            public bool bReceive;
            public bool bCut;
            public bool bSign;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct PSecurityAttribute
        {
            public EncryptAttribute encryptAttribute;
            public SignAttribute signAttribute;
        }

    -------------[c/c++]----------------------------------------------------------------------------------

    extern "C" SFF_API UINT32 SFF_CreateSecurityFile(
     IN PSToken SID,
     IN char *szSourceFilePath,
     IN char *szDstSffFilePath,
     IN char *szDstSflFilePath,
     IN PSecurityAttribute psa,
     IN PLogConfig plc,
     IN char *fileID,
     IN char *szCreator,
     IN GeneralizedTime *expiredDate);

    typedef struct
    {
     UINT32 dwLowDateTime;
     UINT32 dwHighDateTime;
    } GeneralizedTime;

    typedef struct
    {
     UINT32 uCertLen;
     void *certInfo;
    } Certificate;

    typedef struct
    {
     BOOL bReadLog;
     BOOL bWriteLog;
     BOOL bSignLog;
     BOOL bEncrypt;
     BOOL bDecrypt;
     BOOL bSeal;
     BOOL bAddWaterMark;
     BOOL bAddFingerPrint;
     BOOL bPrint;
     BOOL bSend;
     BOOL bReceive;
     BOOL bCut;
     BOOL bSign;
    } LogConfig, *PLogConfig;

    typedef struct
    {
     Certificate  user;
     BOOL   bRead;
     UINT32   nTotalReadCount;
     UINT32   nRemainReadCount;
     BOOL   bModify;
     BOOL   bDelete;
     BOOL   bPrint;
     UINT32   nTotalPrintCount;
     UINT32   nRemainPrintCount;
    } SFACE;

    typedef struct
    {
     SFACE   ace;
     UINT32   nSessionKeyLen;
     void   *sessionKey;
    } Receiver, *PReceiver;

    typedef struct
    {
     UINT32   uSymAlg;
     UINT32   uAsymAlg;
     UINT32   uHashAlg;
    } CipherMethod, *PCipherMethod;

    typedef struct
    {
     UINT32   nReceiverCount;
     Receiver**  receiver;
    } ReceiverList, *PReceiverList;

    typedef struct tagEncyrptAttribute
    {
     BOOL   bEncrypt;
     CipherMethod cipherMethod;
     ReceiverList receiverList;
    } EncryptAttribute, *PEncyrptAttribute;

    typedef struct tagSignAttribute
    {
     CipherMethod cipherMethod;
     Certificate  Signer;
     UINT32   nSignMessageLen;
     void*   signMessage;
    } SignAttribute, *PSignAttribute;

    typedef struct
    {
     EncryptAttribute encryptAttribute;
     SignAttribute  signAttribute;
    } SecurityAttribute, *PSecurityAttribute;

    typedef struct
    {
     Certificate exCertificate;
     Certificate sigCertificate;
    } SToken, *PSToken;

     


    2011年6月14日 6:47

答案

  • 结构的声明问题很多,居然有object这种类型不定的东西?传指针的话用IntPtr。不过指向非托管上的地址的指针不能直接当ref struct,要自己手动把数据复制到托管堆。像这种复杂的列集,比较好的方法是用C++/CLI把函数接口重新封装下。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • 已标记为答案 bary,sacc 2011年6月15日 5:56
    2011年6月15日 3:55
    版主

全部回复

  • 老提示这个错误:尝试读取或写入受保护的内存。这通常指示其他内存已损坏 该函数定义的定义如下: extern "C" SFF_API UINT32 SFF_CreateSecurityFile( IN PSToken SID, IN char *szSourceFilePath, IN char *szDstSffFilePath, IN char *szDstSflFilePath, IN PSecurityAttribute psa, IN PLogConfig plc, IN char *fileID, IN char *szCreator, IN GeneralizedTime *expiredDate); (里面的IN是什么意思?*呢?引用?) 我在c#里是这样定义的: [DllImport("SFF.dll")] public static unsafe extern UInt32 SFF_CreateSecurityFile(SToken sid, string sourceFilePath, string dstsffFilePath, string dstsflFilePath, PSecurityAttribute psa, PLogConfig plc, string fileID, string szCreator, GeneralizedTime expiredDate); c#中也同样定义了c++中的结构体 详细如下: -----------c#----- [StructLayout(LayoutKind.Sequential)] public struct SToken { public Certificate exCertificate; public Certificate sigCertificate; } [StructLayout(LayoutKind.Sequential)] public struct Certificate { public UInt32 uCertLen; public object certInfo; } [StructLayout(LayoutKind.Sequential)] public struct GeneralizedTime { public UInt32 dwLowDateTime; public UInt32 dwHighDateTime; } [StructLayout(LayoutKind.Sequential)] public struct SecurityAttribute { public EncryptAttribute encryptAttribute; public SignAttribute signAttribute; } [StructLayout(LayoutKind.Sequential)] public struct SignAttribute { public CipherMethod cipherMethod; public Certificate Signer; public UInt32 nSignMessageLen; public object signMessage; } [StructLayout(LayoutKind.Sequential)] public struct EncryptAttribute { public bool bEncrypt; public CipherMethod cipherMethod; public ReceiverList receiverList; } [StructLayout(LayoutKind.Sequential)] public struct CipherMethod { public UInt32 uSymAlg; public UInt32 uAsymAlg; public UInt32 uHashAlg; } [StructLayout(LayoutKind.Sequential)] public struct ReceiverList { public UInt32 nReceiverCount; public Receiver receiver; } [StructLayout(LayoutKind.Sequential)] public struct Receiver { public SFACE ace; public UInt32 nSessionKeyLen; public object sessionKey; } [StructLayout(LayoutKind.Sequential)] public struct SFACE { public Certificate user; public bool bRead; public UInt32 nTotalReadCount; public UInt32 nRemainReadCount; public bool bModify; public bool bDelete; public bool bPrint; public UInt32 nTotalPrintCount; public UInt32 nRemainPrintCount; } [StructLayout(LayoutKind.Sequential)] public struct PLogConfig { public bool bReadLog; public bool bWriteLog; public bool bSignLog; public bool bEncrypt; public bool bDecrypt; public bool bSeal; public bool bAddWaterMark; public bool bAddFingerPrint; public bool bPrint; public bool bSend; public bool bReceive; public bool bCut; public bool bSign; } [StructLayout(LayoutKind.Sequential)] public struct PSecurityAttribute { public EncryptAttribute encryptAttribute; public SignAttribute signAttribute; } -------------[c/c++]--------------- typedef struct { UINT32 dwLowDateTime; UINT32 dwHighDateTime; } GeneralizedTime; typedef struct { UINT32 uCertLen; void *certInfo; } Certificate; typedef struct { BOOL bReadLog; BOOL bWriteLog; BOOL bSignLog; BOOL bEncrypt; BOOL bDecrypt; BOOL bSeal; BOOL bAddWaterMark; BOOL bAddFingerPrint; BOOL bPrint; BOOL bSend; BOOL bReceive; BOOL bCut; BOOL bSign; } LogConfig, *PLogConfig; typedef struct { Certificate user; BOOL bRead; UINT32 nTotalReadCount; UINT32 nRemainReadCount; BOOL bModify; BOOL bDelete; BOOL bPrint; UINT32 nTotalPrintCount; UINT32 nRemainPrintCount; } SFACE; typedef struct { SFACE ace; UINT32 nSessionKeyLen; void *sessionKey; } Receiver, *PReceiver; typedef struct { UINT32 uSymAlg; UINT32 uAsymAlg; UINT32 uHashAlg; } CipherMethod, *PCipherMethod; typedef struct { UINT32 nReceiverCount; Receiver** receiver; } ReceiverList, *PReceiverList; typedef struct tagEncyrptAttribute { BOOL bEncrypt; CipherMethod cipherMethod; ReceiverList receiverList; } EncryptAttribute, *PEncyrptAttribute; typedef struct tagSignAttribute { CipherMethod cipherMethod; Certificate Signer; UINT32 nSignMessageLen; void* signMessage; } SignAttribute, *PSignAttribute; typedef struct { EncryptAttribute encryptAttribute; SignAttribute signAttribute; } SecurityAttribute, *PSecurityAttribute; typedef struct { Certificate exCertificate; Certificate sigCertificate; } SToken, *PSToken;
    2011年6月15日 2:31
  • 老提示这个错误:尝试读取或写入受保护的内存。这通常指示其他内存已损坏 该函数定义的定义如下:

    extern "C" SFF_API UINT32 SFF_CreateSecurityFile(IN PSToken SID,
    IN char *szSourceFilePath,
    IN char *szDstSffFilePath,
    IN char *szDstSflFilePath,
    IN PSecurityAttribute psa,
    IN PLogConfig plc,
    IN char *fileID,
    IN char *szCreator,
    IN GeneralizedTime *expiredDate);

    (里面的IN是什么意思?*呢?引用?)

    我在c#里是这样定义的:

    [DllImport("SFF.dll")]
    public static unsafe extern UInt32 SFF_CreateSecurityFile(
    SToken sid,
    string sourceFilePath,
    string dstsffFilePath,
    string dstsflFilePath,
    PSecurityAttribute psa,
    PLogConfig plc,
    string fileID,
    string szCreator,
    GeneralizedTime expiredDate);

    c#中也同样定义了c++中的结构体 详细如下:

    -----------c#-----


            [StructLayout(LayoutKind.Sequential)]
            public struct SToken
            {
                public Certificate exCertificate;
                public Certificate sigCertificate;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct Certificate
            {
                public UInt32 uCertLen;
                public object certInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct GeneralizedTime
            {
                public UInt32 dwLowDateTime;
                public UInt32 dwHighDateTime;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct SecurityAttribute
            {
                public EncryptAttribute encryptAttribute;
                public SignAttribute signAttribute;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct SignAttribute
            {
                public CipherMethod cipherMethod;
                public Certificate Signer;
                public UInt32 nSignMessageLen;
                public object signMessage;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct EncryptAttribute
            {
                public bool bEncrypt;
                public CipherMethod cipherMethod;
                public ReceiverList receiverList;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct CipherMethod
            {
                public UInt32 uSymAlg;
                public UInt32 uAsymAlg;
                public UInt32 uHashAlg;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct ReceiverList
            {
                public UInt32 nReceiverCount;
                public Receiver receiver;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct Receiver
            {
                public SFACE ace;
                public UInt32 nSessionKeyLen;
                public object sessionKey;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct SFACE
            {
                public Certificate user;
                public bool bRead;
                public UInt32 nTotalReadCount;
                public UInt32 nRemainReadCount;
                public bool bModify;
                public bool bDelete;
                public bool bPrint;
                public UInt32 nTotalPrintCount;
                public UInt32 nRemainPrintCount;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct PLogConfig
            {
                public bool bReadLog;
                public bool bWriteLog;
                public bool bSignLog;
                public bool bEncrypt;
                public bool bDecrypt;
                public bool bSeal;
                public bool bAddWaterMark;
                public bool bAddFingerPrint;
                public bool bPrint;
                public bool bSend;
                public bool bReceive;
                public bool bCut;
                public bool bSign;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct PSecurityAttribute
            {
                public EncryptAttribute encryptAttribute;
                public SignAttribute signAttribute;
            }

        -------------[c/c++]----------------------------------------------------------------------------------

        typedef struct
        {
         UINT32 dwLowDateTime;
         UINT32 dwHighDateTime;
        } GeneralizedTime;

        typedef struct
        {
         UINT32 uCertLen;
         void *certInfo;
        } Certificate;

        typedef struct
        {
         BOOL bReadLog;
         BOOL bWriteLog;
         BOOL bSignLog;
         BOOL bEncrypt;
         BOOL bDecrypt;
         BOOL bSeal;
         BOOL bAddWaterMark;
         BOOL bAddFingerPrint;
         BOOL bPrint;
         BOOL bSend;
         BOOL bReceive;
         BOOL bCut;
         BOOL bSign;
        } LogConfig, *PLogConfig;

        typedef struct
        {
         Certificate  user;
         BOOL   bRead;
         UINT32   nTotalReadCount;
         UINT32   nRemainReadCount;
         BOOL   bModify;
         BOOL   bDelete;
         BOOL   bPrint;
         UINT32   nTotalPrintCount;
         UINT32   nRemainPrintCount;
        } SFACE;

        typedef struct
        {
         SFACE   ace;
         UINT32   nSessionKeyLen;
         void   *sessionKey;
        } Receiver, *PReceiver;

        typedef struct
        {
         UINT32   uSymAlg;
         UINT32   uAsymAlg;
         UINT32   uHashAlg;
        } CipherMethod, *PCipherMethod;

        typedef struct
        {
         UINT32   nReceiverCount;
         Receiver**  receiver;
        } ReceiverList, *PReceiverList;

        typedef struct tagEncyrptAttribute
        {
         BOOL   bEncrypt;
         CipherMethod cipherMethod;
         ReceiverList receiverList;
        } EncryptAttribute, *PEncyrptAttribute;

        typedef struct tagSignAttribute
        {
         CipherMethod cipherMethod;
         Certificate  Signer;
         UINT32   nSignMessageLen;
         void*   signMessage;
        } SignAttribute, *PSignAttribute;

        typedef struct
        {
         EncryptAttribute encryptAttribute;
         SignAttribute  signAttribute;
        } SecurityAttribute, *PSecurityAttribute;

        typedef struct
        {
         Certificate exCertificate;
         Certificate sigCertificate;
        } SToken, *PSToken;

    2011年6月15日 2:33
  • 结构的声明问题很多,居然有object这种类型不定的东西?传指针的话用IntPtr。不过指向非托管上的地址的指针不能直接当ref struct,要自己手动把数据复制到托管堆。像这种复杂的列集,比较好的方法是用C++/CLI把函数接口重新封装下。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • 已标记为答案 bary,sacc 2011年6月15日 5:56
    2011年6月15日 3:55
    版主