在c#中尝试使用dllimport读取或写入受保护的内存

时间:2013-05-21 16:09:30

标签: c# c++ web-services dllimport

我的项目有问题: 在dll c ++中:

    extern "C" __declspec(dllexport) int results(char* imgInput, void* tree)
{   
    struct kd_node* nodeTree = new(tree)kd_node ; // new kd_tree with data from memory address
    ...

    ...
    int ret = atoi(retValueStr.c_str());
    return ret;
}

extern "C" __declspec(dllexport) void* buildKDTree(char* folder)
{
    struct kd_node* kd_root;
    ....

    feature *LFData = listFeat.data();
    kd_root = kdtree_build(LFData,listFeat.size());
    void* address_kdtree = (void*)&kd_root; // get memory address of kd_tree
    return address_kdtree;
}

我在c#中使用dllimport:

[DllImport(@"kdtreeWithsift.dll", EntryPoint = "buildKDTree", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void* buildKDTree(byte[] urlImage);


[DllImport(@"kdtreeWithsift.dll", EntryPoint = "results", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I4)]
public unsafe static extern int results(byte[] imgInput, void* tree);

static unsafe void Main()
{
     string urlImg1 = "C:/Users../test img/1202001T1.jpg";
     string urlImg = "C:/export_features"; 

     try
     {  
     IntPtr result;
     int result1;
     result1 = results(convertStringToByte(urlImg1), 5, buildKDTree(convertStringToByte(urlImg))); //  this error
     Console.WriteLine("results = %d",result1);
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex);
          Console.ReadLine();
     }
}

当我运行程序时,此程序显示错误: 尝试读取或写入受保护的内存。这通常表明其他内存已损坏

您知道什么错误以及如何解决? 谢谢!

4 个答案:

答案 0 :(得分:1)

此处您不需要convertStringToByte方法。您可以告诉运行时将您的字符串封送为char *。另外,我建议你让方法返回IntPtr,如下所示:

[DllImport(@"kdtreeWithsift.dll", EntryPoint = "buildKDTree",
    CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr buildKDTree([MarshalAs(UnmanagedType.LPStr)]string urlImage);

[DllImport(@"kdtreeWithsift.dll", EntryPoint = "results",
    CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I4)]
public static extern int results([MarshalAs(UnmanagedType.LPStr)]string imgInput, IntPtr tree);

然后你可以用:

来调用它
IntPtr tree = buildKDTree(urlImg);
int result1 = results(urlImg, 50, tree);

Console.WriteLine("results = {0}",result1);

答案 1 :(得分:0)

嗯,首先,C函数名为buildKDTree,但是您要在C#代码中使用入口点“buildKDTreeWithFeatures”将其导入。尝试使这些一致,看看你是否会得到更好的结果。

答案 2 :(得分:0)

我试着称之为:

IntPtr tree = buildKDTree(urlImg);
int result1 = results(urlImg, 50, tree);

Console.WriteLine("results = {0}",result1);

但你说的不是你的错。 我认为函数intPtr tree中的变量results([MarshalAs(UnmanagedType.LPStr)]string imgInput, IntPtr tree);导致了错误

答案 3 :(得分:0)

我认为由于 char * 参数导致类似问题,在我自己的问题中,由于以下链接问题解决了这个问题。

  

因此,您唯一的解决方案是将字符串参数作为IntPtr传递。   使用Marshal.StringToHGlobalAnsi

分配内存

https://stackoverflow.com/questions/16674616/attempted-to-read-or-write-protected-memory-with-dllimport-in-c-sharp