Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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?
Posted
Updated 25-Jul-11 3:33am
v3
Comments
[no name] 25-Jul-11 5:23am    
Show the source code which threw the error.
Sergey Alexandrovich Kryukov 25-Jul-11 9:59am    
Not clear. Where is the C++ part, what was P/Invoked? Where the exception is thrown?
--SA
DaveyM69 25-Jul-11 14:41pm    
We need to see the C++ declaration of the function and your C# P/Invoke version of it.
jonaschu 25-Jul-11 20:25pm    
the problem is i don't have the c++ source..
headaches now.. the c++ source is from my supplier..
Philippe Mori 25-Jul-11 22:01pm    
How would you uses it from C++. You should have an header file that would allows you to use the DLL from C++. Then you could probably do the appropriate translation.

1 solution

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
 
Share this answer
 
Comments
jonaschu 26-Jul-11 21:24pm    
so how to i check for this? is it any work around method?
Max Bogon 27-Jul-11 14:57pm    
There is no workaround. You might try finding the TP_ISSUE_DATA struct definition in your dis-assembly. Then determine how many bytes each field is in C++; then find an appropriate C# data type (same number of bytes) and define your structure that way. You are going to have to trust your disassembled code. ANSII uses one byte per character, UNICODE uses 2 bytes per character, as a general rule. You have to watch you arithmetic data type sizes as well. Trial and error is the only way that I know of.
jonaschu 29-Jul-11 0:28am    
thanks max, i try to work for this..
is really take my time..
my friend tell me maybe is the pointer problem.
im not pro in C++, bad luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900