VS2013 Natvis LegacyAddin 使用方法

1. 自定义类型: MyType

class MyType
{
public:
	char m_strName[32];
	int m_nAge;
};


2. 编写 natvis 文件: MyType.natvis

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="MyType">
    <DisplayString LegacyAddin="MyType.dll" Export="AddIn_MyType"></DisplayString>
  </Type>
</AutoVisualizer>


3. 生成MyType.dll, 导出函数 AddIn_MyType

.h
typedef struct tagDEBUGHELPER
{
	DWORD dwVersion;
	HRESULT(WINAPI *ReadDebuggeeMemory)(struct tagDEBUGHELPER *pThis, DWORD dwAddr, DWORD nWant, VOID* pWhere, DWORD *nGot);
	// from here only when dwVersion >= 0x20000
	DWORDLONG(WINAPI *GetRealAddress)(struct tagDEBUGHELPER *pThis);
	HRESULT(WINAPI *ReadDebuggeeMemoryEx)(struct tagDEBUGHELPER *pThis, DWORDLONG qwAddr, DWORD nWant, VOID* pWhere, DWORD *nGot);
	int (WINAPI *GetProcessorType)(struct tagDEBUGHELPER *pThis);
} DEBUGHELPER;

typedef HRESULT(WINAPI *CUSTOMVIEWER)(DWORD dwAddress, DEBUGHELPER *pHelper, int nBase, BOOL bUniStrings, char *pResult, size_t max, DWORD reserved);

.cpp
static LRESULT ReadMemory(DEBUGHELPER *pHelper, DWORDLONG qwAddr, DWORD nSize, VOID* pBuf, DWORD *nGot)
{
	if (pHelper->dwVersion < 0x20000)
	{
		// Visual C++ 6.0 version
		if (pHelper->ReadDebuggeeMemory(pHelper, qwAddr, nSize, pBuf, nGot) != S_OK)
		{
			return E_FAIL;
		}
	}
	else
	{
		if (pHelper->ReadDebuggeeMemoryEx(pHelper, qwAddr, nSize, pBuf, nGot) != S_OK)
		{
			return E_FAIL;
		}
	}

	return S_OK;
}

__declspec(dllexport) HRESULT __stdcall AddIn_MyType(DWORD dwAddress, DEBUGHELPER *pHelper, int nBase, BOOL bUniStrings, char *pResult, size_t max, DWORD reserved)
{
	// read file time from debuggee memory space
	if (pHelper->dwVersion >= 0x20000)
	{
		dwAddress = pHelper->GetRealAddress(pHelper);
	}

	DWORD nGot;
	char name[32] = { 0 };
	int age = -1;

	ReadMemory(pHelper, dwAddress, sizeof(name), name, &nGot);
	ReadMemory(pHelper, dwAddress+ 32, sizeof(age), &age, &nGot);

	sprintf_s(pResult, max, "name: %s, age: %d", name, age);

	return S_OK;
}

4. 部署并测试

将  MyType.natvis 和 MyType.dll 一起放入下面文件夹中的其中之一
%VSINSTALLDIR%\Common7\Packages\Debugger\Visualizers (requires admin access)

%USERPROFILE%\My Documents\Visual Studio 2012\Visualizers\

VS extension folders

测试结果如下:


参考文章:https://stackoverflow.com/questions/11545418/how-to-write-a-custom-native-visualizer-dll-for-visual-studio-2012-debugger/11545420#11545420
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值