当前位置:编程学习 > C#/ASP.NET >>

c#.net调用vc++写的dll文件,出现错误求高手改正~

dll文件原函数是:extern "C" _declspec(dllexport) void Encrypt(unsigned int PublicKey,unsigned int mod, FILE* fipRe, FILE* fipWr,char* extrName );

c#调用为:
[DllImport("encryption.dll", CallingConvention = CallingConvention.Cdecl)]
         public static extern void Encrypt(uint PublicKey, uint mod, IntPtr fipRe, IntPtr fipWr, string extrName);

 private void Encrypt_OK_Click(object sender, EventArgs e)
        {
            uint PublicKey = Convert.ToUInt32(Pu_K);
            uint mod = Convert.ToUInt32(Mo);
            IntPtr fipRe = Marshal.StringToHGlobalAnsi(Encrypt_label1.Text);
            IntPtr fipWr = Marshal.StringToHGlobalAnsi(Encrypt_label2.Text);
            string extrName=null;
            Encrypt(PublicKey,mod,fipRe,fipWr,extrName);
        }

运行报错:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”
求高手指教,不胜感激! --------------------编程问答-------------------- 给你一个例子,可以调用getLastError()查一查到底C++出错里面出错的信息是什么

 

        //创建(即连接)设备用的API
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        public static extern IntPtr CreateFile(
            String lpFileName,   // file name
            EFileAccess dwDesiredAccess,   // access mode
            EFileShare dwShareMode, // share mode
            // SecurityAttributes attr, // SD
             IntPtr attr, // SD
            ECreationDisposition dwCreationDisposition, // how to create
            EFileAttributes dwFlagsAndAttributes, // file attributes
            IntPtr hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern uint GetLastError();
--------------------编程问答-------------------- 真折服你了. fipRe fipWr这个明显传递的是一个文件句柄嘛...

不同平台之间的文件句柄怎么传递啊?我还想请教呢.

Encrypt(unsigned int PublicKey,unsigned int mod, FILE* fipRe, FILE* fipWr,char* extrName );

在C#里 IntPtr是句柄. 但是我不知道怎么传递给C
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,