在DLL中使用的问题 [英] The problem to use in DLL

查看:88
本文介绍了在DLL中使用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用C ++ DLL的C#中遇到问题.

这是错误:

试图读取或写入受保护的内存.这通常表明其他内存已损坏.

有这个主意吗?

I meet a problem in c# that use the C++ DLL.

This is the error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Any idea for this?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct TP_ISSUE_DATA
        {
            public Byte CardType;
            public Byte IssueCount;
            public UInt32 CID;
            public UInt32 Sum;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line1;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line2;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line3;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line4;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line5;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string line6;
        }
 
TP_ISSUE_DATA tpissue = new TP_ISSUE_DATA();
tpissue.CardType = 3;
tpissue.IssueCount = 1;
tpissue.CID = 1234;
tpissue.Sum = 1000;
tpissue.line1 = "Line 1";
tpissue.line2 = "Line 2";
tpissue.line3 = "Line 3";
tpissue.line4 = "Line 4";
tpissue.line5 = "Line 5";
tpissue.line6 = "Line 6";

if (TP_Open(Handle, 1) == 0)
{
    int iInite = TP_Issue(tpissue);
}



TP_ISSUE是DLL中的函数,我尝试过使用它,但是它会显示错误消息.

我在网上搜索,对此有所了解.
C ++和C#的类型声明是不同的,所以得到了此消息,我也尝试匹配该声明,仍然相同.



TP_ISSUE is the function in the DLL, I try want to use it, but it come out the error message.

I search from the web, got some idea for this.
The type declaration for the C++ and C# are difference so got this message and I also try to match the declaration already, still the same.

How?

推荐答案

这可能是C ++使用ANSII而C#使用UNICODE字符串的事实.您收到的错误将指示溢出情况(读取超出字符串末尾).只是一个猜测
It might be the fact that C++ uses ANSII while C# uses UNICODE strings. The error you are getting would indicate an overflow condition (reading past the end of the string). Just a guess


这篇关于在DLL中使用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆