麻烦C#< - 方式> C ++的DllImport"尝试读取或写入受保护的内存" [英] Trouble with C#<->C++ DLLImport "Attempted to read or write protected memory."

查看:152
本文介绍了麻烦C#< - 方式> C ++的DllImport"尝试读取或写入受保护的内存"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是C ++头文件中的代码。

这是一个C ++头文件中的代码。 >

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

这是C ++源文件中的代码

  int LabelStoringSSDsim(int devNum,UCHAR serialLbl [40],UCHAR wwnLbl [40],
UCHAR ConfigID [5],UCHAR FrmRev [8] ,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;

}



  [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);

所以当我到达最后一位代码时,我得到尝试读取或写入保护内存,这通常表明其他内存已损坏。错误。



我以前没有像这样导入DLL的经验,我做了很多搜索,但似乎找不到问题的解决方案。



我尝试从头开始,用一个简单的函数返回一个整数,这是有效的。然后我添加了一个int给我传递给函数,它仍然工作。然后我添加了一个字节数组,让我通过,这工作。然后我试图把这个字节数组变成一个引用,它失败了。



任何帮助都非常感谢。

解决方案

尝试将 [In] 改为 [In,Out] 。我也不确定在一个参数中同时使用 ref [In,Out] (编辑:Hans Passant在下面的评论中对这两者之间的区别有很好的解释。)



请参阅这篇MSDN文章更多的信息,特别是数组,字符串和接口)由于性能原因而被封装为In参数。除非将InAttribute和OutAttribute(或只是OutAttribute)应用于方法参数,否则不会看到对这些类型的更改。


I have a C++ dll which has a function within it I am trying to call from a C# application.

Here's 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's 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 C# relevant 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 there I get the "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." error.

I have no prior experience in importing DLL's like this and I've done a lot of searching but cannot seem to find a solution to the problem.

I tried starting over from scratch with a simple function returning an integer, and that worked. Then I added an int for me to pass to the function and it still worked. Then I added a byte array for me to pass, which worked. Then I attempted to turn that byte array into a reference and it failed. So my guess is I'm getting the data back incorrectly.

Any help is greatly appreciated.

解决方案

Try changing [In] to [In, Out]. I'm also not sure about using both the ref and [In, Out] keywords together on a single argument. (Edit: Hans Passant has a good explanation of the differences between the two in his comment below.)

See this MSDN article for more information, especially the passage, "By default, reference types (classes, arrays, strings, and interfaces) passed by value are marshaled as In parameters for performance reasons. You do not see changes to these types unless you apply InAttribute and OutAttribute (or just OutAttribute) to the method parameter."

这篇关于麻烦C#&LT; - 方式&gt; C ++的DllImport&QUOT;尝试读取或写入受保护的内存&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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