Attempted to read or write protected memory with dllimport in c#

做~自己de王妃 提交于 2019-12-12 16:50:54

问题


I have a problem with my project: In 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;
}

and I use to dllimport in c#:

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

when i run the program, this program show error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

what error do you know and how to resolved ? thank you!


回答1:


You don't need a convertStringToByte method here. You can tell the runtime to marshal your string as a char *. Also, I would suggest that you make the method return an IntPtr, like this:

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

You can then call it with:

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

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



回答2:


Well, for one thing, the C function is called buildKDTree, but you are importing it in the C# code with entry point "buildKDTreeWithFeatures". Try making these consistent and see if you get better results.




回答3:


I try to call it :

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

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

but it is not your fault where you said. I think the variable intPtr tree in function results([MarshalAs(UnmanagedType.LPStr)]string imgInput, IntPtr tree); that caused the error




回答4:


I think its similar problem due to char* parameters, in my own problem thanks to this below link question solves the problem.

So your only solution is to pass the string parameters as IntPtr. Allocate the memory with Marshal.StringToHGlobalAnsi

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



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!