您的位置:首页 > 编程语言 > C语言/C++

方便调试vc++的dll(序列化和反序列化 结构体或者类的问题)

2010-07-14 16:37 429 查看
最近需要调试从c#调用c++的dll,dll都是vc6.0编出来的,固定的调用方法和调试方法 【调用就是在c#中重新封了一个接口,只要参数的数据类型对应上即可;调试也是直接创建新的exe进行调用】 !

1.首先是设置vc6.0的配置环境:



2.每次都需要在别的地方监控输入dll里面的参数的之,如果是一个字符串还行,但是是一个结构体或者是一个类的话,那么属性是很多的,所以需要一个中间的转换过程,避免人工操作,所以用到了将某个参数(结构体或者自定义类)序列化,在exe调试的时候需要传入相同的参数,只要将原来的参数反序列化即可!

序列化relData:

try
{
// Insert code to set properties and fields of the object.
System.Xml.Serialization.XmlSerializer mySerializer = new System.Xml.Serialization.XmlSerializer(typeof(RELDATA));
// To write to a file, create a StreamWriter object.
System.IO.StreamWriter
//序列化结构体到relData.xml
myWriter = new System.IO.StreamWriter("E://relData.xml");
mySerializer.Serialize(myWriter, relData);
myWriter.Dispose();
}
catch(Exception ee) {
LogUtil.WriteLog("生成失败: "+ee.Message);
}


反序列化结构体relData:

RELDATA relData = new RELDATA();
//----------反序列化 ----
//xml序列化的方式只能保存public的字段和可读写的属性,对于private等类型的字段不能进行序列化
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
try
{
XmlSerializer mySerializer = new XmlSerializer(typeof(RELDATA));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream("E://relData.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
// Calls the Deserialize method and casts to the object type.
relData = (RELDATA)mySerializer.Deserialize(myFileStream);
//----------反序列化 ----
}
catch { }


大功告成!

3.顺带记录下在vs2008上面通过c#创建exe调用c++的dll的方法:(两者都为release版本或者debug)

(1)将c#写的exe和c++都拉到一个工程solution里面,然后将exe生成的目录和c++的dll生成的目录设为同一个!

然后再exe里面封装一个函数,如下:

/// <summary>
/// 生成证书文件
/// </summary>
/// <returns></returns>
[
DllImport("CoreX.dll",
EntryPoint = "CreateAPI",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.StdCall)
]
//public static extern int CreateAPI(RELDATA relData, byte[] rel, ref int relLength);
public static extern int CreateAPI(RELDATA relData, byte[] rel );


(2)exe工程相关的设置如下:



(3)c++工程设置:

VS2008 Release 工程调试修改方式:

项目 -> 属性 -> C/C++ -> 常规 -> 调试信息格式 -> 用于 “编辑并继续” 的程序数据库(/ZI)

项目 -> 属性 -> C/C++ -> 优化 -> 优化 -> 禁用(/Od)

项目 -> 属性 -> 链接器 -> debugging -> 生成调试信息 -> 是(/DEBUG)

(4)将exe设为启动项,直接调试即可进入断点!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: