//extern "C" CP_API int __stdcall char* GetValueStr(const char* propertyName);
[DllImport("ZSDK.dll", CharSet = CharSet.Ansi)]
public static extern string GetValueStr(string propertyName);
在xp下运行正常,在win7系统下使用提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”是不是我的声明有问题,还请高人解答?
[DllImport("ZSDK.dll", CharSet = CharSet.Ansi)]
public static extern string GetValueStr(string propertyName);
在xp下运行正常,在win7系统下使用提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”是不是我的声明有问题,还请高人解答?
解决方案 »
- 获取实际屏幕尺寸(手机/电脑)
- 【高手进】:datagridview怎么美化啊 啊有什么好的方法啊 或者重画之类的
- C# asp.net 生成excel 报表
- 请问一下dll里的Setting问题
- Collaboration Applications 是什么意思
- hashtable的问题?
- 调用方法AddTree('M', (TreeNode)null)r提示”无法从string到char"
- 请教:在C#中如何调用MSSQLServer的存储过程?
- TreeView1_SelectedNodeChanged失效
- 能否根据ip地址判断来实现访问页面内容
- 如何获得第三方程序中表格里的所有单元格
- Form中BackgroundImage加载图片问题
[DllImport("ZSDK.dll",CharSet=CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string GetValueStr([MarshalAs(UnmanagedType.BStr)]string propertyName);
http://msdn.microsoft.com/en-us/library/f1cf4kkz(VS.80).aspxHowever, if you define the method as a platform invoke prototype, replace each BSTR type with a String type, and call MethodOne, the common language runtime attempts to free b twice. You can change the marshaling behavior by using IntPtr types rather than String types.
[DllImport("ZSDK.dll",CharSet=CharSet.Ansi)]
public static extern IntPtr GetValueStr([MarshalAs(UnmanagedType.BStr)]string propertyName);IntPtr buff=GetValueStr("");
string s=Marshal.PtrToStringBSTR(buff);你需要确定C++中用的是否是BStr类型,如不是可以尝试PtrToStringAnsi ,PtrToStringAuto PtrToStringUni http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal_methods(v=VS.80).aspx
应该以参数形式返回
GetValueStr(const char* propertyName, char *buff, int buffLength);
这样可以由调用方控制内存的申请和释放,避免内存泄露
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string GetValueStr([MarshalAs(UnmanagedType.BStr)]string propertyName);我也遇到过类似的问题,不知道解决办法是否相同。我当时是用一个返回bool类型的DLL函数,也是出现返回值不正确的问题,但后来加上了 [return: MarshalAs(UnmanagedType.I1)]后就没问题了,所以我认为你可能是在 [return: MarshalAs(UnmanagedType.BStr)]这句上有问题,看看string是否应该对应UnmanagedType.BStr这种类型。