C#–尝试读取或写入受保护的内存,这通常指示其他内存已损坏。

2021年11月8日 开发日志 评论已被关闭

记:

近期在C#中调用别人的DLL的时候有时候出现了 尝试读取或写入受保护的内存 。这通常指示其他内存已损坏 的问题。

 

错误类型:System.AccessViolationException。

 

问题位置:在与C++ dll规定传参的类型用的是string导致

 

  问题原因:C++ string类的字符在内存的储存位置:数据<=16字节,在当前栈区;数据>16字节,在堆区

       C# string是存放在堆区的。

 

解决方案:在传值的时候用指针,再做转换就好了。

    public class APP_DLL
    {
        [DllImport("ruihua.dll", CallingConvention = CallingConvention.Cdecl)] //引入dll,并设置字符集
        public static extern int test(byte[] src1, int w1, int h1, int channel1, byte[] src2, int w2, int h2, int channel2, string str);
    }

改为:

    public class APP_DLL
    {
        [DllImport("ruihua.dll", CallingConvention = CallingConvention.Cdecl)] //引入dll,并设置字符集
        public static extern int test(byte[] src1, int w1, int h1, int channel1, byte[] src2, int w2, int h2, int channel2, IntPtr str);
    }

C# string转IntPtr方法:

  IntPtr ptrIn = Marshal.StringToHGlobalAnsi(obj.ToString());

C# IntPtr转string方法:

  string retlust = Marshal.PtrToStringAnsi(ptrIn);  

转之   https://www.cnblogs.com/mexihq/p/12696579.html


查看数据库中那个表占用的空间多

2021年10月28日 开发日志 评论已被关闭
create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100)) 
 
declare @name varchar(100) 
declare cur cursor  for 
    select name from sysobjects where xtype='u' order by name 
open cur 
fetch next from cur into @name 
while @@fetch_status=0 
begin 
    insert into #data 
    exec sp_spaceused   @name 
    print @name 
 
    fetch next from cur into @name 
end 
close cur 
deallocate cur 
 
create table #DataNew(name varchar(100),row int,reserved int,data int,index_size int,unused int) 
 
insert into #dataNew 
select name,convert(int,row) as row,convert(int,replace(reserved,'KB','')) as reserved,convert(int,replace(data,'KB','')) as data, 
convert(int,replace(index_size,'KB','')) as index_size,convert(int,replace(unused,'KB','')) as unused from #data  
 
select * from #dataNew order by data desc

Vue技术 Vue中的数据代理

2021年10月26日 开发日志 评论已被关闭

5 4 3 2 1

Object defineProperty

2021年10月26日 网站维护 评论已被关闭

更换iis的https证书

2021年10月25日 网站维护 评论已被关闭

阿里云oss上传回调错误及排除

2020年2月16日 开发日志 评论已被关闭

IFrame中弹出登录页面问题

2020年2月11日 开发笔记 评论已被关闭

A server error occured on the current command. The results, if any, should be discarded

2019年9月15日 开发日志 评论已被关闭

请求筛选模块被配置为拒绝包含双重转义序列的请求。

2018年12月24日 开发日志 评论已被关闭

遇到一个奇怪的问题

2018年12月24日 开发日志 评论已被关闭