C#调用dll “尝试读取或写入受保护的内存。这通常指示其他内存已损坏"
该DLL是将DICOM图片格式传到PIVIEW中。该程序在VC里已经实现,现要在C#里实现。dll里方法接口为BOOL __stdcall dicom_storescu_execute(LPCSTR servername, int serverport,
LPCSTR servertitle, LPCSTR clienttitle,
LPCSTR filelist, STORESCUCALLBACK callback);
其中STORESCUCALLBACK 是一个函数指针typedef void (__stdcall * STORESCUCALLBACK)(T_StoreProgress* progress);。
调用为:
isSuccessed = dicom_storescu_execute(TheadIPadress,
TheadServerPort ,
TheadServerTitle,
TheadClientTitle,
TheadImageFile ,
MyStoreScuCallback);
MyStoreScuCallback为一函数名
在C#中
public delegate void STORESCUCALLBACK(T_StoreProgress progress);
[DllImport("dcmtrans.dll", EntryPoint = "dicom_storescu_execute")]
public static extern bool dicom_storescu_execute(string servername,
int serverport, string servertitle, string clienttitle,
string filelist, STORESCUCALLBACK callback);
调用为
internal unsafe void startSCU(Object lpParam)
{
StoreSCUThead scu_CS = (StoreSCUThead)lpParam;
string TheadIPadress = scu_CS.strIPadress;
int TheadServerPort = scu_CS.strServerPort;
string TheadServerTitle = scu_CS.strServerTitle;
string TheadClientTitle = scu_CS.strClientTitle;
string TheadImageFile = scu_CS.strImageFile;
int mxh = scu_CS.Theadxh;
bool isSuccessed = false;
STORESCUCALLBACK callback = new STORESCUCALLBACK(MyStoreScuCallback);
isSuccessed = dicom_storescu_execute(TheadIPadress,
TheadServerPort,
TheadServerTitle,
TheadClientTitle,
TheadImageFile,
callback);
if (!isSuccessed)
falseNum++;
}
MyStoreScuCallback为函数名
public void MyStoreScuCallback(T_StoreProgress progress)
{
switch (progress.state)
{
case T_StoreProgressState.StoreBegin:
break;
case T_StoreProgressState.StoreProgressing:
break;
case T_StoreProgressState.StoreEnd:
break;
default:
break;
}
}
其中startSCU是开辟一个线程调用的
internal unsafe void OnSend(string path)
{
string m_aetitle="";
string m_ip = "";
int m_port = 0;
string m_Filename = "";
string m_path = "";
StringBuilder m_tmp = new StringBuilder(255);
m_Filename = path + "test.dcm";
m_path = path + "user_setting.ini";
GetPrivateProfileString("SetupDCM", "aetitle", null, m_tmp, 15, m_path);
m_aetitle = m_tmp.ToString();
GetPrivateProfileString("SetupDCM" , "ip" , null , m_tmp , 15 , m_path);
m_ip = m_tmp.ToString();
GetPrivateProfileString("SetupDCM", "port", null, m_tmp, 15, m_path);
m_port = Int32.Parse(m_tmp.ToString());
StoreSCUThead uStoreSCUThead = new StoreSCUThead();
uStoreSCUThead.strIPadress = m_ip;
uStoreSCUThead.strServerPort = m_port;
uStoreSCUThead.strServerTitle = "OPACS";
uStoreSCUThead.strClientTitle = m_aetitle;
uStoreSCUThead.strImageFile = m_Filename;
uStoreSCUThead.Theadxh = 1;
//Thread fThread = new Thread(new ThreadStart(startSCU));
Thread fThread = new Thread(startSCU);
fThread.Start(uStoreSCUThead);
fThread.Join(1000);
}
一编译就出错在调用函数dicom_storescu_execute“这报尝试读取或写入受保护的内存”的错。请问各位大哥,我是不是函数指针转化的类型出错啦?而且我把MyStoreScuCallback里参数换为不是指针类型的了。这些有影响吗?