Problem with C # & # 8596; C ++ DLLImport "Attempting to read or write protected memory."

I have a C ++ dll that has a function inside. I am trying to call from a C # application.

Here is the code in the C ++ header file

extern "C" _declspec(dllexport) int LabelStoringSSDsim(int devNum, UCHAR serial[40], UCHAR wwn[40],
                UCHAR ConfigID[5], UCHAR FrmRev[8], UCHAR DevName[40], int eCode);

Here is the code in the C ++ source file

int LabelStoringSSDsim(int devNum, UCHAR serialLbl[40], UCHAR wwnLbl[40],
                UCHAR ConfigID[5], UCHAR FrmRev[8], UCHAR DevName[40], int eCode)

{

string strConfigID="12111";                                     //5 bytes
string strFrmRev="1.25....";                                    //8 bytes
string strDevName="ABC-123.................................";   //40 bytes

for (int i=0;i<5;i++)
    ConfigID[i] = strConfigID[i];

for (int i=0;i<8;i++)
    FrmRev[i] = strFrmRev[i];

for (int i=0;i<40;i++)
    DevName[i] = strDevName[i];
return eCode;

}

Here's the corresponding C # code

        [DllImport("LabelStoring.dll")]
    static extern int LabelStoringSSDsim(
        int devNum,
        byte[] strserial,
        byte[] strwwn,
        [In] ref byte[] ConfigID,
        [In] ref byte[] FrmRev,
        [In] ref byte[] DevName,
        int eCode);


int errNum = LabelStoringSSDsim(devNum, bserial, bwwn, ref ConfigID, ref FrmRev, ref DevName, 123123);

So, when I get to the last bit of code, I get "Attempting to read or write protected memory. This often indicates that another memory is damaged." error.

I have no experience in importing DLLs, and I searched many times, but could not find a solution to the problem.

, , . int , . , , . , . , .

.

+5
2

[In] [In, Out]. , ref, [In, Out] . (: .)

. MSDN , " (, , ) , In . , InAttribute OutAttribute ( OutAttribute) ."

+5

, interop Windows 7. XP Win 7, XP.

, , , , (WCHAR *).

, , .Net 3.5 ... .

, XP, Win 7:

[DllImport("NativeBin.dll")]
public static extern String GetWCharStr();

, Win 7 XP:

[DllImport("NativeBin.dll")]
private static extern IntPtr GetWCharStr();
public static String GetString()
{
    return Marshal.PtrToStringUni(GetWCharStr());
}
+1

All Articles