C#调用C++dll报错ACCESS VIOLATION

wangzhihongji wangzhihongji 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
原帖地址:http://bbs.csdn.net/topics/391012458
代码改了一下,问题还没有解决。于是重开一个贴

-----------------------------------------------------C#代码

namespace WpfApplication2
{
class MyClass1 : INotifyPropertyChanged
{
public MyClass1()
{
result="null";
}
private string result;

public string Result
{
get { return result; }
set { result = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Result"));
}
}

}
public event PropertyChangedEventHandler PropertyChanged;
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>

public class mytransdll
{
[DllImport("mytransdll.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int mytransfunc(StringBuilder exportpath, StringBuilder importpath);
}
public partial class Main : Window
{

MyClass1 myout=new MyClass1 ();
private StringBuilder exportpath;
private StringBuilder importpath;
public Main()
{
InitializeComponent();
myout.Result = "there will show ,if transform is ok ";// Result 赋值应该放到Main的构造函数或者窗口的Loaded事件中赋值的。
//把结果binding上去。。。请注意,XAML里的binding并不能访问C#里的变量,而C#里的变量却可以访问XAML
Binding bind1 = new Binding();
bind1.Source = myout;
bind1.Path = new PropertyPath("Result");
this.MyLabel.SetBinding(Label.ContentProperty, bind1);


}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//int isornotsuccess = 1;

int isornotsuccess = mytransdll.mytransfunc(exportpath, importpath);
myout.Result = " Transforming,please wait....";
Binding bind3 = new Binding();
bind3.Source = myout;
bind3.Path = new PropertyPath("Result");
this.MyLabel.SetBinding(Label.ContentProperty, bind3); //按下按钮之后马上显示正在传输
if (isornotsuccess == 1)
{
myout.Result = exportpath.ToString() ;
Binding bind2 = new Binding();
bind2.Source = myout;
bind2.Path = new PropertyPath("Result");
this.MyLabel.SetBinding(Label.ContentProperty, bind2); //这里写成功后的反应
}
}

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //key是一个类,这是一个类的判断
{
StringBuilder exportpath=new StringBuilder(this.textBox1.Text,200);


}
}



---------------------------------------------------C++编写的DLL

//

#include "stdafx.h"
#include "stdio.h"
#include"pcap.h"
#include<WinSock.h>
#include"packet32.h"
#include<Win32-Extensions.h>
#include "stdlib.h"



extern "C" _declspec(dllimport) int mytransfunc(char* exportpath,char* importpath);
int mytransfunc(char* exportpath,char* importpath)
{
//和EXPORTPATH无关的代码
char A[]="regenerate_original_picture.txt";
char PA[1024];
char exportpathvalue=*exportpath;
//!!!!高能,就是这句出问题。不管是用这种方法中转还是直接调用*exportpath都出现报错
sprintf(PA,"%s\\%s",exportpathvalue,A);//连接字符串
FILE *fpopen = fopen(PA, "rb");
//和EXPORTPATH无关的代码
}


-----------------------------------------问题
C#编写了一个UI界面,调用DLL的时候传入文件所在地址,然后由DLL对其操作
测试过DLL程序的正确性。

之前大神们说的,如使用stringbuilder,DllImport函数的参数的设定都不管用。有网友说是关于托管代码和非托管代码内存管理的问题。。。楼主是新手,不太明白,还请指教。
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
wangzhihongji
C# 111,128 成员 642,536 内容